Skip to main content

A Python SDK for AnySync: 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 any-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\anysync\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.0.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.0-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ny_sync-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 faf5e6af87072456cc51c6553738a8faaecdead32c2d716b08922a92c4587f2c
MD5 bba248af616166ded058049791c7f232
BLAKE2b-256 f22b1e3ea8843e097e632c5a8f3d0756e95c1c6ddd89ffca30ce3440ee7ecead

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ny_sync-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6c49ebfc0f3b0ab0e5ec8f92530fb2da73fda72acb66a9fdb1886e9d2b503313
MD5 13d68497a0d7939d809a3d2fe6198468
BLAKE2b-256 8efa10979f8f66336ee3a22ccef453496125d4d7bde4f3799a626025eef4d952

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