調用lambda_如何使用Lambda調用上下文動態設置超時

調用lambda

by Yan Cui

崔燕

如何使用Lambda調用上下文動態設置超時 (How to set timeouts dynamically using Lambda invocation context)

With API Gate-way and Lamb-da, you’re forced to use short time-outs on the serv-er-side:

使用API?? Gateway和Lambda,您不得不在服務器端使用較短的超時:

  • API Gate-way has a 29s max time-out on all inte-gra-tion points

    API網關在所有集成點上的最大超時為29秒

  • The Server-less frame-work uses a default of 6s for AWS Lamb-da func-tions

    無服務器框架對AWS Lambda函數使用默認值6s

How-ev-er, you have lim-it-ed influ-ence over a Lamb-da function’s cold start time. And you have no con-trol over how much over-head API Gate-way adds. So the actu-al laten-cy you’d expe-ri-ence from a call-ing func-tion is far less pre-dictable than you might think.

但是,你要通過lambda函數的冷啟動時間的影響有限。 而且,您無法控制API網關添加多少開銷。 因此,您從調用函數遇到的實際延遲遠比您想象的要難預測。

We don’t want a slow HTTP respons-e to cause the call-ing func-tion to time-out. This has a negative impact on the user experience. Instead, we should stop wait-ing for a response before the call-ing func-tion times out.

我們不希望緩慢的HTTP響應導致調用函數超時。 這對用戶體驗有負面影響。 相反,我們應該在調用函數超時之前停止等待響應。

“The goal of the time-out strat-e-gy is to give HTTP requests the best chance to suc-ceed, pro-vid-ed that doing so does not cause the call-ing func-tion itself to err.”

“超時策略的目標是為HTTP請求提供成功最佳機會,前提是這樣做不會導致調用函數本身出錯。”

- Me
- 我

Most of the time, I see folks use fixed time-out val-ues — but it’s often tricky to decide:

在大多數情況下,我看到人們使用固定的超時值-但要決定通常很棘手:

  • Too short, and you won’t give the request the best chance to suc-ceed. For example, there’s 5s left in the invo-ca-tion, but the time-out is set to 3s.

    太短了,您將不會給請求獲得成功最佳機會。 例如,調用中還剩下5s,但是超時設置為3s。

  • Too long, and you run the risk of let-ting the request time-out the call-ing func-tion. For example, there’s 5s left in the invo-ca-tion but the time-out is set to 6s.

    太長了,您冒著使請求超時的調用函數的風險。 例如,調用中還剩下5s,但是超時設置為6s。

Things are fur-ther com-pli-cat-ed by the fact that we often per-form more than one HTTP request dur-ing a func-tion invo-ca-tion. For example,

在函數調用期間我們經常執行多個HTTP請求這一事實使事情變得更加復雜。 例如,

  1. read from DynamoDB

    從DynamoDB讀取

  2. perform business logic on the data

    對數據執行業務邏輯
  3. save the update to DynamoDB

    將更新保存到DynamoDB

  4. publish an event to Kinesis

    向Kinesis發布事件

Let’s look at two com-mon approach-es for pick-ing time-out val-ues, and where they fall short.

讓我們看一下兩種選擇超時值的常見方法,以及它們的不足之處。

Instead of following these approaches, I propose we should set the request time-out based on the amount of invo-ca-tion time left. We should also reserve some time to per-form recovery steps in the event of failures.

我建議不要基于這些方法, 而應根據剩余的調用時間設置請求超時 。 如果發生故障,我們還應該保留一些時間來執行恢復步驟

You can find out how much time is left in the cur-rent invo-ca-tion through the context object.

您可以通過context對象找出當前調用中還剩下多少時間。

For exam-ple, if a function’s timeout is 6s, and we’re 1s into the invocation. If we reserve 500ms for recov-ery, then that leaves us with 4.5s to wait for a HTTP response.

例如,如果一個函數的timeout為6s,而調用的timeout為1s。 如果我們保留500毫秒用于恢復,那么剩下4.5秒鐘的時間來等待HTTP響應。

