Skip to content

Commit 51a27db

Browse files
committed
return a sub-context of lock context directly, for value inheritance
1 parent 8e5861e commit 51a27db

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

lock/example/main.go

+22-9
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import (
77

88
"github.com/coreos/etcd/clientv3"
99
"github.com/leopoldxx/go-utils/lock"
10+
"github.com/leopoldxx/go-utils/trace"
1011
)
1112

1213
func newClientv3() *clientv3.Client {
1314
cli, err := clientv3.New(clientv3.Config{
14-
Endpoints: []string{"http://127.0.0.1:2389"},
15+
Endpoints: []string{"http://10.0.2.15:2389"},
1516
DialTimeout: 5 * time.Second,
1617
})
1718
if err != nil {
@@ -22,24 +23,36 @@ func newClientv3() *clientv3.Client {
2223

2324
func main() {
2425
locker := lock.New(newClientv3())
26+
ctx := trace.WithTraceForContext(context.TODO(), "test-main")
27+
tracer := trace.GetTraceFromContext(ctx)
28+
tracer.Info("begin test")
2529

26-
unlock, ctx, err := locker.Trylock(context.TODO(), "/lock", lock.WithTTL(time.Second*10))
30+
unlock, ctx2, err := locker.Trylock(ctx, "/lock", lock.WithTTL(time.Second*10))
2731
if err != nil {
28-
log.Printf("lock failed: %s", err)
32+
tracer.Warnf("lock failed: %s", err)
33+
return
2934
}
30-
log.Println("safe")
3135

3236
select {
3337
case <-ctx.Done():
34-
case <-time.After(time.Minute):
38+
case <-time.After(10 * time.Second):
3539
}
36-
log.Println("safe")
40+
go func(ctx context.Context) {
41+
tracer := trace.GetTraceFromContext(ctx)
42+
tracer.Info("test context")
43+
}(ctx2)
44+
45+
tracer.Info("safe")
3746
unlock()
3847

39-
unlock, ctx, err = locker.Trylock(context.TODO(), "/lock", lock.WithTTL(time.Minute*10))
48+
unlock, ctx3, err := locker.Trylock(ctx2, "/lock", lock.WithTTL(time.Minute*10))
4049
if err != nil {
41-
log.Printf("lock failed: %s", err)
50+
tracer.Infof("lock failed: %s", err)
51+
return
4252
}
43-
log.Println("safe")
53+
go func(ctx context.Context) {
54+
tracer := trace.GetTraceFromContext(ctx)
55+
tracer.Info("test context")
56+
}(ctx3)
4457
unlock()
4558
}

lock/lock.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (l *locker) Trylock(ctx context.Context, key string, ops ...Options) (Unloc
8484
s, err := concurrency.NewSession(l.etcdCli, concurrency.WithContext(cancelCtx), concurrency.WithTTL(1 /* 1s */))
8585
if err != nil {
8686
cancelFunc()
87-
tmpCh <- result{nil, nil, err}
87+
tmpCh <- result{func() {}, nil, err}
8888
return
8989
}
9090

@@ -116,11 +116,19 @@ func (l *locker) Trylock(ctx context.Context, key string, ops ...Options) (Unloc
116116
s.Close()
117117
}()
118118

119+
newCtx, newCancel := context.WithCancel(ctx)
120+
go func(ctx context.Context, cancel context.CancelFunc) {
121+
select {
122+
case <-ctx.Done():
123+
}
124+
cancel()
125+
}(cancelCtx, newCancel)
126+
119127
tmpCh <- result{
120128
func() {
121129
cancelFunc()
122130
},
123-
cancelCtx,
131+
newCtx,
124132
nil,
125133
}
126134
}()

0 commit comments

Comments
 (0)