日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

调用lambda_如何使用Lambda调用上下文动态设置超时

發(fā)布時(shí)間:2023/11/29 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 调用lambda_如何使用Lambda调用上下文动态设置超时 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

調(diào)用lambda

by Yan Cui

崔燕

如何使用Lambda調(diào)用上下文動(dòng)態(tài)設(shè)置超時(shí) (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,您不得不在服務(wù)器端使用較短的超時(shí):

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

    API網(wǎng)關(guān)在所有集成點(diǎn)上的最大超時(shí)為29秒

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

    無(wú)服務(wù)器框架對(duì)AWS Lambda函數(shù)使用默認(rèn)值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.

但是,你要通過(guò)lambda函數(shù)的冷啟動(dòng)時(shí)間的影響有限。 而且,您無(wú)法控制API網(wǎng)關(guān)添加多少開(kāi)銷。 因此,您從調(diào)用函數(shù)遇到的實(shí)際延遲遠(yuǎn)比您想象的要難預(yù)測(cè)。

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響應(yīng)導(dǎo)致調(diào)用函數(shù)超時(shí)。 這對(duì)用戶體驗(yàn)有負(fù)面影響。 相反,我們應(yīng)該在調(diào)用函數(shù)超時(shí)之前停止等待響應(yīng)。

“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.”

“超時(shí)策略的目標(biāo)是為HTTP請(qǐng)求提供成功最佳機(jī)會(huì),前提是這樣做不會(huì)導(dǎo)致調(diào)用函數(shù)本身出錯(cuò)。”

- Me- 我

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

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

  • 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.

    太短了,您將不會(huì)給請(qǐng)求獲得成功的最佳機(jī)會(huì)。 例如,調(diào)用中還剩下5s,但是超時(shí)設(shè)置為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.

    太長(zhǎng)了,您冒著使請(qǐng)求超時(shí)的調(diào)用函數(shù)的風(fēng)險(xiǎn)。 例如,調(diào)用中還剩下5s,但是超時(shí)設(shè)置為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,

在函數(shù)調(diào)用期間我們經(jīng)常執(zhí)行多個(gè)HTTP請(qǐng)求這一事實(shí)使事情變得更加復(fù)雜。 例如,

  • read from DynamoDB

    從DynamoDB讀取

  • perform business logic on the data

    對(duì)數(shù)據(jù)執(zhí)行業(yè)務(wù)邏輯
  • save the update to DynamoDB

    將更新保存到DynamoDB

  • publish an event to Kinesis

    向Kinesis發(fā)布事件

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

    讓我們看一下兩種選擇超時(shí)值的常見(jiàn)方法,以及它們的不足之處。

    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.

    我建議不要基于這些方法, 而應(yīng)根據(jù)剩余的調(diào)用時(shí)間來(lái)設(shè)置請(qǐng)求超時(shí) 。 如果發(fā)生故障,我們還應(yīng)該保留一些時(shí)間來(lái)執(zhí)行恢復(fù)步驟

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

    您可以通過(guò)context對(duì)象找出當(dāng)前調(diào)用中還剩下多少時(shí)間。

    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.

    例如,如果一個(gè)函數(shù)的timeout為6s,而調(diào)用的timeout為1s。 如果我們保留500毫秒用于恢復(fù),那么剩下4.5秒鐘的時(shí)間來(lái)等待HTTP響應(yīng)。

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

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

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

      允許請(qǐng)求根據(jù)我們剩下的實(shí)際調(diào)用時(shí)間獲得成功的最佳機(jī)會(huì)
    • 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.

      防止響應(yīng)緩慢導(dǎo)致功能超時(shí),這為我們提供了執(zhí)行恢復(fù)操作的機(jī)會(huì)。

    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?

    但是,這些請(qǐng)求超時(shí)后您將要做什么? 由于您無(wú)法完成所需執(zhí)行的任何操作,您是否仍將不得不以HTTP錯(cuò)誤進(jìn)行響應(yīng)?

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

    至少,恢復(fù)操作應(yīng)包括:

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

      使用盡可能多的上下文記錄超時(shí)事件。 例如,請(qǐng)求目標(biāo),超時(shí)值, 相關(guān)性ID和請(qǐng)求對(duì)象。

    • 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自定義指標(biāo),以便在情況升級(jí)時(shí)可以對(duì)其進(jìn)行監(jiān)視并向團(tuán)隊(duì)發(fā)出警報(bào)

    • 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 :-)”

      在響應(yīng)正文中返回應(yīng)用程序錯(cuò)誤代碼和原始請(qǐng)求ID。 然后,客戶端應(yīng)用程序可以顯示一條用戶友好的消息,例如“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.

    在某些 情況下,您還可以使用后備功能更優(yōu)雅地恢復(fù)。

    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的猬庫(kù)支持通過(guò)它使用大量的Command模式回退的幾種口味。 我建議閱讀其Wiki頁(yè)面 ,因?yàn)槟抢镉写罅坑杏玫男畔⒑拖敕ā?

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

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

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

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

    For exam-ple,

    例如,

  • exe-cute a DynamoDB read inside CommandA

    執(zhí)行CommandA內(nèi)部讀取的DynamoDB

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

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

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

    如果沒(méi)有緩存的響應(yīng),則CommandB將失敗并觸發(fā)自己的getFallback方法

  • Exe-cute CommandC, which returns a stubbed response

    執(zhí)行CommandC ,它返回存根響應(yīng)

  • 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.

    如果還沒(méi)有,請(qǐng)檢查Hystrix 。 大多數(shù)被烤成豪豬的模式可以在我們的無(wú)服務(wù)器應(yīng)用程序,幫助輕易采取使他們更適應(yīng)故障。

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

    調(diào)用lambda

    創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

    總結(jié)

    以上是生活随笔為你收集整理的调用lambda_如何使用Lambda调用上下文动态设置超时的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。