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