diff options
Diffstat (limited to 'timeoutat/timeoutat.go')
| -rw-r--r-- | timeoutat/timeoutat.go | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/timeoutat/timeoutat.go b/timeoutat/timeoutat.go new file mode 100644 index 0000000..e77804e --- /dev/null +++ b/timeoutat/timeoutat.go @@ -0,0 +1,30 @@ +package timeoutat + +import ( + "context" + "fmt" + "time" +) + +type TimeoutAt struct { + when time.Time + response chan interface{} +} + +func NewTimeoutAt(ctx context.Context, when time.Time, response chan interface{}) *TimeoutAt { + timeoutAt := &TimeoutAt{when: when, response: response} + timeoutAt.start(ctx) + return timeoutAt +} + +func (ta *TimeoutAt) start(ctx context.Context) { + go func() { + fmt.Printf("Timeout expected to end at %v\n", ta.when) + select { + case <-time.After(ta.when.Sub(time.Now())): + case <-ctx.Done(): + } + ta.response <- struct{}{} + fmt.Printf("Timeout ended at %v\n", time.Now()) + }() +} |
