firewall

package
v0.5.7 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2020 License: MIT Imports: 3 Imported by: 0

README

Firewall

Intro
files how it works Benchmark / 2.6 GHz Intel Core i7
Limiter limiter interface
SleepLimiter sleep process when tokens are run out 20M / qps 81.9 ns/op
MutexLimiter use mutex counter and ticker to rate 100M / qps 14.2 ns/op
Semaphore wraped chan struct{} 30M / qps 40.4 ns/op
How to use
// RateLimiter
limiter := New{XXX}Limiter(1*time.Second, 300)	// 300 tokens in one second
limiter.Acquire()	// return immediately or waiting useable token

// Semaphore
sema := NewChanSemaphore(cap)
sema.Acquire()
sema.Release()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChanResourceLimiter added in v0.5.7

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

func NewChanResourcePool added in v0.5.7

func NewChanResourcePool(cap int) *ChanResourceLimiter

NewChanResourcePool ...

func (*ChanResourceLimiter) Acquire added in v0.5.7

func (this *ChanResourceLimiter) Acquire() interface{}

func (*ChanResourceLimiter) Release added in v0.5.7

func (this *ChanResourceLimiter) Release(resource interface{})

type ChanSemaphore

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

Semaphore 限制同一时刻,最多可以发生的次数

func NewChanSemaphore

func NewChanSemaphore(cap int) *ChanSemaphore

NewChanSemaphore

func (*ChanSemaphore) Acquire

func (this *ChanSemaphore) Acquire()

func (*ChanSemaphore) Release

func (this *ChanSemaphore) Release()

type Limiter added in v0.5.6

type Limiter interface {
	Acquire()
}

type MutexLimiter added in v0.5.6

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

func NewMutexLimiter added in v0.5.6

func NewMutexLimiter(ttl time.Duration, limit int) *MutexLimiter

func (*MutexLimiter) Acquire added in v0.5.6

func (l *MutexLimiter) Acquire()

type ResourceLimiter added in v0.5.7

type ResourceLimiter interface {
	Acquire() interface{}
	Release(resource interface{})
}

type Semaphore

type Semaphore interface {
	// P wait()
	Acquire()

	// V signal()
	Release()
}

func NewSemaphore

func NewSemaphore(cap int) Semaphore

NewSemaphore

type SleepLimiter

type SleepLimiter struct {
	sync.Mutex
	// contains filtered or unexported fields
}

SleepLimiter 限制单位时间内的可执行次数

func NewSleepLimiter

func NewSleepLimiter(ttl time.Duration, limit int) *SleepLimiter

func (*SleepLimiter) Acquire

func (this *SleepLimiter) Acquire()

Jump to

Keyboard shortcuts

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