]>
Commit | Line | Data |
---|---|---|
bae9f6d2 JC |
1 | package aws |
2 | ||
3 | import ( | |
4 | "time" | |
5 | ) | |
6 | ||
7 | // Context is an copy of the Go v1.7 stdlib's context.Context interface. | |
8 | // It is represented as a SDK interface to enable you to use the "WithContext" | |
9 | // API methods with Go v1.6 and a Context type such as golang.org/x/net/context. | |
10 | // | |
11 | // See https://golang.org/pkg/context on how to use contexts. | |
12 | type Context interface { | |
13 | // Deadline returns the time when work done on behalf of this context | |
14 | // should be canceled. Deadline returns ok==false when no deadline is | |
15 | // set. Successive calls to Deadline return the same results. | |
16 | Deadline() (deadline time.Time, ok bool) | |
17 | ||
18 | // Done returns a channel that's closed when work done on behalf of this | |
19 | // context should be canceled. Done may return nil if this context can | |
20 | // never be canceled. Successive calls to Done return the same value. | |
21 | Done() <-chan struct{} | |
22 | ||
23 | // Err returns a non-nil error value after Done is closed. Err returns | |
24 | // Canceled if the context was canceled or DeadlineExceeded if the | |
25 | // context's deadline passed. No other values for Err are defined. | |
26 | // After Done is closed, successive calls to Err return the same value. | |
27 | Err() error | |
28 | ||
29 | // Value returns the value associated with this context for key, or nil | |
30 | // if no value is associated with key. Successive calls to Value with | |
31 | // the same key returns the same result. | |
32 | // | |
33 | // Use context values only for request-scoped data that transits | |
34 | // processes and API boundaries, not for passing optional parameters to | |
35 | // functions. | |
36 | Value(key interface{}) interface{} | |
37 | } | |
38 | ||
39 | // BackgroundContext returns a context that will never be canceled, has no | |
40 | // values, and no deadline. This context is used by the SDK to provide | |
41 | // backwards compatibility with non-context API operations and functionality. | |
42 | // | |
43 | // Go 1.6 and before: | |
44 | // This context function is equivalent to context.Background in the Go stdlib. | |
45 | // | |
46 | // Go 1.7 and later: | |
47 | // The context returned will be the value returned by context.Background() | |
48 | // | |
49 | // See https://golang.org/pkg/context for more information on Contexts. | |
50 | func BackgroundContext() Context { | |
51 | return backgroundCtx | |
52 | } | |
53 | ||
54 | // SleepWithContext will wait for the timer duration to expire, or the context | |
55 | // is canceled. Which ever happens first. If the context is canceled the Context's | |
56 | // error will be returned. | |
57 | // | |
58 | // Expects Context to always return a non-nil error if the Done channel is closed. | |
59 | func SleepWithContext(ctx Context, dur time.Duration) error { | |
60 | t := time.NewTimer(dur) | |
61 | defer t.Stop() | |
62 | ||
63 | select { | |
64 | case <-t.C: | |
65 | break | |
66 | case <-ctx.Done(): | |
67 | return ctx.Err() | |
68 | } | |
69 | ||
70 | return nil | |
71 | } |