Debian Codegraph @

lfshttp/retries.go

raw symbols SCIP
1 package lfshttp
2
3 import (
4 "context"
5 "net/http"
6 )
7
8 // ckey is a type that wraps a string for package-unique context.Context keys.
9 type ckey string
10
11 const (
12 // contextKeyRetries is a context.Context key for storing the desired
13 // number of retries for a given request.
14 contextKeyRetries ckey = "retries"
15
16 // defaultRequestRetries is the default number of retries to perform on
17 // a given HTTP request.
18 defaultRequestRetries = 0
19 )
20
21 // WithRetries stores the desired number of retries "n" on the given
22 // http.Request, and causes it to be retried "n" times in the case of a non-nil
23 // network related error.
24 func WithRetries(req *http.Request, n int) *http.Request {
25 ctx := req.Context()
26 ctx = context.WithValue(ctx, contextKeyRetries, n)
27
28 return req.WithContext(ctx)
29 }
30
31 // Retries returns the number of retries requested for a given http.Request.
32 func Retries(req *http.Request) (int, bool) {
33 n, ok := req.Context().Value(contextKeyRetries).(int)
34
35 return n, ok
36 }