With this approach, we get the best of both worlds:

通過這種方法,我們可以兩全其美:

  • Allow requests the best chance to suc-ceed based on the actu-al amount of invo-ca-tion time we have left

    允許請求根據我們剩下的實際調用時間獲得成功的最佳機會
  • Pre-vent slow respons-es from tim-ing out the func-tion, which gives us a window of oppor-tu-ni-ty to per-form recov-ery actions.

    防止響應緩慢導致功能超時,這為我們提供了執行恢復操作的機會。

But what are you going to do after you time out these requests? Aren’t you still going to have to respond with a HTTP error, since you couldn’t fin-ish what-ev-er oper-a-tions you need-ed to per-form?

但是,這些請求超時您將要做什么? 由于您無法完成所需執行的任何操作,您是否仍將不得不以HTTP錯誤進行響應?

At the min-i-mum, the recov-ery actions should include:

至少,恢復操作應包括:

  • Log the timeout incident with as much context as possible. For example, request target, timeout value, correlation IDs, and the request object.

    使用盡可能多的上下文記錄超時事件。 例如,請求目標,超時值, 相關性ID和請求對象。

  • Track cus-tom met-rics for serviceX.timedout so it can be mon-i-tored and the team can be alert-ed if the sit-u-a-tion esca-lates

    跟蹤serviceX.timedout自定義指標,以便在情況升級時可以對其進行監視并向團隊發出警報

  • Return an appli-ca-tion error code and the original request ID in the response body. The client app can then dis-play a user-friend-ly mes-sage like “Oops, looks like this fea-ture is cur-rent-ly unavail-able, please try again lat-er. If this is urgent, please con-tact us at xxx@domain.com and quote the request ID f19a7dca. Thank you for your coop-er-a-tion :-)”

    在響應正文中返回應用程序錯誤代碼和原始請求ID。 然后,客戶端應用程序可以顯示一條用戶友好的消息,例如“Oops, looks like this feature is currently unavailable, please try again later. If this is urgent, please contact us at xxx@domain.com and quote the request ID f19a7dca. Thank you for your cooperation :-)” “Oops, looks like this feature is currently unavailable, please try again later. If this is urgent, please contact us at xxx@domain.com and quote the request ID f19a7dca. Thank you for your cooperation :-)”

{   "errorCode": 10021,   "requestId": "f19a7dca",   "message": "service X timed out" }

In some cas-es, you can also recov-er even more grace-ful-ly using fall-backs.

某些 情況下,您還可以使用后備功能更優雅地恢復。

Netflix’s Hys-trix library sup-ports sev-er-al flavors of fall-backs via the Com-mand pat-tern it employs heav-i-ly. I recommend reading its wiki page, as there is tons of use-ful infor-ma-tion and ideas there.

Netflix的猬庫支持通過它使用大量的Command模式回退的幾種口味。 我建議閱讀其Wiki頁面 ,因為那里有大量有用的信息和想法。

Every Hystrix com-mand lets you spec-i-fy a fall-back action.

每個Hystrix命令都可以讓您指定一個后備操作。

You can also chain the fall-back togeth-er by chain-ing com-mands via their respec-tive getFallback meth-ods.

您還可以通過各自的getFallback方法將命令鏈接在一起,從而將后備鏈接鏈接在一起。

For exam-ple,

例如,

  1. exe-cute a DynamoDB read inside CommandA

    執行CommandA內部讀取的DynamoDB

  2. In the getFallback method, exe-cute CommandB which would return a pre-vi-ous-ly cached response if avail-able

    getFallback方法中,執行CommandB ,它將返回以前緩存的響應(如果有)

  3. If there is no cached response, then CommandB would fail and trig-ger its own getFallback method

    如果沒有緩存的響應,則CommandB將失敗并觸發自己的getFallback方法

  4. Exe-cute CommandC, which returns a stubbed response

    執行CommandC ,它返回存根響應

