You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
4.0 KiB

  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build !go1.9
  5. package context
  6. import "time"
  7. // A Context carries a deadline, a cancelation signal, and other values across
  8. // API boundaries.
  9. //
  10. // Context's methods may be called by multiple goroutines simultaneously.
  11. type Context interface {
  12. // Deadline returns the time when work done on behalf of this context
  13. // should be canceled. Deadline returns ok==false when no deadline is
  14. // set. Successive calls to Deadline return the same results.
  15. Deadline() (deadline time.Time, ok bool)
  16. // Done returns a channel that's closed when work done on behalf of this
  17. // context should be canceled. Done may return nil if this context can
  18. // never be canceled. Successive calls to Done return the same value.
  19. //
  20. // WithCancel arranges for Done to be closed when cancel is called;
  21. // WithDeadline arranges for Done to be closed when the deadline
  22. // expires; WithTimeout arranges for Done to be closed when the timeout
  23. // elapses.
  24. //
  25. // Done is provided for use in select statements:
  26. //
  27. // // Stream generates values with DoSomething and sends them to out
  28. // // until DoSomething returns an error or ctx.Done is closed.
  29. // func Stream(ctx context.Context, out chan<- Value) error {
  30. // for {
  31. // v, err := DoSomething(ctx)
  32. // if err != nil {
  33. // return err
  34. // }
  35. // select {
  36. // case <-ctx.Done():
  37. // return ctx.Err()
  38. // case out <- v:
  39. // }
  40. // }
  41. // }
  42. //
  43. // See http://blog.golang.org/pipelines for more examples of how to use
  44. // a Done channel for cancelation.
  45. Done() <-chan struct{}
  46. // Err returns a non-nil error value after Done is closed. Err returns
  47. // Canceled if the context was canceled or DeadlineExceeded if the
  48. // context's deadline passed. No other values for Err are defined.
  49. // After Done is closed, successive calls to Err return the same value.
  50. Err() error
  51. // Value returns the value associated with this context for key, or nil
  52. // if no value is associated with key. Successive calls to Value with
  53. // the same key returns the same result.
  54. //
  55. // Use context values only for request-scoped data that transits
  56. // processes and API boundaries, not for passing optional parameters to
  57. // functions.
  58. //
  59. // A key identifies a specific value in a Context. Functions that wish
  60. // to store values in Context typically allocate a key in a global
  61. // variable then use that key as the argument to context.WithValue and
  62. // Context.Value. A key can be any type that supports equality;
  63. // packages should define keys as an unexported type to avoid
  64. // collisions.
  65. //
  66. // Packages that define a Context key should provide type-safe accessors
  67. // for the values stores using that key:
  68. //
  69. // // Package user defines a User type that's stored in Contexts.
  70. // package user
  71. //
  72. // import "golang.org/x/net/context"
  73. //
  74. // // User is the type of value stored in the Contexts.
  75. // type User struct {...}
  76. //
  77. // // key is an unexported type for keys defined in this package.
  78. // // This prevents collisions with keys defined in other packages.
  79. // type key int
  80. //
  81. // // userKey is the key for user.User values in Contexts. It is
  82. // // unexported; clients use user.NewContext and user.FromContext
  83. // // instead of using this key directly.
  84. // var userKey key = 0
  85. //
  86. // // NewContext returns a new Context that carries value u.
  87. // func NewContext(ctx context.Context, u *User) context.Context {
  88. // return context.WithValue(ctx, userKey, u)
  89. // }
  90. //
  91. // // FromContext returns the User value stored in ctx, if any.
  92. // func FromContext(ctx context.Context) (*User, bool) {
  93. // u, ok := ctx.Value(userKey).(*User)
  94. // return u, ok
  95. // }
  96. Value(key interface{}) interface{}
  97. }
  98. // A CancelFunc tells an operation to abandon its work.
  99. // A CancelFunc does not wait for the work to stop.
  100. // After the first call, subsequent calls to a CancelFunc do nothing.
  101. type CancelFunc func()