context.go 2.5 KB

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