mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-12-27 20:45:28 -05:00
no-op timer
This commit is contained in:
@@ -5,21 +5,25 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type ActivityTimer struct {
|
||||
type ActivityTimer interface {
|
||||
Update()
|
||||
}
|
||||
|
||||
type realActivityTimer struct {
|
||||
updated chan bool
|
||||
timeout time.Duration
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
func (t *ActivityTimer) Update() {
|
||||
func (t *realActivityTimer) Update() {
|
||||
select {
|
||||
case t.updated <- true:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func (t *ActivityTimer) run() {
|
||||
func (t *realActivityTimer) run() {
|
||||
for {
|
||||
select {
|
||||
case <-time.After(t.timeout):
|
||||
@@ -37,9 +41,9 @@ func (t *ActivityTimer) run() {
|
||||
}
|
||||
}
|
||||
|
||||
func CancelAfterInactivity(ctx context.Context, timeout time.Duration) (context.Context, *ActivityTimer) {
|
||||
func CancelAfterInactivity(ctx context.Context, timeout time.Duration) (context.Context, ActivityTimer) {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
timer := &ActivityTimer{
|
||||
timer := &realActivityTimer{
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
timeout: timeout,
|
||||
@@ -48,3 +52,11 @@ func CancelAfterInactivity(ctx context.Context, timeout time.Duration) (context.
|
||||
go timer.run()
|
||||
return ctx, timer
|
||||
}
|
||||
|
||||
type noOpActivityTimer struct{}
|
||||
|
||||
func (noOpActivityTimer) Update() {}
|
||||
|
||||
func BackgroundTimer() ActivityTimer {
|
||||
return noOpActivityTimer{}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user