Documentation ¶
Overview ¶
Example ¶
package main import ( "context" "fmt" "log" "os" "os/signal" "sync" "time" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/s3" s3setlock "github.com/mashiike/s3-setlock" ) func main() { ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) defer cancel() bucketName, client, err := newS3Client(ctx) if err != nil { log.Fatal(err) } locker, err := s3setlock.New(fmt.Sprintf("s3://%s/myobject.lock", bucketName), s3setlock.WithContext(ctx), s3setlock.WithClient(client), s3setlock.WithDelay(true), s3setlock.WithLeaseDuration(60*time.Second), ) if err != nil { log.Fatal(err) } var wg sync.WaitGroup counter := 0 for i := 0; i < 10; i++ { wg.Add(1) go func() { defer wg.Done() for i := 0; i < 10; i++ { locker.Lock() counter++ locker.Unlock() } }() } wg.Wait() fmt.Println(counter) } func newS3Client(ctx context.Context) (string, s3setlock.S3Client, error) { bucketName, ok := os.LookupEnv("TEST_S3_SETLOCK_BUCKET_NAME") if !ok || bucketName == "" { return "s3-setlock-test", &mockClient{}, nil } awsCfg, err := config.LoadDefaultConfig(ctx) if err != nil { return "", nil, err } return bucketName, s3.NewFromConfig(awsCfg), nil }
Output: 100
Index ¶
- Variables
- func AsBailout(result interface{}, target *error) bool
- func HandleBailout(fn func() error) (err error)
- func WithClient(client S3Client) func(opts *Options)
- func WithContext(ctx context.Context) func(opts *Options)
- func WithDelay(delay bool) func(opts *Options)
- func WithLeaseDuration(d time.Duration) func(opts *Options)
- func WithLogger(logger *slog.Logger) func(opts *Options)
- func WithNoBailout() func(opts *Options)
- func WithRegion(region string) func(opts *Options)
- type Locker
- type Options
- type S3Client
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var (
DefaultLeaseDuration = 60 * time.Second
)
Default values
var Version = "0.2.0"
Functions ¶
func AsBailout ¶ added in v0.2.0
AsBailout returns bailout errors example:
defer func() { var err error if s3setlock.AsBailout(recover(),&err) { log.Fatal(err) } }()
func HandleBailout ¶ added in v0.2.0
HandleBailout executes the provided function and recovers from any bailout errors, returning the bailout error if one occurs.
func WithClient ¶
WithClient specifies the AWS SDK Client. If not specified, the default client is created.
func WithContext ¶
WithContext specifies the Context used by Lock() and Unlock().
func WithDelay ¶
WithDelay will delay the acquisition of the lock if it fails to acquire the lock. This is similar to the N option of setlock. The default is delay enalbed(true). Specify false if you want to exit immediately if Lock acquisition fails.
func WithLeaseDuration ¶
WithLeaseDuration affects the heartbeat interval and TTL after Lock acquisition. The default is 10 seconds
func WithLogger ¶
WithLogger is a setting to enable the log output of S3 Locker. By default, Logger that does not output anywhere is specified.
func WithNoBailout ¶ added in v0.2.0
func WithNoBailout() func(opts *Options)
WithNoBailout changes the behavior so that it does not panic if an error occurs in the Lock () and Unlock () functions. Check the LastErr () function to see if an error has occurred when WithNoBailout is specified.
func WithRegion ¶
WithRegion specifies the AWS Region. Default AWS_DEFAULT_REGION env
Types ¶
type Locker ¶
type Locker struct { NoBailout bool // contains filtered or unexported fields }
Locker is a locker that uses S3 as a lock storage
func (*Locker) LockGranted ¶
type S3Client ¶
type S3Client interface { PutObject(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error) HeadObject(ctx context.Context, params *s3.HeadObjectInput, optFns ...func(*s3.Options)) (*s3.HeadObjectOutput, error) DeleteObject(ctx context.Context, params *s3.DeleteObjectInput, optFns ...func(*s3.Options)) (*s3.DeleteObjectOutput, error) }