Skip to content

Commit 9047fad

Browse files
committed
Minor optimization on context manager
1 parent 29c7bf2 commit 9047fad

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

internal/ctx_manager.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ func (cm *CtxManager) Ctx() context.Context {
2020
func (cm *CtxManager) Cancel() bool {
2121
cm.rwMu.Lock()
2222
defer cm.rwMu.Unlock()
23-
cancelled := cm.c.Err() == nil
23+
if cm.c.Err() != nil {
24+
return false
25+
}
2426
cm.cl()
25-
return cancelled
27+
return true
2628
}
2729

2830
func (cm *CtxManager) IsDead() bool {

internal/ctx_manager_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ func TestCtxManager(t *testing.T) {
2020

2121
assert.NotNil(t, cm.Ctx().Err())
2222
assert.True(t, cm.IsDead())
23+
24+
assert.False(t, cm.Cancel())
2325
},
2426
},
2527
}

0 commit comments

Comments
 (0)