minrate

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2024 License: GPL-3.0 Imports: 3 Imported by: 0

README

Rate Limiter

A simple and flexible rate limiter implementation in Go that allows you to control the rate at which actions are performed. This package is useful for scenarios where you need to limit the number of operations or requests over a specific duration, such as API calls, task executions, or event handling.

Features

  • Configurable Rate Limits: Set the number of actions and the duration for which the limit applies.
  • Flexible Duration: Use any duration (e.g., seconds, minutes) for rate limiting.
  • Concurrency Safe: Designed to work safely with goroutines.

Installation

To use the minrate package in your Go project, you can install it using go get:

go get github.com/goupdate/minrate

Usage

Here's a quick example of how to use the RateLimiter:

package main

import (
    "fmt"
    "github.com/goupdate/minrate"
    "time"
)

func main() {
    // Create a RateLimiter that allows 10 actions per minute
    rl := minrate.New(10, time.Minute)

    // Perform 15 actions
    for i := 0; i < 15; i++ {
        go func(i int) {
            rl.Wait() // Wait until it's allowed to perform the action
            b := rl.Can()
            fmt.Printf("Action %d, can: %t", i, b)
        }(i)
    }

    // Wait for all actions to complete
    time.Sleep(2 * time.Minute)
}

API

New(actionsPerDuration int, duration time.Duration) *RateLimiter

Creates a new RateLimiter instance that allows actionsPerDuration actions over the specified duration.

- actionsPerDuration: Number of actions allowed per duration.
- duration: Time duration over which the actions are limited.

Wait()

Blocks until an action can be performed according to the rate limit. Call this method before performing an action to ensure compliance with the rate limit.

Can()

Informs will Wait() be locking right now or not. Call this method to test this case.

Contributing

Contributions are welcome! Please open an issue or submit a pull request if you have suggestions or improvements.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RateLimiter

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

RateLimiter представляет собой структуру для ограничения частоты действий

func New

func New(actionsPerDuration int, duration time.Duration) *RateLimiter

New creates new RateLimiter with given actions per duration minimum duration is 50millis = time.Millisecond * 50

func (*RateLimiter) Can

func (rl *RateLimiter) Can() bool

can now take action or wait? true if now wait i.e. Wait() call will end immidiatelly

func (*RateLimiter) CanOrWait added in v0.0.4

func (rl *RateLimiter) CanOrWait() bool

= false if !Can() = true if can and already waited

func (*RateLimiter) Close added in v0.0.2

func (rl *RateLimiter) Close()

remove limiter from checking queue

func (*RateLimiter) Wait

func (rl *RateLimiter) Wait()

Wait ожидает, пока действие станет возможным согласно ограничениям

Jump to

Keyboard shortcuts

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