internal

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2025 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const Rendezvous = "decentralized-rate-limiter"

Variables

This section is empty.

Functions

func ConnectToBootstrapPeer

func ConnectToBootstrapPeer(ctx context.Context, h host.Host)

func Decode

func Decode[V any](data []byte) V

func Encode

func Encode(value any) []byte

func NewDeltaCache

func NewDeltaCache(size int) *lru.Cache[string, *TokenBucket]

func SetupPeerDiscovery

func SetupPeerDiscovery(ctx context.Context, h host.Host) *dht.IpfsDHT

func ToDeltaMessage

func ToDeltaMessage(cache *lru.Cache[string, *TokenBucket]) map[string]*BucketState

Types

type BucketCache

type BucketCache struct {
	Buckets    *lru.Cache[string, *TokenBucket]
	Disk       *Disk
	Capacity   float64
	RefillRate float64
}

func NewBucketCache

func NewBucketCache(node string, capacity float64, refillRate float64) *BucketCache

func (*BucketCache) GetOrCreateBucket

func (cache *BucketCache) GetOrCreateBucket(userID string) *TokenBucket

func (*BucketCache) RefreshDisk

func (cache *BucketCache) RefreshDisk()

type BucketState

type BucketState struct {
	UsedTokens   float64
	UsableTokens float64
	LastRefilled time.Time
	Timestamp    time.Time
}

type Disk

type Disk struct {
	DB *badger.DB
}

func NewDiskDB

func NewDiskDB(path string) *Disk

func (*Disk) Get

func (disk *Disk) Get(userID string) ([]byte, bool)

func (*Disk) Save

func (disk *Disk) Save(userID string, bucket []byte) error

type P2PNode

type P2PNode struct {
	Host      peer.ID
	Topic     *pubsub.Topic
	Sub       *pubsub.Subscription
	Multiaddr string
	DHT       *dht.IpfsDHT
}

func NewP2PNode

func NewP2PNode(ctx context.Context, topicName string) *P2PNode

func (*P2PNode) Broadcast

func (n *P2PNode) Broadcast(data []byte) error

func (*P2PNode) ReadLoop

func (n *P2PNode) ReadLoop(handle func([]byte))

type TokenBucket

type TokenBucket struct {
	Capacity     float64
	RefillRate   float64
	UsedTokens   float64
	UsableTokens float64
	LastRefilled time.Time
	Mutex        sync.RWMutex
}

func NewTokenBucket

func NewTokenBucket(capacity float64, refillRate float64) *TokenBucket

func (*TokenBucket) Consume

func (tb *TokenBucket) Consume() bool

func (*TokenBucket) Merge

func (tb *TokenBucket) Merge(incoming *BucketState) time.Duration

func (*TokenBucket) Refill

func (tb *TokenBucket) Refill()

Jump to

Keyboard shortcuts

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