github.com/avence12/go-ethereum@v1.5.10-0.20170320123548-1dfd65f6d047/mobile/context.go (about) 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 17 // Contains all the wrappers from the golang.org/x/net/context package to support 18 // client side context management on mobile platforms. 19 20 package geth 21 22 import ( 23 "time" 24 25 "golang.org/x/net/context" 26 ) 27 28 // Context carries a deadline, a cancelation signal, and other values across API 29 // boundaries. 30 type Context struct { 31 context context.Context 32 cancel context.CancelFunc 33 } 34 35 // NewContext returns a non-nil, empty Context. It is never canceled, has no 36 // values, and has no deadline. It is typically used by the main function, 37 // initialization, and tests, and as the top-level Context for incoming requests. 38 func NewContext() *Context { 39 return &Context{ 40 context: context.Background(), 41 } 42 } 43 44 // WithCancel returns a copy of the original context with cancellation mechanism 45 // included. 46 // 47 // Canceling this context releases resources associated with it, so code should 48 // call cancel as soon as the operations running in this Context complete. 49 func (c *Context) WithCancel() *Context { 50 child, cancel := context.WithCancel(c.context) 51 return &Context{ 52 context: child, 53 cancel: cancel, 54 } 55 } 56 57 // WithDeadline returns a copy of the original context with the deadline adjusted 58 // to be no later than the specified time. 59 // 60 // Canceling this context releases resources associated with it, so code should 61 // call cancel as soon as the operations running in this Context complete. 62 func (c *Context) WithDeadline(sec int64, nsec int64) *Context { 63 child, cancel := context.WithDeadline(c.context, time.Unix(sec, nsec)) 64 return &Context{ 65 context: child, 66 cancel: cancel, 67 } 68 } 69 70 // WithTimeout returns a copy of the original context with the deadline adjusted 71 // to be no later than now + the duration specified. 72 // 73 // Canceling this context releases resources associated with it, so code should 74 // call cancel as soon as the operations running in this Context complete. 75 func (c *Context) WithTimeout(nsec int64) *Context { 76 child, cancel := context.WithTimeout(c.context, time.Duration(nsec)) 77 return &Context{ 78 context: child, 79 cancel: cancel, 80 } 81 }