Saltar al contenido principal

Rate Limiting

AutoP2P uses a three-layer rate limiting architecture to stay within Binance's limits while maximising repricing frequency. A circuit breaker provides automatic recovery from overload.

Three-Layer Architecture

Requests pass through three independent layers in order. All layers must allow a request before it is dispatched.

LayerScopeMechanismKey Config
L1Per-AdToken bucket per individual ad engineLIMITER_GLOBAL_RPS=4.5, LIMITER_PER_AD_RPS=1.5
L2GlobalShared token bucket + concurrency semaphoreGLOBAL_BINANCE_RPS=5.0, GLOBAL_MAX_CONCURRENT=8, weight limit 12 000/min
L3Client Retry BudgetPer-call exponential backoff with attempt capRETRY_BUDGET=5 attempts

Circuit Breaker State Machine

The global limiter includes a circuit breaker that automatically suspends requests when error rates spike, then probes recovery before resuming full traffic.

CLOSED
Normal operation
≥ threshold failures
OPEN
Requests blocked
cooldown elapsed
HALF-OPEN
Probe requests
HALF-OPEN→ probe success →CLOSED
HALF-OPEN→ probe failure →OPEN
VariableDefaultDescription
GLOBAL_CB_THRESHOLD15Consecutive failures before tripping OPEN
GLOBAL_CB_COOLDOWN45.0Seconds to wait in OPEN before entering HALF-OPEN
GLOBAL_CB_HALF_OPEN_REQUESTS1Number of probe requests allowed in HALF-OPEN state

Client-Side Rate Limit Groups

Binance enforces separate weight limits per API group. AutoP2P tracks usage per group using response headers.

GroupEndpointsWeight HeaderLimit
advAll /sapi/v1/c2c/ endpointsx-mbx-used-weight-1m12 000 / min
sapiSigned SAPI requests (general)x-sapi-used-ip-weight-1m12 000 / min
spot/api/v3/ endpoints (e.g. server time)x-mbx-used-weight-1m6 000 / min

Weight Tracking

After every response, AutoP2P reads the x-mbx-used-weight-1m header and compares it to the known limit. When usage exceeds WEIGHT_BACKOFF_THRESHOLD (default 80%), the global limiter slows token emission to avoid hitting the hard ceiling.

# Pseudo-logic in global_limiter.on_response()
used = int(response.headers.get("x-mbx-used-weight-1m", 0))
ratio = used / 12_000  # WEIGHT_LIMIT

if ratio >= WEIGHT_BACKOFF_THRESHOLD:  # default 0.8
    # Reduce token emission rate proportionally
    limiter.set_rate(GLOBAL_BINANCE_RPS * (1 - ratio))
else:
    limiter.set_rate(GLOBAL_BINANCE_RPS)

Empty response arrays from the public BAPI search (EP-0) trigger a separate soft rate limit detector. After EMPTY_RESPONSE_THRESHOLD consecutive empty results, the limiter adds a cooldown before the next search request.

Best Practices

Price-Only Updates

Send only advNo + price in EP-7. Each extra field adds Binance validation load and risks error 187049.

Fresh Timestamps on Retry

On -1021 / -1022 errors, always rebuild the request with a new timestamp after syncing server time. Reusing the old signed URL will fail again.

Respect Concurrency Limit

GLOBAL_MAX_CONCURRENT is a semaphore, not a queue. Requests that cannot acquire a slot immediately are deferred, not dropped.

Stealth Headers for BAPI

The public BAPI search (EP-0) soft rate limits on repeated identical User-Agent strings. Rotate browser-like User-Agent headers to avoid detection.

All Environment Variables

All rate limiting behaviour is configurable via environment variables. Defaults are tuned for safe production operation with a single Binance account.

VariableDefaultDescription
GLOBAL_BINANCE_RPS5.0Global token bucket RPS
GLOBAL_MAX_CONCURRENT8Max concurrent Binance requests
GLOBAL_CB_THRESHOLD15Circuit breaker failure threshold
GLOBAL_CB_COOLDOWN45.0Circuit breaker cooldown (seconds)
GLOBAL_CB_HALF_OPEN_REQUESTS1Requests allowed in half-open state
LIMITER_GLOBAL_RPS4.5Per-ad global RPS across all ads
LIMITER_PER_AD_RPS1.5Per-ad max RPS
WEIGHT_BACKOFF_THRESHOLD0.8Weight usage ratio triggering backoff
EMPTY_RESPONSE_THRESHOLD3Empty responses before soft rate limit detection
RETRY_BUDGET5Max retry attempts per call
ERROR_187XXX_RETRY_SLEEP_SEC0.3Sleep before retry on 187xxx errors
BINANCE_RETRY_SYNC_ON_1000falsePer-method flag: sync time on -1000 errors
C2C_AD_NOT_FOUND_TTL_SEC60Negative 404 cache TTL
C2C_AD_NOT_FOUND_CACHE_MAX1024Max entries in 404 cache
ENGINE_BINANCE_RANGE_TTL_SEC300Dynamic price range cache TTL