limits

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2023 License: Apache-2.0 Imports: 5 Imported by: 1

Documentation

Index

Constants

View Source
const NoLimit = -1

Variables

View Source
var LimitsNotFound = NewLimitsError("limit error: limits not found in context")

LimitsNotFound is a standard error that indicates limits were expected to be present in a context, but were not found.

Functions

func NewLimitsError

func NewLimitsError(message string, args ...interface{}) error

NewLimitsError returns a new LimitsError with the given message.

func ReadAll

func ReadAll(reader io.Reader, limit int64) ([]byte, error)

ReadAll reads from the given reader until EOF or the limit is reached. If the given limit is less than zero, the entire reader is read.

func TrackCost

func TrackCost(ctx context.Context, cost int) error

TrackCost increments the processing cost associated with the context by the given amount. If the cost limit is exceeded, an error is returned.

func WithLimits

func WithLimits(ctx context.Context, l Limits) context.Context

WithCodeFunc adds an CodeFunc to the context, which can be used by objects to retrieve the active code at runtime

Types

type Limits

type Limits interface {

	// IOTimeout returns the maximum amount of time to wait for IO operations.
	IOTimeout() time.Duration

	// MaxBodySize returns the maximum allowed size buffer in bytes. This is
	// relevant for any buffered I/O operation, e.g. reading an HTTP request body.
	MaxBufferSize() int64

	// TrackHTTPRequest returns an error if the HTTP request should not
	// be processed due to exceeding a limit.
	TrackHTTPRequest(*http.Request) error

	// TrackHTTPResponse returns an error if the HTTP response should not
	// be processed due to exceeding a body or content length limit.
	TrackHTTPResponse(*http.Response) error

	// TrackCost returns an error if the given incremental processing cost
	// causes the cost limit to be exceeded.
	TrackCost(cost int) error

	// ReadAll reads from the given reader until EOF or a limit is reached.
	// This counts towards the allocation limit.
	ReadAll(reader io.Reader) ([]byte, error)
}

func GetLimits

func GetLimits(ctx context.Context) (Limits, bool)

GetLimits returns Risor limits associated with the context, if any.

func New

func New(opts ...Option) Limits

New creates a new Limits instance with the given options.

type LimitsError

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

LimitsError indicates that a limit was exceeded.

func (*LimitsError) Error

func (e *LimitsError) Error() string

type Option

type Option func(*StandardLimits)

Option is a function that configures a Limits instance.

func WithIOTimeout

func WithIOTimeout(timeout time.Duration) Option

WithIOTimeout sets the maximum amount of time to wait for IO operations.

func WithMaxBufferSize

func WithMaxBufferSize(size int64) Option

WithMaxBufferSize sets the maximum allowed size buffer in bytes. This is relevant for any buffered I/O operation, e.g. reading an HTTP request body.

func WithMaxCost

func WithMaxCost(cost int64) Option

WithMaxCost sets the maximum number allowed processing cost.

func WithMaxHttpRequestCount

func WithMaxHttpRequestCount(count int64) Option

WithMaxHttpRequestCount sets the maximum number of HTTP requests that are allowed to be processed.

type StandardLimits

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

func (*StandardLimits) IOTimeout

func (l *StandardLimits) IOTimeout() time.Duration

func (*StandardLimits) MaxBufferSize

func (l *StandardLimits) MaxBufferSize() int64

func (*StandardLimits) ReadAll

func (l *StandardLimits) ReadAll(reader io.Reader) ([]byte, error)

func (*StandardLimits) TrackCost

func (l *StandardLimits) TrackCost(cost int) error

func (*StandardLimits) TrackHTTPRequest

func (l *StandardLimits) TrackHTTPRequest(req *http.Request) error

func (*StandardLimits) TrackHTTPResponse

func (l *StandardLimits) TrackHTTPResponse(resp *http.Response) error

Jump to

Keyboard shortcuts

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