You should check out Hys-trix if you haven’t already. Most of the pat-terns that are baked into Hys-trix can be eas-i-ly adopt-ed in our server-less appli-ca-tions to help make them more resilient to fail-ures.

如果還沒有,請檢查Hystrix 。 大多數被烤成豪豬的模式可以在我們的無服務器應用程序,幫助輕易采取使他們更適應故障。

翻譯自: https://www.freecodecamp.org/news/how-to-set-timeouts-dynamically-using-lambda-invocation-context-3e78fa832a5b/

調用lambda

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/395020.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/395020.shtml
英文地址,請注明出處:http://en.pswp.cn/news/395020.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

php+tp框架+API,php - tp框架能開發API接口嗎

怪我咯2017-04-10 17:12:231樓TP的controller其實有很多種的HproseController,JsonRpcController,RestController,RpcController,YarController我拿RestController給你介紹一下RESTFul方法的操作方法定義主要區別在于,需要對請求類型和資源類型進行判斷,…

OTL翻譯(9) --常量的SQL語句

常量的SQL語句 一個沒有綁定變量的SQL語句、SQL語句塊或是存儲過程就被稱為常量的SQL語句。OTL通過一個靜態的函數來執行這樣的SQL語句。 例如: // static otl_cursor::direct_exec()otl_cursor::direct_exec(db, // connect object"create table test_tab(f1 …

[HNOI2016]網絡 樹鏈剖分,堆

[HNOI2016]網絡 LG傳送門 表示亂搞比正解難想。 整體二分很好想吧。 但是為了好寫快樂,我們選擇三個\(\log\)的亂搞。 先樹剖,線段樹套堆維護區間最大值。對于一次修改,如果是插入,就把樹上除了這條鏈的地方加上這個重要度&#x…

python壓縮文件為zip-python 壓縮文件為zip后刪除原文件

壓縮.log 文件為zip后刪除原文件 需要注意:本人作為小白,該腳本需要和.log在一起,后面有時間需要改正。 #!/usr/local/python/bin/python #-*-codingutf8 -*- import time import os import sys import zipfile N 7 #設置刪除多少天前的文件…

css text-align-last設置末尾文本對齊方式

text-align-last:auto | start | end | left | right | center | justify auto: 無特殊對齊方式。 left: 內容左對齊。 center: 內容居中對齊。 right: 內容右對齊。 justify: 內容兩端對齊。 start&#x…

后端開發除了編碼還要做什么_每個開發人員都應掌握的基本技能(除了編碼)

后端開發除了編碼還要做什么Whether you are learning to code, looking for a new job, or just want to improve your skills as a developer, you need to master the essential tools of team collaboration. These are as important as knowing how to code.無論您是學習編…

Python中的defaultdict方法

字典(dictionary)是Python中一種常用的數據類型。不同于其他由數字索引的序列,字典是用"鍵"(key)來索引的。通常表示為dict(key: val, ...),有以下特征: 鍵可以是任何不可變&#xff…

git撤銷commit 并保存之前的修改

撤銷并保留修改 參數 –soft # 先進行commit &#xff0c;之后后悔啦$ git commit -am "對首篇報告研究員字段改為author_name"執行git log $ git logcommit 3d6788f577faba5e1d408e372031c81beee79749Author: yous <yous.com>Date: Thu Dec 14 10:08:36 2017 …

php替換中文,PHP中文替換

//定義編碼header( Content-Type:text/html;charsetutf-8 );$wordsarray(我,你,他);$content"測一測我是不是違禁詞";$bannedgenerateRegularExpression($words);//檢查違禁詞$res_bannedcheck_words($banned,$content);write_html($content,$res_banned);/*** descr…

secoclient隧道保活超時或協商超時_推薦:承德市隧道led大屏廠家電話【聯豐智慧科技】...

