summaryrefslogtreecommitdiff
path: root/timeoutat/timeoutat.go
blob: e77804e61b78d2fe01a31d70876a12858aa795d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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())
	}()
}