-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
53 lines (45 loc) · 1.15 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
50
51
52
53
package main
import (
"context"
"flag"
"log"
"os"
"os/signal"
"syscall"
"cloud.google.com/go/spanner"
"github.com/flowerinthenight/spindle/v2"
)
func main() {
dbstr := flag.String("db", "", "db, fmt: projects/{v}/instances/{v}/databases/{v}")
table := flag.String("table", "testlease", "table name")
name := flag.String("name", "mylock", "lock name")
flag.Parse()
// To run, update the database name, table name, and, optionally, the lock name.
// It is assumed that your environment is able to authenticate to Spanner via
// GOOGLE_APPLICATION_CREDENTIALS environment variable.
ctx := context.Background()
db, err := spanner.NewClient(ctx, *dbstr)
if err != nil {
log.Println(err)
return
}
defer db.Close()
quit, cancel := context.WithCancel(ctx)
lock := spindle.New(db,
*table,
*name,
spindle.WithDuration(10000),
spindle.WithLeaderCallback(nil, func(d interface{}, m []byte) {
log.Println("callback:", string(m))
}),
)
done := make(chan error, 1)
lock.Run(quit, done) // start main loop
go func() {
sigch := make(chan os.Signal, 1)
signal.Notify(sigch, syscall.SIGINT, syscall.SIGTERM)
<-sigch
cancel()
}()
<-done
}