interceptor

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package interceptor provides decorators for tg.Invoker to sniff, augment, and control MTProto operations.

Index

Constants

This section is empty.

Variables

View Source
var ErrFloodShedding = errors.New("tgbox: request dropped due to flood wait saturation (load shedding)")

ErrFloodShedding is returned when an MTProto request is dropped due to excessive flood wait saturation.

Functions

func IsOutgoing

func IsOutgoing(ctx context.Context) bool

IsOutgoing checks if the context originates from an outgoing (self-sent) call.

func OutgoingPeer added in v0.2.0

func OutgoingPeer(ctx context.Context) tg.InputPeerClass

OutgoingPeer retrieves the target input peer from the context if present.

func WithOutgoing

func WithOutgoing(ctx context.Context) context.Context

WithOutgoing marks the context as originating from an outgoing (self-sent) call.

func WithOutgoingPeer added in v0.2.0

func WithOutgoingPeer(ctx context.Context, peer tg.InputPeerClass) context.Context

WithOutgoingPeer associates the destination input peer with an outgoing request context.

Types

type BoundedFloodWaiter

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

BoundedFloodWaiter retries requests that hit FLOOD_WAIT errors, but only up to a bounded number of retries and a bounded wait duration; beyond either threshold the request is dropped with ErrFloodShedding.

func NewBoundedFloodWaiter

func NewBoundedFloodWaiter(maxRetries uint, maxWait time.Duration) *BoundedFloodWaiter

NewBoundedFloodWaiter creates the flood-wait middleware. A zero maxRetries or maxWait disables that specific limit.

func (*BoundedFloodWaiter) Handle

Handle wraps the standard tg.Invoker with backoff and shedding capabilities.

func (*BoundedFloodWaiter) WithClock

WithClock sets a custom clock for the BoundedFloodWaiter (essential for virtual clock testing).

type DispatcherFunc

type DispatcherFunc func(context.Context, tg.UpdatesClass) error

DispatcherFunc defines the callback signature to inject synthetic updates back into the pipeline.

type InMemoryLimiter

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

InMemoryLimiter implements a local token bucket limiter using golang.org/x/time/rate.

func NewInMemoryLimiter

func NewInMemoryLimiter(rateLimit rate.Limit, burst int) *InMemoryLimiter

NewInMemoryLimiter creates a local token-bucket limiter that refills rateLimit tokens per second with the given maximum burst.

func (*InMemoryLimiter) Wait

func (l *InMemoryLimiter) Wait(ctx context.Context) error

Wait blocks until a token is available or the context is canceled.

type Limiter

type Limiter interface {
	// Wait blocks until the limiter permits execution or the context is canceled.
	Wait(ctx context.Context) error
}

Limiter abstracts a rate-limiting strategy, allowing local (in-memory) and distributed (e.g. Redis) backends to be used interchangeably.

type OutgoingInterceptor

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

OutgoingInterceptor wraps a tg.Invoker to intercept outgoing message/media methods, emit synthetic UpdatesClass payloads, and trace network latencies via OpenTelemetry.

func NewOutgoingInterceptor

func NewOutgoingInterceptor(next tg.Invoker, dispatch DispatcherFunc, extractor TelemetryExtractor) *OutgoingInterceptor

NewOutgoingInterceptor creates a new decorator over a base tg.Invoker with telemetry support.

func (*OutgoingInterceptor) Invoke

func (o *OutgoingInterceptor) Invoke(ctx context.Context, input bin.Encoder, output bin.Decoder) error

Invoke executes the MTProto RPC call, tracing its execution time and sniffing outgoing message payloads.

type RateLimiter

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

RateLimiter wraps tg.Invoker to throttle outgoing RPC calls before hitting Telegram servers.

func NewRateLimiter

func NewRateLimiter(limiter Limiter) *RateLimiter

NewRateLimiter instantiates a new MTProto rate limiter middleware.

func (*RateLimiter) Handle

func (r *RateLimiter) Handle(next tg.Invoker) telegram.InvokeFunc

Handle intercepts the execution pipeline and enforces the rate limit rules.

type TelemetryExtractor

type TelemetryExtractor func(context.Context) (traceID string, clientID int64)

TelemetryExtractor defines a callback to extract trace and client identifiers without causing import cycles.

Jump to

Keyboard shortcuts

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