Skip to content

Commit

Permalink
Add TTL to Amazon DynamoDB deduplication storage driver
Browse files Browse the repository at this point in the history
  • Loading branch information
maestre3d committed Apr 24, 2023
1 parent 3003cdb commit 2da78b0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
4 changes: 4 additions & 0 deletions driver/dynamodb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ In order for this driver to work, the database MUST have a deduplication table w
{
"AttributeName": "message_id",
"AttributeType": "S"
},
{
"AttributeName": "expiration_time",
"AttributeType": "N"
}
],
"BillingMode": "PAY_PER_REQUEST"
Expand Down
22 changes: 14 additions & 8 deletions driver/dynamodb/deduplication_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ import (
"context"
"log"
"os"
"strconv"
"time"

"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"

"github.com/alexandria-oss/streams"
"github.com/aws/aws-sdk-go-v2/aws"

"github.com/aws/aws-sdk-go-v2/service/dynamodb"

"github.com/alexandria-oss/streams"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)

// DeduplicationStorageConfig is the configuration schema for Amazon DynamoDB streams.DeduplicationStorage implementation.
type DeduplicationStorageConfig struct {
TableName string
Logger *log.Logger
ErrorLogger *log.Logger
TableName string
Logger *log.Logger
ErrorLogger *log.Logger
RowTTLDuration time.Duration // Total duration for a row to be available; DynamoDB will remove the row automatically if configured.
}

// DeduplicationStorage is the Amazon DynamoDB streams.DeduplicationStorage
Expand All @@ -40,6 +40,9 @@ func NewDeduplicationStorage(cfg DeduplicationStorageConfig, client *dynamodb.Cl
cfg.ErrorLogger = logger
}
}
if cfg.RowTTLDuration == 0 {
cfg.RowTTLDuration = time.Minute * 60
}
return DeduplicationStorage{
client: client,
cfg: cfg,
Expand All @@ -56,6 +59,9 @@ func (d DeduplicationStorage) Commit(ctx context.Context, workerID, messageID st
"worker_id": &types.AttributeValueMemberS{
Value: workerID,
},
"expiration_time": &types.AttributeValueMemberN{
Value: strconv.Itoa(int(time.Now().UTC().Add(d.cfg.RowTTLDuration).Unix())),
},
},
TableName: d.tableRef,
ConditionExpression: nil,
Expand Down

0 comments on commit 2da78b0

Please sign in to comment.