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