rollKeys

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2024 License: MIT Imports: 4 Imported by: 0

README

RollKeys

Make your API Credentials Rotational




This is a simple library that allows you to rotate your API credentials. Let's say there is a free API key limit 5 request per second. And to avoid this limitation is to create multiple API keys. and use them in rotation.

install

go get github.com/FPNL/RollKeys

usage

package main

import (
    "fmt"
	rollKeys "github.com/FPNL/RollKeys"
    "time"
)

func main() {
	// means each key could make 5 request per second
	keys, err := rollKeys.NewRotationalSlice([]string{"key1", "key2"}, 5)

	// if all the keys are fully loaded, it will wait.
	// most expect wait duration is (givenRequestTimes / (len(givenAPIKeys) * givenRate)) - 1
    // if there is 100 request at same time, in this example is 100 / (2 * 5) - 1 = 9 seconds
	key, err := keys.Get(context.TODO()) 
	
	// Do what you want to key
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithPickStrategy

func WithPickStrategy[T any](pickStrategy func([]*Limiter[T]) *Limiter[T]) func(*RotationalSlice[T])

Types

type Limiter

type Limiter[T any] struct {
	*rate.Limiter
	// contains filtered or unexported fields
}

func DefaultPickStrategy

func DefaultPickStrategy[T any](slice []*Limiter[T]) *Limiter[T]

func NewLimiter

func NewLimiter[T any](obj T, r int) *Limiter[T]

NewLimiter allow r executions per second

type RotationalSlice

type RotationalSlice[T any] struct {
	// contains filtered or unexported fields
}

func NewRotationalSlice

func NewRotationalSlice[T any](slice []T, r int, options ...func(*RotationalSlice[T])) (*RotationalSlice[T], error)

func (*RotationalSlice[T]) Get

func (c *RotationalSlice[T]) Get(ctx context.Context) (T, error)

Jump to

Keyboard shortcuts

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