Untitled
unknown
golang
15 days ago
1.7 kB
2
Indexable
Never
func (r *sessionRepository) PreserveSession(sessionId, userId string) error { key := fmt.Sprintf("SESSION#%s", sessionId) input := &dynamodb.TransactWriteItemsInput{ TransactItems: []types.TransactWriteItem{ { Update: &types.Update{ TableName: aws.String(r.tableName), Key: map[string]types.AttributeValue{ "pk": &types.AttributeValueMemberS{Value: key}, "sk": &types.AttributeValueMemberS{Value: key}, }, UpdateExpression: aws.String( "SET #current = #current + :incr", ), ExpressionAttributeNames: map[string]string{ "#current": "current", "#pk": "pk", "#headcount": "headcount", }, ExpressionAttributeValues: map[string]types.AttributeValue{ ":incr": &types.AttributeValueMemberN{ Value: "1", }, }, ConditionExpression: aws.String( "attribute_exists(#pk) AND #current < #headcount", ), }, }, { ConditionCheck: &types.ConditionCheck{ TableName: aws.String(r.tableName), Key: map[string]types.AttributeValue{ "pk": &types.AttributeValueMemberS{Value: fmt.Sprintf("USER#%s", userId)}, "sk": &types.AttributeValueMemberS{Value: key}, }, ConditionExpression: aws.String( "attribute_not_exists(pk) AND attribute_not_exists(sk)", ), }, }, }, } _, err := r.client.TransactWriteItems(context.TODO(), input) if err != nil { slog.Error("err/preserve-session", "error", err) var ccf *types.TransactionCanceledException if errors.As(err, &ccf) { return protocol.NewError(protocol.DATA_INTEGRITY_ERROR, "Integrity error") } return err } return nil }
Leave a Comment