瀏覽代碼

move ctx into function

Darien Raymond 8 年之前
父節點
當前提交
9caa59c827
共有 1 個文件被更改,包括 6 次插入9 次删除
  1. 6 9
      common/signal/timer.go

+ 6 - 9
common/signal/timer.go

@@ -12,8 +12,6 @@ type ActivityUpdater interface {
 type ActivityTimer struct {
 	updated chan bool
 	timeout chan time.Duration
-	ctx     context.Context
-	cancel  context.CancelFunc
 }
 
 func (t *ActivityTimer) Update() {
@@ -27,7 +25,7 @@ func (t *ActivityTimer) SetTimeout(timeout time.Duration) {
 	t.timeout <- timeout
 }
 
-func (t *ActivityTimer) run() {
+func (t *ActivityTimer) run(ctx context.Context, cancel context.CancelFunc) {
 	ticker := time.NewTicker(<-t.timeout)
 	defer func() {
 		ticker.Stop()
@@ -36,23 +34,24 @@ func (t *ActivityTimer) run() {
 	for {
 		select {
 		case <-ticker.C:
-		case <-t.ctx.Done():
+		case <-ctx.Done():
 			return
 		case timeout := <-t.timeout:
 			if timeout == 0 {
-				t.cancel()
+				cancel()
 				return
 			}
 
 			ticker.Stop()
 			ticker = time.NewTicker(timeout)
+			continue
 		}
 
 		select {
 		case <-t.updated:
 		// Updated keep waiting.
 		default:
-			t.cancel()
+			cancel()
 			return
 		}
 	}
@@ -60,12 +59,10 @@ func (t *ActivityTimer) run() {
 
 func CancelAfterInactivity(ctx context.Context, cancel context.CancelFunc, timeout time.Duration) *ActivityTimer {
 	timer := &ActivityTimer{
-		ctx:     ctx,
-		cancel:  cancel,
 		timeout: make(chan time.Duration, 1),
 		updated: make(chan bool, 1),
 	}
 	timer.timeout <- timeout
-	go timer.run()
+	go timer.run(ctx, cancel)
 	return timer
 }