From 4c9a70fe1c7e7596eebaf475cf78596eee614c7e Mon Sep 17 00:00:00 2001 From: Will Hawkins Date: Mon, 13 Dec 2021 21:44:50 -0500 Subject: Intermediate check-in commit. Adding functionality and refactoring. --- timeoutat/timeoutat.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 timeoutat/timeoutat.go (limited to 'timeoutat/timeoutat.go') 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()) + }() +} -- cgit v1.2.3