nicehttp

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 22, 2025 License: MIT Imports: 11 Imported by: 2

README

nicehttp

A nice HTTP client written in golang with rate limit, backup and max retries. Respects 429 Retry-After header.

Go Report Card Test Go Reference

Usage

package main

import (
    "fmt"
    "io"
    "net/http"

    "github.com/rohfle/nicehttp"
)

func main() {
	headers := make(http.Header)
	headers.Set("Authentication", "Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")

	downstream := http.DefaultTransport.(*http.Transport).Clone()
	downstream.MaxConnsPerHost = 1
	// downstream.TLSClientConfig = &tls.Config{
	//     InsecureSkipVerify: true, // Skip certificate verification
	// }

	backoff := nicehttp.NewExponentialBackoff(1*time.Second, 120*time.Second, nicehttp.DefaultExponentialBackoffCoefficients)
	// backoff := nicehttp.NewExponentialBackoff(1*time.Second, 120*time.Second, nicehttp.ExponentialBackoffCoefficients{
	// 	   Success: 0.5,
	// 	   Fail:    1.5,
	// 	   Jitter:  0.3,
	// })

	transport, err := nicehttp.NewNiceTransportBuilder().
		SetDefaultHeaders(headers).
		SetUserAgent("your-user-agent-here/0.1").
		SetMaxAttempts(10).
		SetAttemptTimeout(120 * time.Second).
		SetLimiterBackoff(backoff).
		SetDownstreamTransport(downstream).
		Build()
	if err != nil {
		fmt.Println("error:", err)
		return
	}

	client := &http.Client{
		Transport: transport,
		Timeout:   10 * time.Minute,
		// CheckRedirect:
	}

	ctx := nicehttp.SetAttemptTimeoutInContext(context.Background(), 5*time.Second)
	ctx = nicehttp.SetMaxAttemptsInContext(ctx, 5)
	req, err := http.NewRequestWithContext(ctx, "GET", server.URL, nil)
	if err != nil {
		fmt.Println("error:", err)
		return
	}

	resp, err := client.Do(req)
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	defer resp.Body.Close()
	data, err := io.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	fmt.Println("got resp:", string(data))
}

License

MIT License

Copyright (c) 2025 Rohan Fletcher

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Overview

Package nicehttp provides a custom HTTP client with built-in support for rate limiting, automatic retries, and exponential backoff.

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultExponentialBackoffCoefficients = ExponentialBackoffCoefficients{
	Success: 0.5,
	Fail:    1.5,
	Jitter:  0.3,
}
View Source
var DefaultUserAgent = fmt.Sprintf("nicehttp/%s", getVersion())
View Source
var NoTimeout time.Duration = 0

Functions

func GetAttemptTimeoutFromContext added in v0.2.0

func GetAttemptTimeoutFromContext(ctx context.Context) (time.Duration, bool)

GetAttemptTimeoutFromContext retrieves the timeout value from the context.

func GetMaxAttemptsFromContext added in v0.2.0

func GetMaxAttemptsFromContext(ctx context.Context) (int, bool)

GetMaxAttemptsFromContext retrieves the max attempts value from the context.

func SetAttemptTimeoutInContext added in v0.2.0

func SetAttemptTimeoutInContext(parent context.Context, timeout time.Duration) context.Context

SetAttemptTimeoutInContext returns a new context with the given timeout value.

func SetMaxAttemptsInContext added in v0.2.0

func SetMaxAttemptsInContext(parent context.Context, attempts int) context.Context

SetMaxAttemptsInContext returns a new context with the given max attempts value.

Types

type Backoff added in v0.2.0

type Backoff interface {
	Update(fail bool, waitGiven time.Duration) time.Duration
	UntilNext() time.Duration
	Next() time.Time
	Clone() Backoff
}

type ExponentialBackoff added in v0.2.0

type ExponentialBackoff struct {
	// contains filtered or unexported fields
}

func NewExponentialBackoff added in v0.2.0

func NewExponentialBackoff(minWait time.Duration, maxWait time.Duration, coeff ExponentialBackoffCoefficients) *ExponentialBackoff

func (*ExponentialBackoff) Clone added in v0.2.0

func (adj *ExponentialBackoff) Clone() Backoff

func (*ExponentialBackoff) Next added in v0.2.0

func (adj *ExponentialBackoff) Next() time.Time

func (*ExponentialBackoff) UntilNext added in v0.2.0

func (adj *ExponentialBackoff) UntilNext() time.Duration

func (*ExponentialBackoff) Update added in v0.2.0

func (adj *ExponentialBackoff) Update(fail bool, newWait time.Duration) time.Duration

type ExponentialBackoffCoefficients added in v0.2.0

type ExponentialBackoffCoefficients struct {
	Success float64
	Fail    float64
	Jitter  float64
}

type Limiter added in v0.2.0

type Limiter struct {
	// contains filtered or unexported fields
}

func NewLimiter added in v0.2.0

func NewLimiter(backoff Backoff) *Limiter

func (*Limiter) Clone added in v0.2.0

func (rl *Limiter) Clone() *Limiter

