mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-02-20 23:47:21 -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 {
|
type ActivityTimer struct {
|
||||||
updated chan bool
|
updated chan bool
|
||||||
timeout chan time.Duration
|
timeout chan time.Duration
|
||||||
ctx context.Context
|
|
||||||
cancel context.CancelFunc
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *ActivityTimer) Update() {
|
func (t *ActivityTimer) Update() {
|
||||||
@ -27,7 +25,7 @@ func (t *ActivityTimer) SetTimeout(timeout time.Duration) {
|
|||||||
t.timeout <- timeout
|
t.timeout <- timeout
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *ActivityTimer) run() {
|
func (t *ActivityTimer) run(ctx context.Context, cancel context.CancelFunc) {
|
||||||
ticker := time.NewTicker(<-t.timeout)
|
ticker := time.NewTicker(<-t.timeout)
|
||||||
defer func() {
|
defer func() {
|
||||||
ticker.Stop()
|
ticker.Stop()
|
||||||
@ -36,23 +34,24 @@ func (t *ActivityTimer) run() {
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
case <-t.ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
case timeout := <-t.timeout:
|
case timeout := <-t.timeout:
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
t.cancel()
|
cancel()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ticker.Stop()
|
ticker.Stop()
|
||||||
ticker = time.NewTicker(timeout)
|
ticker = time.NewTicker(timeout)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-t.updated:
|
case <-t.updated:
|
||||||
// Updated keep waiting.
|
// Updated keep waiting.
|
||||||
default:
|
default:
|
||||||
t.cancel()
|
cancel()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -60,12 +59,10 @@ func (t *ActivityTimer) run() {
|
|||||||
|
|
||||||
func CancelAfterInactivity(ctx context.Context, cancel context.CancelFunc, timeout time.Duration) *ActivityTimer {
|
func CancelAfterInactivity(ctx context.Context, cancel context.CancelFunc, timeout time.Duration) *ActivityTimer {
|
||||||
timer := &ActivityTimer{
|
timer := &ActivityTimer{
|
||||||
ctx: ctx,
|
|
||||||
cancel: cancel,
|
|
||||||
timeout: make(chan time.Duration, 1),
|
timeout: make(chan time.Duration, 1),
|
||||||
updated: make(chan bool, 1),
|
updated: make(chan bool, 1),
|
||||||
}
|
}
|
||||||
timer.timeout <- timeout
|
timer.timeout <- timeout
|
||||||
go timer.run()
|
go timer.run(ctx, cancel)
|
||||||
return timer
|
return timer
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user