Documentation ¶
Overview ¶
Package httpclient provides a production ready http.Client.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶
New helps you construct a production-ready http.Client using functional options.
Example ¶
var buf bytes.Buffer h := slog.NewJSONHandler(&buf, &slog.HandlerOptions{}) c := New( LogHandler(h), ) s := http.Server{ Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) }), } ls, err := net.Listen("tcp", ":0") if err != nil { fmt.Println(err) return } errCh := make(chan error) go func() { defer close(errCh) s.Serve(ls) }() resp, err := c.Get(fmt.Sprintf("http://%s", ls.Addr())) if err != nil { fmt.Println(err) return } if resp.StatusCode != http.StatusOK { return } ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() err = s.Shutdown(ctx) if err != nil { fmt.Println(err) return } br := bufio.NewReader(&buf) line, _, err := br.ReadLine() if err != nil { fmt.Println(err) return } var log struct { Msg string `json:"msg"` } err = json.Unmarshal(line, &log) if err != nil { fmt.Println(err) return } fmt.Println(log.Msg)
Output: request sent
Example (Named) ¶
var buf bytes.Buffer h := slog.NewJSONHandler(&buf, &slog.HandlerOptions{}) c := New( Name("example"), LogHandler(h), ) s := http.Server{ Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) }), } ls, err := net.Listen("tcp", ":0") if err != nil { fmt.Println(err) return } errCh := make(chan error) go func() { defer close(errCh) s.Serve(ls) }() resp, err := c.Get(fmt.Sprintf("http://%s", ls.Addr())) if err != nil { fmt.Println(err) return } if resp.StatusCode != http.StatusOK { return } ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() err = s.Shutdown(ctx) if err != nil { fmt.Println(err) return } br := bufio.NewReader(&buf) line, _, err := br.ReadLine() if err != nil { fmt.Println(err) return } var log struct { Name string `json:"http_client"` Msg string `json:"msg"` } err = json.Unmarshal(line, &log) if err != nil { fmt.Println(err) return } fmt.Println(log.Name) fmt.Println(log.Msg)
Output: example request sent
Types ¶
type CircuitOption ¶
type CircuitOption interface { Option // contains filtered or unexported methods }
CircuitOption are Options specifically for configuring the request circuit breaker.
func CountResetInterval ¶
func CountResetInterval(d time.Duration) CircuitOption
CountResetInterval is the cyclic period of the closed state for the CircuitBreaker to clear the internal Counts. If CountResetInterval is less than or equal to 0, the CircuitBreaker doesn't clear internal Counts during the closed state.
func HalfOpenRequests ¶
func HalfOpenRequests(n uint32) CircuitOption
HalfOpenRequests is the maximum number of requests allowed to pass through when the CircuitBreaker is half-open. If HalfOpenRequests is 0, the CircuitBreaker allows only 1 request.
func OpenStateTimeout ¶
func OpenStateTimeout(d time.Duration) CircuitOption
OpenStateTimeout is the period of the open state, after which the state of the CircuitBreaker becomes half-open. The default timeout is 60 seconds.
func TripAfter ¶
func TripAfter(n uint32) CircuitOption
TripAfter will cause the circuit to become open if the number of consecutive failuires is greater than or equal to n.
type OAuthOption ¶
type OAuthOption interface { Option // contains filtered or unexported methods }
OAuthOption are Options specifically for configuring OAuth.
func OAuth ¶
func OAuth(ts oauth2.TokenSource) OAuthOption
OAuth enables automatically adding the Authorization HTTP header with its value being an OAuth Bearer token.
type OTelOption ¶
type OTelOption interface { Option // contains filtered or unexported methods }
OTelOption are Options specifically for configuring OpenTelemetry.
func OTel ¶
func OTel(opts ...otelhttp.Option) OTelOption
OTel enables wrapping outbound requests with a span.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option is used to configure a http.Client in a functional manner.
func LogHandler ¶
LogHandler enables the http.Client to provide logs around:
- sending requests
- receiving responses
- circuit state changes
- retry attempts
func Name ¶
Name allows for naming this clients circuit breaker and providing a field in any logs where the key is "http_client" and the value being the name provided to this option.
func RoundTripper ¶
func RoundTripper(rt http.RoundTripper) Option
RoundTripper allows you to provide a custom base http.RoundTripper which all other capabilities, such as, circuit breaking and retries will wrap around.
type RetryOption ¶
type RetryOption interface { Option // contains filtered or unexported methods }
RetryOption are Options specifically for configuring request retry attempts.
func MaxRetries ¶
func MaxRetries(n int) RetryOption
MaxRetries specifies the maximum number of retries attempted.
func MaxRetryAfter ¶
func MaxRetryAfter(d time.Duration) RetryOption
MaxRetryAfter specifies the maximum time to wait before retrying a request.
func MinRetryAfter ¶
func MinRetryAfter(d time.Duration) RetryOption
MinRetryAfter specifies the minimum time to wait before retrying a request.