-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
49 lines (42 loc) · 1.17 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"context"
"fmt"
"sync"
"time"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/skyvell/locksv2/s3lock"
)
const bucketName = "versioningbucketcrossbreed"
const key = "key-201"
func main() {
ctx := context.Background()
config, err := config.LoadDefaultConfig(ctx)
if err != nil {
panic("Could not load config.")
}
// Construct 10 lock instances that will compete for the lock.
competingLocks := []*s3lock.S3Lock{}
for i := 0; i < 10; i++ {
competingLocks = append(competingLocks, s3lock.NewS3Lock(config, fmt.Sprintf("Lock%v", i), bucketName, key, 50*time.Second))
}
// Use WaitGroup to wait for all Goroutines to complete.
var wg sync.WaitGroup
for _, lock := range competingLocks {
wg.Add(1)
go func(l *s3lock.S3Lock) {
// Decrement counter when Goroutine completes.
defer wg.Done()
err := l.AcquireLockWithRetry(ctx, time.Second*60)
defer l.RemoveLockIfOwner(ctx)
if err == nil {
// Execute code here when lock is acquired.
fmt.Printf("Lock: %s aquired lock. Executing:\n", l.LockName)
fmt.Printf("Sleeping for 1 second. \n\n")
time.Sleep(time.Second * 1)
l.ReleaseLock(ctx)
}
}(lock)
}
wg.Wait()
}