Documentation
¶
Overview ¶
Package limiter provides resource limiting mechanisms for high availability.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrLimitExceeded = errors.New("resource limit exceeded") ErrRateLimitExceeded = errors.New("rate limit exceeded") )
Errors
Functions ¶
This section is empty.
Types ¶
type CompositeLimiter ¶
type CompositeLimiter struct {
// contains filtered or unexported fields
}
CompositeLimiter combines multiple limiters.
func NewCompositeLimiter ¶
func NewCompositeLimiter(limiters ...interface {
Acquire() error
Release()
}) *CompositeLimiter
NewCompositeLimiter creates a new composite limiter.
func (*CompositeLimiter) Acquire ¶
func (l *CompositeLimiter) Acquire() error
Acquire acquires all limiters.
func (*CompositeLimiter) Release ¶
func (l *CompositeLimiter) Release()
Release releases all limiters.
type ConnectionLimiter ¶
type ConnectionLimiter struct {
// contains filtered or unexported fields
}
ConnectionLimiter limits the number of concurrent connections.
func NewConnectionLimiter ¶
func NewConnectionLimiter(max int64) *ConnectionLimiter
NewConnectionLimiter creates a new connection limiter.
func (*ConnectionLimiter) Acquire ¶
func (l *ConnectionLimiter) Acquire() error
Acquire attempts to acquire a connection slot. Returns ErrLimitExceeded if the limit is reached.
func (*ConnectionLimiter) Available ¶
func (l *ConnectionLimiter) Available() int64
Available returns the number of available slots.
func (*ConnectionLimiter) Current ¶
func (l *ConnectionLimiter) Current() int64
Current returns the current number of connections.
func (*ConnectionLimiter) Release ¶
func (l *ConnectionLimiter) Release()
Release releases a connection slot.
type GoroutineLimiter ¶
type GoroutineLimiter struct {
// contains filtered or unexported fields
}
GoroutineLimiter limits the number of concurrent goroutines.
func NewGoroutineLimiter ¶
func NewGoroutineLimiter(max int64) *GoroutineLimiter
NewGoroutineLimiter creates a new goroutine limiter.
func (*GoroutineLimiter) Acquire ¶
func (l *GoroutineLimiter) Acquire() error
Acquire acquires a goroutine slot.
func (*GoroutineLimiter) Current ¶
func (l *GoroutineLimiter) Current() int64
Current returns the current number of goroutines.
func (*GoroutineLimiter) Go ¶
func (l *GoroutineLimiter) Go(fn func()) error
Go runs a function in a new goroutine if the limit is not exceeded.
func (*GoroutineLimiter) Release ¶
func (l *GoroutineLimiter) Release()
Release releases a goroutine slot.
type MemoryLimiter ¶
type MemoryLimiter struct {
// contains filtered or unexported fields
}
MemoryLimiter tracks memory usage.
func NewMemoryLimiter ¶
func NewMemoryLimiter(maxBytes int64) *MemoryLimiter
NewMemoryLimiter creates a new memory limiter.
func (*MemoryLimiter) Allocate ¶
func (l *MemoryLimiter) Allocate(bytes int64) error
Allocate attempts to allocate the given number of bytes.
func (*MemoryLimiter) Available ¶
func (l *MemoryLimiter) Available() int64
Available returns the available bytes.
func (*MemoryLimiter) Release ¶
func (l *MemoryLimiter) Release(bytes int64)
Release releases the given number of bytes.
func (*MemoryLimiter) UsagePercent ¶
func (l *MemoryLimiter) UsagePercent() float64
UsagePercent returns the usage as a percentage.
func (*MemoryLimiter) Used ¶
func (l *MemoryLimiter) Used() int64
Used returns the currently used bytes.
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter implements a token bucket rate limiter.
func NewRateLimiter ¶
func NewRateLimiter(rate int64, interval time.Duration) *RateLimiter
NewRateLimiter creates a new rate limiter. rate is the number of operations allowed per interval.
func (*RateLimiter) Allow ¶
func (rl *RateLimiter) Allow() bool
Allow checks if an operation is allowed.
func (*RateLimiter) AllowN ¶
func (rl *RateLimiter) AllowN(n int64) bool
AllowN checks if n operations are allowed.
func (*RateLimiter) Tokens ¶
func (rl *RateLimiter) Tokens() int64
Tokens returns the current number of tokens.
type ResourceMonitor ¶
type ResourceMonitor struct {
// contains filtered or unexported fields
}
ResourceMonitor monitors resource usage.
func NewResourceMonitor ¶
func NewResourceMonitor(maxConns, maxGoroutines, maxMemory int64, rateLimit int64, rateInterval time.Duration) *ResourceMonitor
NewResourceMonitor creates a new resource monitor.
func (*ResourceMonitor) Stats ¶
func (m *ResourceMonitor) Stats() Stats
Stats returns current resource statistics.