bk

package module
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README

Bridgekeeper - What is your (Re)Quest?

Go Report Card codecov GoDoc License PRs Welcome

Bridgekeeper is a request throttler for http.Client

Bridgekeeper replaces the hard implementation of http.Client with an implementation of a shared interface such that anything implementing the bk.Client interface can use Bridgekeeper to throttle API requests through configuration.

Using Bridgekeeper
go get -u go.devnw.com/bk@latest
Example
    client := bk.New(
        ctx, // Your application context
        http.DefaultClient, // Your HTTP Client
        time.Millisecond, // Delay between requests
        5, // Retry count
        10, // Concurrent request limit
        http.DefaultClient.Timeout, // Request timeout
    )

    resp, err := client.Do(http.NewRequest(http.MethodGet, "localhost:5555"))

Client Interface

Bridgekeeper implements the interface shown below

type Client interface {
    Do(request *http.Request) (*http.Response, error)
}

This interface is also implemented by http.Client.

Documentation

Overview

package bk is intended to create a client side load balancer or rate limiter for API integrations. This library is specifically designed to wrap the `Do` method of the http.Client but since it uses an interface abstraction it can wrap any interface and limit requests.

Controls - Delay between requests - Number of retries per request - Concurrency limit for the client

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	Do(req *http.Request) (*http.Response, error)
}

Client defines an interface with a Do method for use with http.Client or other mocked instances that implement a do method that accept a request and return a response error combo

func New

func New(
	ctx context.Context,
	client Client,
	delay time.Duration,
	retries int,
	concurrency int,
	requestTimeout time.Duration,
) Client

New creates a new instance of the bridgekeeper for use with an api. New returns an interface implementation of Client which replaces the implementation of an http.Client interface so that it looks like an http.Client and can perform the same functions but it limits the requests using the parameters defined when created. NOTE: If a request timeout is not set at creation then the default HTTP client request timeout will be used

Jump to

Keyboard shortcuts

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