Skip to content

Instantly share code, notes, and snippets.

@ankur22
Created March 4, 2022 11:11
Show Gist options
  • Save ankur22/8ead554d5c966e8af536456c0077a24a to your computer and use it in GitHub Desktop.
Save ankur22/8ead554d5c966e8af536456c0077a24a to your computer and use it in GitHub Desktop.
Checking whether the underlying client and context have a context and setting one on the context if no timeout set in either
// Set a timeout of 10secs if no timeout in the underlying
// http client and no timeout set in the context.
_, ok := ctx.Deadline()
if c.c.Timeout == 0 && !ok {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Second*10)
defer cancel()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment