Skip to main content

A Python SDK for ny_sync: Distributed Token Bucket Rate Limiter

Project description

NySync

Any Data, Any Source, Any Destination.

NySync 是一个 AI-First 的数据同步与任务编排平台:用声明式任务把“任何数据源”持续、可靠地同步到“任何目的地”,并让 Agent 能够安全地生成、校验与运维这些任务。

当前进展(Milestone)

  • 已完成:分布式令牌桶限流(Python SDK,基于 Redis)
    • 实现包含:LuaTokenBucket(默认)、NumericTokenBucket(GCRA)、StringTokenBucket(Slot-based)
    • 支持 Dashboard 可视化与并发/批量场景验证
  • 主线目标:平台层的作业管理、调度编排、执行引擎与观测体系

快速开始(Python SDK):

import redis
from ny_sync import TokenBucket, RateUnit

r = redis.Redis(host="localhost", port=6379, db=0, decode_responses=True)
bucket = TokenBucket(r)

key = "demo"
capacity = 10
rate = 60 / RateUnit.MINUTE  # 每分钟 60 个

allowed = bucket.allow(key, capacity, rate)

提供分布式令牌桶限流,基于 Redis 实现。内置三种实现:

  • LuaTokenBucket(默认):并发能力强,适合通用场景
  • NumericTokenBucket(GCRA):高吞吐批量场景
  • StringTokenBucket(Slot-based):一个 Token 一个 String Key,便于峰值与历史可视化

安装

pip install ny_sync

快速开始

import redis
from ny_sync import TokenBucket, RateUnit, RateLimit, RateLimitExceeded

r = redis.Redis(host="localhost", port=6379, db=0, decode_responses=True)

# 手动使用(TokenBucket 是 RedisTokenBucket 的简化别名)
bucket = TokenBucket(r)
key = "demo"
capacity = 10  # 桶容量10个
rate = 60 / RateUnit.MINUTE  # 每分钟补充 60 个
allowed = bucket.allow(key, capacity, rate)


# 装饰器使用(RateLimit 是 RateLimitDecorator 的简化别名)
@RateLimit(r, "api", capacity, rate)
def my_api():
    return "ok"

可视化 Dashboard

运行简单的 Streamlit Dashboard:

# Windows
c:\project\ny_sync\python_sdk\run_dashboard.bat

主要导出

  • TokenBucket / RedisTokenBucket / LuaTokenBucket / NumericTokenBucket / StringTokenBucket
  • RateUnit(SECOND / MINUTE / HOUR / DAY / WEEK)
  • RateLimit / RateLimitExceeded

许可证

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ny_sync-0.1.1.tar.gz (19.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ny_sync-0.1.1-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

Details for the file ny_sync-0.1.1.tar.gz.

File metadata

  • Download URL: ny_sync-0.1.1.tar.gz
  • Upload date:
  • Size: 19.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for ny_sync-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5239799d2c328bfcc6e7a1dec71bd43a9522f38ab4a00228349c88c4b2f64506
MD5 8750504ccf97d9e4733c002539b7a7aa
BLAKE2b-256 919142f605b2a442e7d7edf3e9b0160f894ef73ecd74c882302ad50fb5d2639d

See more details on using hashes here.

File details

Details for the file ny_sync-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: ny_sync-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 19.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for ny_sync-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7419d1dc4386d8fc8f494cdd995205aac20e1a4195209c00494d25a80c5bbf51
MD5 5f9e5fa041bd539d4893f63a62883ec3
BLAKE2b-256 27ba82c264d1d3431cd463265e9c9946bebb2fe39e210bafa73274fd09ee5d03

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page