-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.go
47 lines (43 loc) · 1.03 KB
/
example.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
package main
import (
"fmt"
"github.com/FelixSeptem/mortise"
"github.com/gomodule/redigo/redis"
"time"
)
func accessResource(key string, ftoken int64, mutex *mortise.MutexManager) error {
if _, err := mutex.CheckCurrentFencingToken(key, ftoken); err != nil {
return err
}
// mock resource operate
func() {
time.Sleep(time.Millisecond * 100)
}()
return nil
}
func job(mutex *mortise.MutexManager, resourceKey string) error {
token, err := mutex.Lock(resourceKey, time.Millisecond*300)
if err != nil {
return err
}
if err := accessResource(resourceKey, token, mutex); err != nil {
return err
}
return nil
}
func main() {
conn, err := redis.Dial("tcp", "localhost:6379", redis.DialConnectTimeout(time.Millisecond*100), redis.DialDatabase(0))
if err != nil {
fmt.Println(err.Error())
}
mutexMgr := mortise.MutexManager{
Conn: conn,
Name: "SyncJob",
}
resourceKey := fmt.Sprintf("Sync:%d", time.Now().UnixNano())
for i := 0; i < 100; i++ {
if err := job(&mutexMgr, resourceKey); err != nil {
fmt.Println(err)
}
}
}