ratelimiter

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2021 License: MIT Imports: 4 Imported by: 0

README

Group Ratelimiter

codecov

Данный пакет предназначен для ограничения доступа к ресурсу между несколькими группами потребителей.

Под собой библиотека использует https://github.com/uber-go/ratelimit

import (
	"fmt"
	"sync"
	"time"
)

func main() {
	const firstGroup = "firstGroup"
	const secondGroup = "secondGroup"

	rl := New(1000). // global limit 1000 rps
	    AddGroup(firstGroup, 100). // limit of the first group 100 rps
	    AddGroup(secondGroup, 200) // limit of the second group 200 rps

	ch1 := make(chan string, 10)
	ch2 := make(chan string, 10)

	var wg sync.WaitGroup
	wg.Add(2)
	ctx := context.Background()

	go func() {
		defer wg.Done()
		prev := time.Now()
		for i := 0; i < 5; i++ {
			now := rl.Take(ctx, firstGroup)
			if i != 0 {
				ch1 <- now.Sub(prev).String()
			}
			prev = now
		}
	}()

	go func() {
		defer wg.Done()
		prev := time.Now()
		for i := 0; i < 5; i++ {
			now := rl.Take(ctx, secondGroup)
			if i != 0 {
				ch2 <- now.Sub(prev).String()
			}
			prev = now
		}
	}()
	wg.Wait()

	close(ch1)
	close(ch2)

	for v := range ch1 {
		fmt.Println(v)
	}

	for v := range ch2 {
		fmt.Println(v)
	}

    // where first four values of the output are time waited before requests for firstGroup
    // and second four values time waited before requests for secondGroup

	// ms - millisecond or one thousandth of a second
	// Output:
	// 10ms
	// 10ms
	// 10ms
	// 10ms
	// 5ms
	// 5ms
	// 5ms
	// 5ms
}

Documentation

Overview

Example
const firstGroup = "firstGroup"
const secondGroup = "secondGroup"

rl := New(1000, WithoutSlack()).
	AddGroup(firstGroup, 100, WithoutSlack()).
	AddGroup(secondGroup, 200, WithoutSlack())

ch1 := make(chan string, 10)
ch2 := make(chan string, 10)

var wg sync.WaitGroup
wg.Add(2)
ctx := context.Background()

go func() {
	defer wg.Done()
	prev := time.Now()
	for i := 0; i < 5; i++ {
		now := rl.Take(ctx, firstGroup)
		if i != 0 {
			ch1 <- now.Sub(prev).String()
		}
		prev = now
	}
}()

go func() {
	defer wg.Done()
	prev := time.Now()
	for i := 0; i < 5; i++ {
		now := rl.Take(ctx, secondGroup)
		if i != 0 {
			ch2 <- now.Sub(prev).String()
		}
		prev = now
	}
}()
wg.Wait()

close(ch1)
close(ch2)

for v := range ch1 {
	fmt.Println(v)
}

for v := range ch2 {
	fmt.Println(v)
}
Output:

10ms
10ms
10ms
10ms
5ms
5ms
5ms
5ms

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Per

func Per(per time.Duration) ratelimit.Option

Per - allow configure time window for limits

func WithClock

func WithClock(clock clock.Clock) ratelimit.Option

WithClock - allow set custom clock objects

func WithSlack

func WithSlack(slack int) ratelimit.Option

WithSlack - allow collect unused requests for future, set how much unused requests can be collected

func WithoutSlack

func WithoutSlack() ratelimit.Option

WithoutSlack - disable slack

Types

type GroupLimiter

type GroupLimiter interface {
	Take(context context.Context, groupName string) time.Time
	AddGroup(groupName string, rate int, opts ...ratelimit.Option) GroupLimiter
	SetGroup(groupName string, limiter ratelimit.Limiter)
}

GroupLimiter - limit access to resource across groups

func New

func New(rate int, opts ...ratelimit.Option) GroupLimiter

Jump to

Keyboard shortcuts

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