mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-22 01:57:12 -05:00
move ctx into function
This commit is contained in:
parent
b0b65cb3fa
commit
9caa59c827
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user