blitz

package module
v0.0.0-...-5b71783 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2024 License: Unlicense Imports: 17 Imported by: 0

README

Blitz

Blitz - Bypassing Limits with Intelligent Technical Zen - is a proxy to deal with rate limiting APIs.

By default, this acts as a transparent REST proxy that throttles requests to a certain rates. When a certain rate limit is exceeded, the proxy connection will be kept active until it can be forwarded. The rate limit can be configured via command line arguments.

Building

Build this using standard go tools, version 1.21 or above.

For example, to build a static linux binary:

GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o blitz ./cmd/blitz

You can also run on the current operating system using:

go run ./cmd/blitz

Running

To run the executable, either compile on-demand using go run, or run the built executable.

For example, to forward requests at a rate of at most 10 requests / second use:

./blitz -target https://example.com/ -queue 10

By default the executable will listen on port 8080 on 127.0.0.1. This can be changed using command line flags, run ./blitz -help to see a complete list of command line flags.

Status API

Clients can request the current status by making a GET request to /blitz/. They get back a JSON object describing the current status:

{
    // the current number of available slots for each queue
    "Slots":[1],

    // the average delay received by clients over the past 10 seconds, for each queue.
    // note that if there are only reservations this may be zero despite no forwards.
    "Delays": [0],
}

Requesting a slot

Clients can also (non-transparently) "reserve" a forwarding slot by making a POST request to /blitz/. They receive back a json object containing a number of milliseconds to wait, and a signed key to submit in a X-Blitz-Reservation header at that time.

{
    // was the request successful
    "Success":true,

    // the actual queue that was used for the reservation.
    // the is queue with the lowest delay, at most what the client requested.
    "Queue": 0,
    
    // delay from now until the reservation becomes valid, in milliseconds.
    "DelayInMilliseconds":0,
    
    // string to pass into the "X-Blitz-Reservation" header to use.
    "X-Blitz-Reservation":"VF8zc/FkSTBCDWj8Nn9fba3+Uc84leJ9Np0LwJaEGddaHZnw6Q3iV+7UOZrUTuHQW8UStDrbwYojZc4X56nbBMHsIj+MAQAAqfAiP4wBAAA",
    
    // time the token is valid from and until, unix timestamp in milliseconds.
    "TokenValidFromUnixMilliseconds":0,
    "TokenValidUntilUnixMilliseconds":0,
}

Afterwards, making any request to the proxy with the X-Blitz-Reservation header set to the provided string makes use of the reservation. A call with an invalid reservation header results in an error.

Multiple slots

Blitz supports running multiple prioritized queues. To start it with multiple queues, simply pass the -queue command line flag multiple times.

Then send requests with the X-Blitz-Queue header to select a queue. For example, passsing X-Blitz-Queue with a value of 0 will select the first queue.

Queues with higher indexes are considered higher priority. If the wait time on a higher queue is longer, the client will be automatically pushed to a lower queue.

Note that blitz reservations only need to pass the header when making the reservation, not when using it.

LICENSE

See LICENSE

Documentation

Index

Constants

View Source
const (
	HeaderReservation = "X-Blitz-Reservation"
	HeaderQueue       = "X-Blitz-Queue"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Blitz

type Blitz struct {
	Logger  *log.Logger
	Handler http.Handler
	// contains filtered or unexported fields
}

func New

func New(rand io.Reader, handler http.Handler, every time.Duration, bs []uint64) (*Blitz, error)

Blitz creates a new blitz server wrapping handler

func (*Blitz) ServeHTTP

func (blitz *Blitz) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Blitz) Status

func (blitz *Blitz) Status() (st Status)

type Stats

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

Stats averages a set of values over the last period d. The zero value is not ready for use, see NewStats.

func NewStats

func NewStats(d time.Duration) *Stats

NewStats creates a new stats object that holds statistics for the given duration.

func (*Stats) Add

func (s *Stats) Add(value *big.Float)

Add adds a new value to be averaged for the current time.

func (*Stats) AddInt64

func (s *Stats) AddInt64(value int64)

AddInt64 is like Add, but takes an int64

func (*Stats) Average

func (s *Stats) Average() *big.Float

Average returns the average values added over the past d duration.

type Status

type Status struct {
	Slots  []int64
	Delays []int64
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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