Skip to content

Commit 86e5faa

Browse files
committed
chore: use aurora-dsql-connectors v0.3.0 occretry package
Replace custom isOCCError and withOCCRetry with the connector's occretry.Retry. NewPool now returns *pgxpool.Pool directly, removing the pool.Pool unwrap.
1 parent 40e03e9 commit 86e5faa

2 files changed

Lines changed: 4 additions & 38 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/Masterminds/squirrel v1.5.4
1010
github.com/MicahParks/keyfunc/v2 v2.1.0
1111
github.com/Yiling-J/theine-go v0.6.2
12-
github.com/awslabs/aurora-dsql-connectors/go/pgx v0.2.0
12+
github.com/awslabs/aurora-dsql-connectors/go/pgx v0.3.0
1313
github.com/cenkalti/backoff/v4 v4.3.0
1414
github.com/cespare/xxhash/v2 v2.3.0
1515
github.com/containerd/errdefs v1.0.0

pkg/storage/postgres/dsql.go

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,18 @@ package postgres
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
7-
"time"
86

97
"github.com/awslabs/aurora-dsql-connectors/go/pgx/dsql"
10-
"github.com/cenkalti/backoff/v4"
11-
"github.com/jackc/pgx/v5/pgconn"
8+
"github.com/awslabs/aurora-dsql-connectors/go/pgx/occretry"
129
"github.com/jackc/pgx/v5/pgxpool"
1310

1411
"github.com/openfga/openfga/pkg/storage/sqlcommon"
1512
)
1613

17-
// isOCCError checks if the error is a DSQL optimistic concurrency control conflict.
18-
// DSQL returns OC000 for mutation conflicts and OC001 for schema conflicts.
19-
func isOCCError(err error) bool {
20-
if err == nil {
21-
return false
22-
}
23-
var pgErr *pgconn.PgError
24-
if errors.As(err, &pgErr) {
25-
return pgErr.Code == "OC000" || pgErr.Code == "OC001" || pgErr.Code == "40001"
26-
}
27-
return false
28-
}
29-
3014
// withOCCRetry executes fn with automatic retry on DSQL OCC errors.
3115
func withOCCRetry(ctx context.Context, fn func() error) error {
32-
policy := backoff.NewExponentialBackOff()
33-
policy.InitialInterval = 10 * time.Millisecond
34-
policy.MaxElapsedTime = 5 * time.Second
35-
36-
return backoff.Retry(func() error {
37-
err := fn()
38-
if err == nil {
39-
return nil
40-
}
41-
if isOCCError(err) {
42-
return err
43-
}
44-
return backoff.Permanent(err)
45-
}, backoff.WithContext(policy, ctx))
16+
return occretry.Retry(ctx, occretry.DefaultConfig(), fn)
4617
}
4718

4819
// initDSQLDB initializes a new Aurora DSQL database connection.
@@ -73,10 +44,5 @@ func initDSQLDB(uri string, cfg *sqlcommon.Config) (*pgxpool.Pool, error) {
7344
poolCfg.MaxConnIdleTime = cfg.ConnMaxIdleTime
7445
}
7546

76-
pool, err := dsql.NewPool(context.Background(), dsqlCfg, poolCfg)
77-
if err != nil {
78-
return nil, fmt.Errorf("create DSQL pool: %w", err)
79-
}
80-
81-
return pool.Pool, nil
47+
return dsql.NewPool(context.Background(), dsqlCfg, poolCfg)
8248
}

0 commit comments

Comments
 (0)