通過為大型隧道施工建設搭建全覆蓋式的定位&#xff0c;可以有效施工的效率、項目現場的保障能力。安裝隧道門禁能解決哪些問題&#xff1f;近年來&#xff0c;我國交通建設正處于高速發展的階段&#xff0c;在交通建設中&#xff0c;工程安防工作也越發受到&#xff0c;越來越…

JavaScript Essentials:如何為循環而煩惱

by Zell Liew由Zell Liew JavaScript Essentials&#xff1a;如何為循環而煩惱 (JavaScript Essentials: how to wrap your head around for loops) Let’s say you want to run a function, bounceBall, four times. How would you do it? Like this?假設您要運行一次功能b…

python中的類的成員變量以及property函數

1 python類的各種變量 1.1 全局變量 在類外定義的變量。 1.2 類變量 定義在類里面&#xff0c;所有的函數外面的變量。這個變量只有一份&#xff0c;是所有的對象共有的。在類外用“類.”來引用。 1.3 實例變量 用self.xxx在類的任何函數中定義的變量就是實例變量。在類內用“s…

C++常用的系統函數

數學<math.h>&#xff1a; 1 三角函數 double sin (double); double cos (double); double tan (double); 2 反三角函數 double asin (double); 結果介于[-PI/2, PI/2] double acos (double); 結果介于[0, PI] double atan (double); 反正切(主值), 結果介于[-PI/2, PI/2…

網頁特效java代碼,美化網頁常用特效代碼

1&#xff0e;讓文字不停地滾動&#xff1c;MARQUEE&#xff1e;滾動文字&#xff1c;/MARQUEE&#xff1e;2&#xff0e;記錄并顯示網頁的最后修改時間&#xff1c;script languageJavaScript&#xff1e;document.write("最后更新時間: " document.lastModified …

作業,兩次實驗

實驗一&#xff1a; 1 編程打印5行的倒三角形&#xff0c;第一行打印9個*&#xff0c;第二行7個*&#xff0c;……第5行打印1個* #include<stdio.h>int main(){printf("*********\n *******\n *****\n ***\n *\n");return 0;} 總結 注意換行以及位置的…

javaweb和ajax使用查詢出來的數據做下拉菜單_區塊鏈瀏覽器實用指南篇:利用鏈上數據把握減半行情...

進入2020年&#xff0c;加密貨幣市場最熱的話題當屬“減半”了。在減半行情的推動下&#xff0c;以BTC為首的減半幣種展現出了極強的上行趨勢。如何抓住這一波行情&#xff0c;評估正確時機&#xff1f;當然&#xff0c;這個問題的答案可以說是爭議紛紛&#xff0c;但有一個參考…

純函數式編程語言_純功能編程語言如何改變您的生活。

純函數式編程語言by Andrea Zanin由Andrea Zanin 純功能編程語言如何改變您的生活。 (How a purely functional programming language can change your life.) I believe everyone should learn Haskell, even if you won’t use it in your work. It’s beautiful, and it ch…

Win10 教育版

Windows 10 版本 1607 引入了專為 K-12 機構的特有需求而設計的兩個版本&#xff1a;Windows 10 專業教育版和 Windows 10 教育版。 這些版本為不斷發展的 K-12 教育 IT 環境提供特定于教育的默認設置。Windows 10 專業教育版Windows 10 專業教育版基于 Windows 10 專業版的商業…

java中的排序方法,Java中的排序比較方式:自然排序和比較器排序

這里所說到的Java中的排序并不是指插入排序、希爾排序、歸并排序等具體的排序算法。而是指執行這些排序算法時&#xff0c;比較兩個對象“大小”的比較操作。我們很容易理解整型的 i>j 這樣的比較方式&#xff0c;但當我們對多個對象進行排序時&#xff0c;如何比較兩個對象…

ImageView縮放選項

ImageView.ScaleType 將圖片邊界縮放到所在view邊界時的縮放選項。 Options for scaling the bounds of an image to the bounds of this view. 不同選項含義 CENTER 居中&#xff0c;不縮放。 Center the image in the view, but perform no scaling. CENTER_CROP 居中&#x…