context.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // Contains all the wrappers from the golang.org/x/net/context package to support
  17. // client side context management on mobile platforms.
  18. package geth
  19. import (
  20. "context"
  21. "time"
  22. )
  23. // Context carries a deadline, a cancelation signal, and other values across API
  24. // boundaries.
  25. type Context struct {
  26. context context.Context
  27. cancel context.CancelFunc
  28. }
  29. // NewContext returns a non-nil, empty Context. It is never canceled, has no
  30. // values, and has no deadline. It is typically used by the main function,
  31. // initialization, and tests, and as the top-level Context for incoming requests.
  32. func NewContext() *Context {
  33. return &Context{
  34. context: context.Background(),
  35. }
  36. }
  37. // WithCancel returns a copy of the original context with cancellation mechanism
  38. // included.
  39. //
  40. // Canceling this context releases resources associated with it, so code should
  41. // call cancel as soon as the operations running in this Context complete.
  42. func (c *Context) WithCancel() *Context {
  43. child, cancel := context.WithCancel(c.context)
  44. return &Context{
  45. context: child,
  46. cancel: cancel,
  47. }
  48. }
  49. // WithDeadline returns a copy of the original context with the deadline adjusted
  50. // to be no later than the specified time.
  51. //
  52. // Canceling this context releases resources associated with it, so code should
  53. // call cancel as soon as the operations running in this Context complete.
  54. func (c *Context) WithDeadline(sec int64, nsec int64) *Context {
  55. child, cancel := context.WithDeadline(c.context, time.Unix(sec, nsec))
  56. return &Context{
  57. context: child,
  58. cancel: cancel,
  59. }
  60. }
  61. // WithTimeout returns a copy of the original context with the deadline adjusted
  62. // to be no later than now + the duration specified.
  63. //
  64. // Canceling this context releases resources associated with it, so code should
  65. // call cancel as soon as the operations running in this Context complete.
  66. func (c *Context) WithTimeout(nsec int64) *Context {
  67. child, cancel := context.WithTimeout(c.context, time.Duration(nsec))
  68. return &Context{
  69. context: child,
  70. cancel: cancel,
  71. }
  72. }