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

go17.go 2.9 kB

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