mirror of
https://github.com/v2fly/v2ray-core.git
synced 2026-04-21 04:59:10 -04:00
no-op timer
This commit is contained in:
@@ -42,7 +42,7 @@ func ReadFullFrom(reader io.Reader, size int) Supplier {
|
||||
|
||||
// Pipe dumps all payload from reader to writer, until an error occurs.
|
||||
// ActivityTimer gets updated as soon as there is a payload.
|
||||
func Pipe(timer *signal.ActivityTimer, reader Reader, writer Writer) error {
|
||||
func Pipe(timer signal.ActivityTimer, reader Reader, writer Writer) error {
|
||||
for {
|
||||
buffer, err := reader.Read()
|
||||
if err != nil {
|
||||
@@ -65,7 +65,7 @@ func Pipe(timer *signal.ActivityTimer, reader Reader, writer Writer) error {
|
||||
}
|
||||
|
||||
// PipeUntilEOF behaves the same as Pipe(). The only difference is PipeUntilEOF returns nil on EOF.
|
||||
func PipeUntilEOF(timer *signal.ActivityTimer, reader Reader, writer Writer) error {
|
||||
func PipeUntilEOF(timer signal.ActivityTimer, reader Reader, writer Writer) error {
|
||||
err := Pipe(timer, reader, writer)
|
||||
if err != nil && errors.Cause(err) != io.EOF {
|
||||
return err
|
||||
|
||||
@@ -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