1 回答

TA貢獻1851條經驗 獲得超4個贊
根據文檔,全局變量獨立于您的 Lambda 函數的處理程序代碼。這導致緩沖區隨著時間的推移而增加。
糾正后的參考粘貼在下面。
package main
import (
? ? "context"
? ? "fmt"
? ? "os"
? ? "sync"
? ? "github.com/aws/aws-lambda-go/lambda"
? ? "github.com/aws/aws-sdk-go/aws"
? ? "github.com/aws/aws-sdk-go/aws/session"
? ? "github.com/aws/aws-sdk-go/service/dynamodb"
? ? "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
)
func exitWithError(err error) {
? ? fmt.Fprintln(os.Stderr, err)
? ? os.Exit(1)
}
//HandleRequest : Lambda entry point
func HandleRequest(ctx context.Context, data LambdaInputJSON) ([]interface{}, error) {
? ? output := DynamoDBBatchGetRecords(data)
? ? return output, nil
}
func main() {
? ? lambda.Start(HandleRequest)
}
func DynamoDBBatchGetRecords(a LambdaInputJSON) []interface{} {
? ? var dataOut []interface{}
? ? var wg = &sync.WaitGroup{}
? ? var mtx = &sync.Mutex{}
? ? iterations := len(a.Ids)
? ? wg.Add(iterations)
? ? for i := 0; i < i; i++ {
? ? ? ? go func(i int) {
? ? ? ? ? ? defer wg.Done()
? ? ? ? ? ? var outputData []interface{}
? ? ? ? ? ? sess, err := session.NewSession(&aws.Config{
? ? ? ? ? ? ? ? Region: aws.String("aws-region"),
? ? ? ? ? ? })
? ? ? ? ? ? if err != nil {
? ? ? ? ? ? ? ? exitWithError(fmt.Errorf("failed to make Query API call, %v", err))
? ? ? ? ? ? }
? ? ? ? ? ? ddb := dynamodb.New(sess)
? ? ? ? ? ? queryInput := &dynamodb.QueryInput{
? ? ? ? ? ? ? ? Limit:? ? ? ? ? ? aws.Int64(1),
? ? ? ? ? ? ? ? TableName:? ? ? ? aws.String("table"),
? ? ? ? ? ? ? ? IndexName:? ? ? ? aws.String("index"),
? ? ? ? ? ? ? ? ScanIndexForward: aws.Bool(false),
? ? ? ? ? ? ? ? ConsistentRead: aws.Bool(false),
? ? ? ? ? ? ? ? KeyConditions: map[string]*dynamodb.Condition{
? ? ? ? ? ? ? ? ? ? "index-column": {
? ? ? ? ? ? ? ? ? ? ? ? ComparisonOperator: aws.String("EQ"),
? ? ? ? ? ? ? ? ? ? ? ? AttributeValueList: []*dynamodb.AttributeValue{
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? S: aws.String(a.Ids[i]),
? ? ? ? ? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? },
? ? ? ? ? ? }
? ? ? ? ? ? output, err := ddb.Query(queryInput)
? ? ? ? ? ? if err != nil {
? ? ? ? ? ? ? ? exitWithError(fmt.Errorf("E1 failed to make Query API call, %v", err))
? ? ? ? ? ? }
? ? ? ? ? ? err = dynamodbattribute.UnmarshalListOfMaps(output.Items, &outputData)
? ? ? ? ? ? if err != nil {
? ? ? ? ? ? ? ? exitWithError(fmt.Errorf("E2 failed to unmarshal Query result items, %v", err))
? ? ? ? ? ? }
? ? ? ? ? ? mtx.Lock()
? ? ? ? ? ? dataOut = append(dataOut, outputData[0])
? ? ? ? ? ? mtx.Unlock()
? ? ? ? }(i)
? ? }
? ? wg.Wait()
? ? return dataOut
}
- 1 回答
- 0 關注
- 187 瀏覽
添加回答
舉報