package main import ( "strconv" "sync" "time" "git.wecise.com/wecise/odbserver/odb" "gitee.com/wecisecode/util/logger" ) // CGO_ENABLED=1 go run --ldflags '-linkmode external -extldflags "-static"' testlocker.go // #cgo LDFLAGS: -L/opt/odbserver/sqlite -lsqlite -ldl // #include "/opt/odbserver/sqlite/sqlite.h" // #include // #include // extern int64_t uhaha_seed; // extern int64_t uhaha_ts; // void uhaha_begin_reader(); // void uhaha_end_reader(); func main() { g, err := odb.New(&odb.Option{Cache: odb.CacheAll, Keyspace: "matrix", DisableInitialHostLookup: true}) // For aliyun //g, err := odb.New(&odb.Option{Cache:odb.CacheAll}) if err != nil { logger.Fatal(err) } locker := odb.NewLocker(g, 5) if err := locker.Lock(str); err == nil { defer locker.UnLock() time.Sleep(time.Duration(200) * time.Millisecond) logger.Infof("lock %s ok .", str) } else { logger.Error("lock err .", err) } var wg sync.WaitGroup for i := 0; i < 1000; i++ { wg.Add(1) go func(str string) { defer wg.Done() locker := odb.NewLocker(g, 5) if err := locker.Lock(str); err == nil { defer locker.UnLock() time.Sleep(time.Duration(200) * time.Millisecond) logger.Infof("lock %s ok .", str) } else { logger.Error("lock err .", err) } }(strconv.Itoa(i)) } for i := 0; i < 1000; i++ { wg.Add(1) go func(str string) { defer wg.Done() locker := odb.NewLocker(g, 5) if err := locker.Lock(str); err == nil { defer locker.UnLock() time.Sleep(time.Duration(200) * time.Millisecond) logger.Infof("lock %s ok .", str) } else { logger.Error("lock err .", err) } }(strconv.Itoa(i)) } wg.Wait() } /* [ { "graph": { "nodes": [ { "_icon": "linux", "class": "/matrix/entity/linux", "id": "linux:h2" }, { "_icon": "cassandra", "class": "/matrix/entity/cassandra", "id": "cassandra:c1" }, { "_icon": "biz", "class": "/matrix/entity/biz", "id": "biz:b1" }, { "_icon": "linux", "class": "/matrix/entity/linux", "id": "linux:h1" } ], "edges": [ { "class": "contain", "id": "biz:b1-linux:h1", "source": "biz:b1", "target": "linux:h1" }, { "class": "contain", "id": "biz:b1-linux:h2", "source": "biz:b1", "target": "linux:h2" }, { "class": "contain", "id": "linux:h1-cassandra:c1", "source": "linux:h1", "target": "cassandra:c1" } ], "paths": null, "pathtags": null } } ] */