File: retries.go

package info (click to toggle)
git-lfs 3.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,880 kB
  • sloc: sh: 23,157; makefile: 519; ruby: 404
file content (36 lines) | stat: -rw-r--r-- 973 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package lfshttp

import (
	"context"
	"net/http"
)

// ckey is a type that wraps a string for package-unique context.Context keys.
type ckey string

const (
	// contextKeyRetries is a context.Context key for storing the desired
	// number of retries for a given request.
	contextKeyRetries ckey = "retries"

	// defaultRequestRetries is the default number of retries to perform on
	// a given HTTP request.
	defaultRequestRetries = 0
)

// WithRetries stores the desired number of retries "n" on the given
// http.Request, and causes it to be retried "n" times in the case of a non-nil
// network related error.
func WithRetries(req *http.Request, n int) *http.Request {
	ctx := req.Context()
	ctx = context.WithValue(ctx, contextKeyRetries, n)

	return req.WithContext(ctx)
}

// Retries returns the number of retries requested for a given http.Request.
func Retries(req *http.Request) (int, bool) {
	n, ok := req.Context().Value(contextKeyRetries).(int)

	return n, ok
}