For example, the context is created with timeout to be 10 seconds later. After a while (e.g. 2 seconds later), I want to refresh it to be 10 seconds later from this time.
What can I do?
To set a deadline on a context, use the context. WithDeadline function and provide it the parent context and a time. Time value for when the context should be canceled. You'll then receive a new context and a function to cancel the new context as return values.
WithTimeout. The context package as the standard library was moved from the golang.org/x/net/context package in Go 1.7. This allows the use of contexts for cancellation, timeouts, and passing request-scoped data in other library packages. context. WithTimeout can be used in a timeout implementation.
The error 'context deadline exceeded' means that we ran into a situation where a given action was not completed in an expected timeframe. Usually, issue occurs if Pods become stuck in Init status.
context is a standard package of Golang that makes it easy to pass request-scoped values, cancelation signals, and deadlines across API boundaries to all the goroutines involved in handling a request.
We can save resources by cancel further processes when timeout happened. We can use context to apply a timeout to a function call in Go. To use context timeout for a function call we need to do these steps: create a channel to flag if the function is completed send a flag to the channel after the function is completed
If you take this approach there are a few of things to be aware of: The timeout starts from the moment the context is created, so any code running in your handlers before the database query counts towards the timeout. If you have multiple queries being executed in a handler, then they all have to complete within that one time.
Use the context.WithTimeout () function to create a context.Context instance with a 5-second timeout duration. Execute the SQL query using the ExecContext () method, passing the context.Context instance as a parameter.
In your code, the timeout will always be reached and not cancelled, that is why you receive DeadlineExceeeded. Your code is correct except the select part which will block until either 10 seconds pass or context timeout is reached. In your case always the context timeout is reached.
context.Context
is not designed that way. context.Context
is delegated down to workers, and if a worker finds that more time should be allowed, it can't override the "master's call".
If you have a situation where an initial 10 seconds timeout is to be used, but this 10 seconds is not written in stone (e.g. it may change before it expires), then don't use a context with 10 seconds timeout. Instead use a context with a cancel function: context.WithCancel()
, and manage the 10 seconds timeout yourself (e.g. with time.AfterFunc()
or with a time.Timer
). If the timeout has expired and you (or your workers) did not detect that it should be extended, call the cancel function.
If before the deadline you detect the timeout should be extended, reset the timer and do not cancel the context with the cancel function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With