func (*Limiter) Done added in v0.2.0

func (rl *Limiter) Done(retry bool, waitOverride time.Duration) error

func (*Limiter) Wait added in v0.2.0

func (rl *Limiter) Wait(ctx context.Context) error

Wait ensures sequential access via a fifo lock, then waits for a period determined by a backoff strategy, while respecting the cancellation and deadline of the context. Requires call to Done() to unlock the fifo lock

type NiceTransport added in v0.2.0

type NiceTransport struct {
	// contains filtered or unexported fields
}

func (*NiceTransport) Clone added in v0.2.0

func (s *NiceTransport) Clone() *NiceTransport

func (*NiceTransport) RoundTrip added in v0.2.0

func (rt *NiceTransport) RoundTrip(origReq *http.Request) (*http.Response, error)

RoundTrip is a custom implementation that handles backoff, error retries and done context

type NiceTransportBuilder added in v0.2.0

type NiceTransportBuilder struct {
	// contains filtered or unexported fields
}

NiceTransportBuilder is a builder used to create a NiceTransport

func NewNiceTransportBuilder added in v0.2.0

func NewNiceTransportBuilder() *NiceTransportBuilder

NewNiceTransportBuilder creates a NewNiceTransportBuilder

Example
package main

import (
	"context"
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"
	"time"

	"github.com/rohfle/nicehttp"
)

func main() {
	// Mock HTTP server
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "hello world")
	}))
	defer server.Close()

	headers := make(http.Header)
	headers.Set("Authentication", "Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")

	downstream := http.DefaultTransport.(*http.Transport).Clone()
	downstream.MaxConnsPerHost = 1
	// downstream.TLSClientConfig = &tls.Config{
	//     InsecureSkipVerify: true, // Skip certificate verification
	// }

	backoff := nicehttp.NewExponentialBackoff(1*time.Second, 120*time.Second, nicehttp.DefaultExponentialBackoffCoefficients)
	// backoff := nicehttp.NewExponentialBackoff(1*time.Second, 120*time.Second, nicehttp.ExponentialBackoffCoefficients{
	// 	   Success: 0.5,
	// 	   Fail:    1.5,
	// 	   Jitter:  0.3,
	// })

	transport, err := nicehttp.NewNiceTransportBuilder().
		SetDefaultHeaders(headers).
		SetUserAgent("your-user-agent-here/0.1").
		SetMaxAttempts(10).
		SetAttemptTimeout(120 * time.Second).
		SetLimiterBackoff(backoff).
		SetDownstreamTransport(downstream).
		Build()
	if err != nil {
		fmt.Println("error:", err)
		return
	}

	client := &http.Client{
		Transport: transport,
		Timeout:   10 * time.Minute,
		// CheckRedirect:
	}

	ctx := nicehttp.SetAttemptTimeoutInContext(context.Background(), 5*time.Second)
	ctx = nicehttp.SetMaxAttemptsInContext(ctx, 5)
	req, err := http.NewRequestWithContext(ctx, "GET", server.URL, nil)
	if err != nil {
		fmt.Println("error:", err)
		return
	}

	resp, err := client.Do(req)
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	defer resp.Body.Close()
	data, err := io.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	fmt.Println("got resp:", string(data))
}
Output:

got resp: hello world

func (*NiceTransportBuilder) Build added in v0.2.0

func (b *NiceTransportBuilder) Build() (*NiceTransport, error)

Build creates a NiceTransport with settings given previously combined with defaults.

func (*NiceTransportBuilder) Set added in v0.2.0

Set sets values of the builder from an existing NiceTransport

func (*NiceTransportBuilder) SetAttemptTimeout added in v0.2.0

func (b *NiceTransportBuilder) SetAttemptTimeout(timeout time.Duration) *NiceTransportBuilder

A value of 0 means no timeout.

func (*NiceTransportBuilder) SetDefaultHeaders added in v0.2.0

func (b *NiceTransportBuilder) SetDefaultHeaders(headers http.Header) *NiceTransportBuilder

SetDefaultHeaders sets headers that will be by default included in each request. If this is called multiple times, then existing keys will be overwritten.

func (*NiceTransportBuilder) SetDownstreamTransport added in v0.2.0

func (b *NiceTransportBuilder) SetDownstreamTransport(rt http.RoundTripper) *NiceTransportBuilder

SetDownstreamTransport sets the downstream transport to be called by RoundTrip

func (*NiceTransportBuilder) SetLimiterBackoff added in v0.2.0

func (b *NiceTransportBuilder) SetLimiterBackoff(backoff Backoff) *NiceTransportBuilder

If this is not set, the DefaultExponentialBackoff will be used

func (*NiceTransportBuilder) SetMaxAttempts added in v0.2.0

func (b *NiceTransportBuilder) SetMaxAttempts(n int) *NiceTransportBuilder

SetMaxAttempts sets the maximum number of request attempts that will be made

func (*NiceTransportBuilder) SetUserAgent added in v0.2.0

func (b *NiceTransportBuilder) SetUserAgent(ua string) *NiceTransportBuilder

SetUserAgent sets the User-Agent in default headers

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL