简体   繁体   English

如何处理 Python API 中的速率限制

[英]How To Handle Rate Limiting In Python API

I am making a SOAP request to Salesforce Marketing Cloud API to retrieve Data Extension object records in batches of 2500 from an object who has 1,000,000 records total.我正在向 Salesforce Marketing Cloud API 发出 SOAP 请求,以从总共有 1,000,000 条记录的 object 中以 2500 条为一批检索数据扩展 object 条记录。 Unfortunately the SDK is not an option due to an existing bug with respect to DE name lengths.不幸的是,SDK 不是一个选项,因为与 DE 名称长度有关的现有错误。 Approximately halfway through, after running fine, I receive a 500 response and subsequent fail.大约中途,运行正常后,我收到 500 响应,随后失败。 From what I've read, it's due to throttling:据我所知,这是由于节流造成的:

https://developer.salesforce.com/docs/marketing/marketing-cloud/guide/rate-limiting-errors-codes.html https://developer.salesforce.com/docs/marketing/marketing-cloud/guide/rate-limiting-errors-codes.html

IN GENERAL, What is a good way to handle rate limiting within API requests?一般来说,在 API 请求中处理速率限制的好方法是什么? Do you add sleep timers to give more time in-between subsequent calls.您是否添加睡眠定时器以在后续通话之间提供更多时间。 Or would you catch the 500 response (or whatever error response you're receiving) and then add a sleep timer from there?或者您会捕获 500 响应(或您收到的任何错误响应)然后从那里添加一个睡眠定时器吗?

It's my first time running into this, so I appreciate any guidance / resources and am happy to test from there.这是我第一次遇到这个问题,所以我很感激任何指导/资源,并且很乐意从那里进行测试。

First and foremost, do not let your app make direct SOAP calls.首先,不要让您的应用直接拨打 SOAP 电话。 Rather, write a utility library, and have the app always go through that.相反,编写一个实用程序库,并让应用程序始终通过它 go。

Consult the documentation to learn the relevant rate limit policies.查阅文档以了解相关的速率限制策略。 You need to know how many requests per hour would be acceptable.您需要知道每小时有多少请求是可以接受的。

Your library should maintain a global in-memory data structure which remembers recent transaction times and counts.您的图书馆应维护一个全局内存数据结构,该结构会记住最近的交易时间和计数。 That is, you should know how busy you've been lately, and how close to the edge of the rate limit you are skating.也就是说,你应该知道你最近有多忙,以及你滑冰的速度限制的边缘有多近。 Consider sleeping a bit if you're getting close.如果你离得很近,考虑睡一会儿。 Definitely log everything, with timestamps.绝对记录所有内容,并带有时间戳。


In the URL you cited, pay careful attention to the 2nd timestamp:在您引用的 URL 中,请注意第二个时间戳:

        <wsu:Created>2017-02-01T19:07:24Z</wsu:Created>
        <wsu:Expires>2017-02-01T19:12:24Z</wsu:Expires>

When your library eventually gets an error response, it should use that 2nd (future.) timestamp to intelligently choose how long to sleep.当您的图书馆最终收到错误响应时,它应该使用第二个(未来)时间戳来智能地选择睡眠时间。 It is telling us when the ban will be lifted -- attempts before then will just be wasted.它告诉我们禁令何时解除——在那之前的尝试只会白费。

Sleep slightly longer than needed.睡眠时间略长于所需时间。 Your goal should be to always remain slightly below the limit.您的目标应该是始终保持略低于限制。


Write unit / integration tests for this.为此编写单元/集成测试。 The overall system is large and complex.整个系统庞大而复杂。 You need to be confident you understand it and can predict its behavior.您需要确信您了解它并且可以预测它的行为。 Sometimes there will be server-side changes, and you don't want those to create hard-to-diagnose Heisenbugs you must track down.有时服务器端会发生变化,而您不希望这些变化造成您必须追踪的难以诊断的Heisenbug

You need tests that deliberately trigger server-side rate limiting and then successfully recover.您需要有意触发服务器端速率限制然后成功恢复的测试。 You also need tests that demonstrate high transaction rate which remains a little below the limit.您还需要测试证明高交易率仍然略低于限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM