Front-end Rate Limiting

Rate limiting is not only a very common front-end interview question, but also a pretty common problem that you want to solve in day to day software.

Fair Game for Interviews

There are a few approaches that I can think of.

Write a rateLimit decorator that takes a function and rate limit configurations and then returns a rate-limited function. For example, a function that should only be called X times within Y minutes.

A slightly more simple one is to rate limit the function using global variables or class variables in scope. Basically you increment a counter every time you call the function, and you have a setInterval function that clears it.

What if you want to do an exponential backoff on failure?

Exponential backoff, meaning wait 1, 2, 4, 8ms every time you encounter failure, and then to reset on success. Well now you have to track 2 pieces of state. On failure, you can clear the interval, queue all batched requests, and call setTimeout, where the wait time is tracked via a global or class function. This is actually reasonably complicated, and will probably require some code to demonstrate.

Here's a simplified example I found: https://gist.github.com/mattheworiordan/1084831

In Practice

In practice, you'd only want to do this if you have some kind of polling mechanism that requires an exponential backoff, or you'd more likely want to disable user action until the request returns. If you're ever in the situation where you're simultaneously firing off hundreds of requests, you might want to reconsolidate your API. But short of that...

Today I found this gem that actually works quite well in practice.

Promise.resolve(myArray).map(myfunction, {concurrency: 3});

This will basically enable you rate-limit iterating over a collection of XHRs and sending them out 3 at a time. The number of outstanding unresolved promises is configurable via the concurrency option. Such a gem. =)

Further Reading

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末哗总,一起剝皮案震驚了整個濱河市赚窃,隨后出現(xiàn)的幾起案子缝裤,更是在濱河造成了極大的恐慌芯杀,老刑警劉巖拴袭,帶你破解...
    沈念sama閱讀 221,635評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異哟楷,居然都是意外死亡爱沟,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評論 3 399
  • 文/潘曉璐 我一進店門奸攻,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蒜危,“玉大人,你說我怎么就攤上這事睹耐》蓿” “怎么了?”我有些...
    開封第一講書人閱讀 168,083評論 0 360
  • 文/不壞的土叔 我叫張陵硝训,是天一觀的道長响委。 經(jīng)常有香客問我,道長窖梁,這世上最難降的妖魔是什么赘风? 我笑而不...
    開封第一講書人閱讀 59,640評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮纵刘,結果婚禮上邀窃,老公的妹妹穿的比我還像新娘。我一直安慰自己假哎,他們只是感情好瞬捕,可當我...
    茶點故事閱讀 68,640評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著舵抹,像睡著了一般肪虎。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上掏父,一...
    開封第一講書人閱讀 52,262評論 1 308
  • 那天笋轨,我揣著相機與錄音,去河邊找鬼赊淑。 笑死爵政,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的陶缺。 我是一名探鬼主播钾挟,決...
    沈念sama閱讀 40,833評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼饱岸!你這毒婦竟也來了掺出?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,736評論 0 276
  • 序言:老撾萬榮一對情侶失蹤苫费,失蹤者是張志新(化名)和其女友劉穎汤锨,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體百框,經(jīng)...
    沈念sama閱讀 46,280評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡闲礼,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,369評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了铐维。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片柬泽。...
    茶點故事閱讀 40,503評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖嫁蛇,靈堂內(nèi)的尸體忽然破棺而出锨并,到底是詐尸還是另有隱情,我是刑警寧澤睬棚,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布第煮,位于F島的核電站,受9級特大地震影響抑党,放射性物質發(fā)生泄漏空盼。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,870評論 3 333
  • 文/蒙蒙 一新荤、第九天 我趴在偏房一處隱蔽的房頂上張望揽趾。 院中可真熱鬧,春花似錦苛骨、人聲如沸篱瞎。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽俐筋。三九已至,卻和暖如春严衬,著一層夾襖步出監(jiān)牢的瞬間澄者,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留粱挡,地道東北人赠幕。 一個月前我還...
    沈念sama閱讀 48,909評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像询筏,于是被迫代替她去往敵國和親榕堰。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,512評論 2 359

推薦閱讀更多精彩內(nèi)容