limiter

package
v0.0.0-...-48af030 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2026 License: MPL-2.0 Imports: 22 Imported by: 0

Documentation

Overview

Package limiter is to control the links that go into the dispatcher

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WaitN

func WaitN(ctx context.Context, limiter *rate.Limiter, count uint64) error

WaitN admits count bytes without asking the limiter for more than its burst in one operation. The first wait failure stops admission.

Types

type BucketSnapshot

type BucketSnapshot struct {
	Limit float64
	Burst int
}

BucketSnapshot contains the observable value state of one rate limiter.

type GlobalDeviceLimitConfig

type GlobalDeviceLimitConfig struct {
	Enable        bool   `mapstructure:"Enable"`
	RedisNetwork  string `mapstructure:"RedisNetwork"` // tcp or unix
	RedisAddr     string `mapstructure:"RedisAddr"`    // host:port, or /path/to/unix.sock
	RedisUsername string `mapstructure:"RedisUsername"`
	RedisPassword string `mapstructure:"RedisPassword"`
	RedisDB       int    `mapstructure:"RedisDB"`
	Timeout       int    `mapstructure:"Timeout"`
	Expiry        int    `mapstructure:"Expiry"` // second
}

type InboundInfo

type InboundInfo struct {
	Tag            string
	NodeSpeedLimit uint64
	UserInfo       *sync.Map // Key: user tag (buildUserTag) -> UserInfo
	BucketHub      *sync.Map // Key: user tag -> *rate.Limiter
	UserOnlineIP   *sync.Map // Key: user tag -> *userOnlineEntry
	GlobalDevices  *globalDeviceState
	GlobalLimit    *globalLimitState
	// contains filtered or unexported fields
}

type InboundLimiterStateSnapshot

type InboundLimiterStateSnapshot struct {
	UserInfo map[string]UserInfo
	Buckets  map[string]bucketStateSnapshot
	// contains filtered or unexported fields
}

type InboundSnapshot

type InboundSnapshot struct {
	Tag                    string
	NodeSpeedLimit         uint64
	Users                  map[string]UserInfo
	Buckets                map[string]BucketSnapshot
	OnlineUsers            []api.OnlineUser
	GlobalDevices          map[int][]string
	GlobalDevicesUpdatedAt time.Time
	GlobalDevicesFresh     bool
	GlobalLimitEnabled     bool
	GlobalLimitClosed      bool
}

InboundSnapshot is a detached read-only view of one applied inbound. Runtime objects, cache backends, and global-limit credentials are omitted.

type Limiter

type Limiter struct {
	InboundInfo *sync.Map
	// contains filtered or unexported fields
}

func New

func New() *Limiter

func (*Limiter) AddInboundLimiter

func (l *Limiter) AddInboundLimiter(tag string, nodeSpeedLimit uint64, userList *[]api.UserInfo, globalLimit *GlobalDeviceLimitConfig) error

func (*Limiter) Admit

func (l *Limiter) Admit(tag, userKey, ip string, reader buf.Reader, writer buf.Writer) (buf.Reader, buf.Writer, bool)

func (*Limiter) ClearGlobalDevices

func (l *Limiter) ClearGlobalDevices(tag string) error

func (*Limiter) Close

func (l *Limiter) Close() error

func (*Limiter) DeleteInboundLimiter

func (l *Limiter) DeleteInboundLimiter(tag string) error

func (*Limiter) GetOnlineDevice

func (l *Limiter) GetOnlineDevice(tag string) (*[]api.OnlineUser, error)

func (*Limiter) GetUserBucket

func (l *Limiter) GetUserBucket(tag string, userKey string, ip string) (*rate.Limiter, bool, bool)

func (*Limiter) RateReader

func (l *Limiter) RateReader(reader buf.Reader, limiter *rate.Limiter) buf.Reader

func (*Limiter) RateWriter

func (l *Limiter) RateWriter(writer buf.Writer, limiter *rate.Limiter) buf.Writer

func (*Limiter) ReplaceInboundUsers

func (l *Limiter) ReplaceInboundUsers(tag string, userList *[]api.UserInfo) error

ReplaceInboundUsers applies an authoritative user policy snapshot for one inbound. State for users absent from the replacement is not retained.

func (*Limiter) RestoreInboundLimiterState

func (l *Limiter) RestoreInboundLimiterState(tag string, snapshot *InboundLimiterStateSnapshot) error

func (*Limiter) SnapshotInboundLimiterState

func (l *Limiter) SnapshotInboundLimiterState(tag string) (*InboundLimiterStateSnapshot, error)

func (*Limiter) StateSnapshot

func (l *Limiter) StateSnapshot() LimiterSnapshot

StateSnapshot returns a detached snapshot of valid applied inbounds. Forged or detached entries in the legacy mutable map are not published.

func (*Limiter) SyncAliveList

func (l *Limiter) SyncAliveList(tag string, aliveList map[int][]string) error

SyncAliveList synchronizes the alive list from panel to local tracking

func (*Limiter) UpdateGlobalDevices

func (l *Limiter) UpdateGlobalDevices(tag string, devices map[int][]string) error

func (*Limiter) UpdateInboundLimiter

func (l *Limiter) UpdateInboundLimiter(tag string, updatedUserList *[]api.UserInfo) error

type LimiterSnapshot

type LimiterSnapshot struct {
	Closed   bool
	Inbounds map[string]InboundSnapshot
}

LimiterSnapshot is a detached read-only view of the currently applied limiter state. Mutating its maps or slices does not change admission state. It contains user keys and IP addresses and must not be logged unredacted or used as metrics labels.

type Reader

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

type UserInfo

type UserInfo struct {
	UID         int
	SpeedLimit  uint64
	DeviceLimit int
}

type Writer

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

Jump to

Keyboard shortcuts

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