api

package
v0.0.0-...-d9a1f69 Latest Latest
Warning

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

Go to latest
Published: May 21, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

W&B backend API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backend

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

The W&B backend server.

There is generally exactly one Backend and a small number of Clients in a process.

func New

func New(opts BackendOptions) *Backend

Creates a Backend.

The `baseURL` is the scheme and hostname for contacting the server, not including a final slash. Example "http://localhost:8080".

func (*Backend) NewClient

func (backend *Backend) NewClient(opts ClientOptions) Client

Creates a new Client for making requests to the Backend.

type BackendOptions

type BackendOptions struct {
	// The scheme and hostname for contacting the server, not including a final
	// slash. For example, "http://localhost:8080".
	BaseURL *url.URL

	// Logger for HTTP operations.
	Logger *slog.Logger

	// W&B API key.
	APIKey string
}

type Client

type Client interface {
	// Sends an HTTP request to the W&B backend.
	//
	// It is guaranteed that the response is non-nil unless there is an error.
	Send(*Request) (*http.Response, error)

	// Sends an arbitrary HTTP request.
	//
	// This is used for libraries that accept a custom HTTP client that they
	// then use to make requests to the backend, like GraphQL. If the request
	// URL matches the backend's base URL, there's special handling as in
	// Send().
	//
	// It is guaranteed that the response is non-nil unless there is an error.
	Do(*http.Request) (*http.Response, error)
}

An HTTP client for interacting with the W&B backend.

There is one Client per API provided by the backend, where "API" is a collection of related HTTP endpoints. It's expected that different APIs have different properties such as rate-limits and ideal retry behaviors.

The client is responsible for setting auth headers, retrying gracefully, and respecting rate-limit response headers.

type ClientOptions

type ClientOptions struct {
	// Maximum number of retries to make for retryable requests.
	RetryMax int

	// Minimum time to wait between retries.
	RetryWaitMin time.Duration

	// Maximum time to wait between retries.
	RetryWaitMax time.Duration

	// Function that determines whether to retry based on the response.
	//
	// If nil, then retries are made on connection errors and server errors
	// (HTTP status code >=500).
	RetryPolicy retryablehttp.CheckRetry

	// Timeout for HTTP requests.
	//
	// This is the time to wait for an individual HTTP request to complete
	// before considering it as failed. It does not include retries: each retry
	// starts a new timeout.
	NonRetryTimeout time.Duration

	// Additional headers to pass in each request to the backend.
	//
	// Note that these are only passed when communicating with the W&B backend.
	// In particular, they are not sent if using this client to send
	// arbitrary HTTP requests.
	ExtraHeaders map[string]string

	// Allows the client to peek at the network traffic, can preform any action
	// on the request and response. Need to make sure that the response body is
	// available to read by later stages.
	NetworkPeeker Peeker
}

type Peeker

type Peeker interface {
	Peek(*http.Request, *http.Response)
}

type PeekingTransport

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

An HTTP transport wrapper that reports HTTP responses back to the client.

func NewPeekingTransport

func NewPeekingTransport(
	peeker Peeker,
	delegate http.RoundTripper,
) *PeekingTransport

func (*PeekingTransport) RoundTrip

func (transport *PeekingTransport) RoundTrip(
	req *http.Request,
) (*http.Response, error)

type RateLimitHeaders

type RateLimitHeaders struct {
	// The RateLimit-Remaining header.
	//
	// Guaranteed non-negative.
	Remaining float64

	// The RateLimit-Reset header.
	//
	// Guaranteed non-negative.
	Reset float64
}

Values of the RateLimit headers defined in https://www.ietf.org/archive/id/draft-polli-ratelimit-headers-02.html

func ParseRateLimitHeaders

func ParseRateLimitHeaders(header http.Header) (RateLimitHeaders, bool)

Parses RateLimitHeaders out of the HTTP header.

type RateLimitTracker

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

func NewRateLimitTracker

func NewRateLimitTracker(params RateLimitTrackerParams) *RateLimitTracker

func (*RateLimitTracker) TargetRateLimit

func (tracker *RateLimitTracker) TargetRateLimit() float64

Returns the current estimated target rate limit.

func (*RateLimitTracker) TrackRequest

func (tracker *RateLimitTracker) TrackRequest()

Registers that we're about to make a request.

func (*RateLimitTracker) UpdateEstimates

func (tracker *RateLimitTracker) UpdateEstimates(
	t time.Time,
	rlHeader RateLimitHeaders,
)

Updates the target rate limit using RateLimit header values.

- t: the current time - rlRemaining: the RateLimit-Remaining header - rlReset: the RateLimit-Reset header

type RateLimitTrackerParams

type RateLimitTrackerParams struct {
	// Minimum rate limit in requests per second.
	MinPerSecond float64

	// Maximum rate limit in requests per second.
	MaxPerSecond float64

	// Smoothing factor in range [0, 1).
	//
	// The closer this is to 0, the faster the rate is updated based on
	// response headers.
	Smoothing float64

	// Minimum number of requests in an estimation window.
	//
	// Setting above 1 causes optimal rate limit estimates to be averaged out
	// over more requests.
	MinRequestsForEstimate uint64
}

type RateLimitedTransport

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

A rate-limited HTTP transport for requests to the W&B backend.

Implements http.RoundTripper for use as a transport for an HTTP client.

func NewRateLimitedTransport

func NewRateLimitedTransport(
	delegate http.RoundTripper,
) *RateLimitedTransport

Rate-limits an HTTP transport for the W&B backend.

func (*RateLimitedTransport) RoundTrip

func (transport *RateLimitedTransport) RoundTrip(
	req *http.Request,
) (*http.Response, error)

type Request

type Request struct {
	// The standard HTTP method.
	Method string

	// The request path, not including the scheme or hostname.
	//
	// Example: "/files/a/b/c/file_stream".
	Path string

	// The request body or nil.
	//
	// Since requests may be retried, this is always a byte slice rather than
	// an [io.ReadCloser] as in Go's standard HTTP package.
	Body []byte

	// Additional HTTP headers to include in request.
	//
	// These are sent in addition to any headers set automatically by the
	// client, such as for auth. The client headers take precedence.
	Headers map[string]string
}

An HTTP request to the W&B backend.

type RetryableClient

type RetryableClient interface {
	Do(*retryablehttp.Request) (*http.Response, error)
}

Jump to

Keyboard shortcuts

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