ratelimit

package
v2.122.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package ratelimit contains an http.RoundTripper that obeys the rate limits of Octopus Server.

See https://octopus.com/docs/administration/managing-infrastructure/rate-limiting

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHttpClient

func NewHttpClient(base *http.Client, options Options) *http.Client

NewHttpClient returns a copy of base that obeys the rate limits of Octopus Server. The copy sends requests through a Transport that wraps the transport of base. If base is nil, NewHttpClient uses a new http.Client. NewHttpClient does not change base.

The Timeout of base includes the time that the Transport waits before a retry or a paced request. If Timeout is less than MaxDelay, a request can fail with a timeout while the Transport waits.

Types

type Options

type Options struct {
	// RetryCount is the maximum number of times that the Transport sends a request again after a 429 response.
	// If RetryCount is 0 or less, the Transport does not retry, and the caller gets the 429 response.
	RetryCount int

	// DefaultDelay is the time to wait before a retry when the 429 response has no Retry-After header.
	// If DefaultDelay is 0 or less, the Transport uses the default of 2 seconds from DefaultOptions.
	DefaultDelay time.Duration

	// MaxDelay is the maximum time to wait before a retry. If the server tells the Transport to wait longer,
	// the Transport does not retry, and the caller gets the 429 response.
	// If MaxDelay is 0 or less, the Transport uses the default of 30 seconds from DefaultOptions.
	MaxDelay time.Duration

	// UseRateLimitHeaders makes the Transport pace its requests with the Octopus-RateLimit-Policy and
	// Octopus-RateLimit headers that the server sends. Then the Transport stays in the rate limit and
	// prevents most 429 responses.
	UseRateLimitHeaders bool
}

Options are the settings for a Transport.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns the recommended settings for a Transport.

type Transport

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

Transport is an http.RoundTripper that obeys the rate limits of Octopus Server.

When the server returns 429 (Too Many Requests), the Transport waits and sends the request again. The server does not process a request that it rejects, thus a retry is safe for all HTTP methods. The Transport retries a request only if it can read the request body again (the body is empty, or http.Request.GetBody is set). The delay doubles with each retry, and has random jitter.

When UseRateLimitHeaders is true, the Transport also paces each request, retries included, with the advisory headers that the server sends. The pacer uses GCRA (the Generic Cell Rate Algorithm), the same as the C# and TypeScript clients.

Use NewTransport to create a Transport. A Transport is safe for concurrent use by multiple goroutines. Do not copy a Transport after its first use.

func NewTransport

func NewTransport(base http.RoundTripper, options Options) *Transport

NewTransport returns a Transport that sends requests with base. If base is nil, the Transport uses http.DefaultTransport. If options.DefaultDelay or options.MaxDelay is 0 or less, the Transport uses the value from DefaultOptions.

func (*Transport) Base

func (t *Transport) Base() http.RoundTripper

Base returns the http.RoundTripper that the Transport uses to send requests.

func (*Transport) CloseIdleConnections

func (t *Transport) CloseIdleConnections()

CloseIdleConnections closes the idle connections of the base transport, if the base transport supports it. http.Client.CloseIdleConnections calls this method.

func (*Transport) RoundTrip

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper.

Jump to

Keyboard shortcuts

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