本站源代码
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.

73 lines
2.8KB

  1. // Copyright 2016 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.7
  5. package context
  6. import (
  7. "context" // standard library's context, as of Go 1.7
  8. "time"
  9. )
  10. var (
  11. todo = context.TODO()
  12. background = context.Background()
  13. )
  14. // Canceled is the error returned by Context.Err when the context is canceled.
  15. var Canceled = context.Canceled
  16. // DeadlineExceeded is the error returned by Context.Err when the context's
  17. // deadline passes.
  18. var DeadlineExceeded = context.DeadlineExceeded
  19. // WithCancel returns a copy of parent with a new Done channel. The returned
  20. // context's Done channel is closed when the returned cancel function is called
  21. // or when the parent context's Done channel is closed, whichever happens first.
  22. //
  23. // Canceling this context releases resources associated with it, so code should
  24. // call cancel as soon as the operations running in this Context complete.
  25. func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {
  26. ctx, f := context.WithCancel(parent)
  27. return ctx, CancelFunc(f)
  28. }
  29. // WithDeadline returns a copy of the parent context with the deadline adjusted
  30. // to be no later than d. If the parent's deadline is already earlier than d,
  31. // WithDeadline(parent, d) is semantically equivalent to parent. The returned
  32. // context's Done channel is closed when the deadline expires, when the returned
  33. // cancel function is called, or when the parent context's Done channel is
  34. // closed, whichever happens first.
  35. //
  36. // Canceling this context releases resources associated with it, so code should
  37. // call cancel as soon as the operations running in this Context complete.
  38. func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc) {
  39. ctx, f := context.WithDeadline(parent, deadline)
  40. return ctx, CancelFunc(f)
  41. }
  42. // WithTimeout returns WithDeadline(parent, time.Now().Add(timeout)).
  43. //
  44. // Canceling this context releases resources associated with it, so code should
  45. // call cancel as soon as the operations running in this Context complete:
  46. //
  47. // func slowOperationWithTimeout(ctx context.Context) (Result, error) {
  48. // ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
  49. // defer cancel() // releases resources if slowOperation completes before timeout elapses
  50. // return slowOperation(ctx)
  51. // }
  52. func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) {
  53. return WithDeadline(parent, time.Now().Add(timeout))
  54. }
  55. // WithValue returns a copy of parent in which the value associated with key is
  56. // val.
  57. //
  58. // Use context Values only for request-scoped data that transits processes and
  59. // APIs, not for passing optional parameters to functions.
  60. func WithValue(parent Context, key interface{}, val interface{}) Context {
  61. return context.WithValue(parent, key, val)
  62. }
上海开阖软件有限公司 沪ICP备12045867号-1