首页 > 其他分享 >Microservice- Resiliency patterns: Timeout Pattern

Microservice- Resiliency patterns: Timeout Pattern

时间:2023-11-19 10:15:57浏览次数:30  
标签:retry calls service gRPC Pattern three patterns context Microservice

Timeout Pattern

What is context.Context? The context in Go enables you to specify deadlines, cancellation signals, or key-value pairs available between processes.

 

context.WithDeadline requires a target date as a parameter, but there is an easier step: context.WithTimeout, a wrapper for WithDeadline that allows you to specify a timeout value instead of calculating the target date from the current time.

Both functions take a parent context, context.Background(), which in our case is an empty context without a deadline.

context.WithTimeout and context.WithDeadline both return two values: context and cancellation functions. You can call the cancellation function to halt any further execution within that context, as needed. More concretely, you can call three competing endpoints with the same context, then cancel the context for the remaining two when the first endpoint finishes and returns a value. This way, you do not have to wait for other endpoint responses that are no longer needed.

 

Another advantage of using context is propagating context information between ser vices. In gRPC, context is available on both the client and server sides. That means that if there are three services to fulfill an operation, the initial context can be propagated to the final one without extra context definition on the second and third services. For example, I know that the order operation should take at most three seconds to complete, and in our scenario, the Order service calls the Payment service, then the Shipping service. We can create a context with a three-second timeout and use it in three services (figure 6.2).

 

Retry Pattern

Transient faults occur when a momentary loss of service functionality self-corrects. The retry pattern in gRPC enables us to retry a failed call automatically and thus is perfect for transient faults such as these:
 Instant network failures
 Temporarily unavailable services
 Resource exhaustion of service due to load
gRPC has an interception mechanism that allows you to implement your interceptor and configure the gRPC client to apply the interceptor to all gRPC calls. Using the same strategy, we can execute interceptors on the gRPC server side before the request is passed to actual business logic. gRPC middleware is a good place to define the retry pattern and apply it to all gRPC calls on the client side instead of duplicating retry logic for each type of client call. Thanks to the gRPC ecosystem, a GitHub organization contains notable, community-maintained projects related to gRPC for resiliency. We will use the gRPC retry module (http://mng.bz/XNV9) of go-grpc-middleware (https://github.com/grpc-ecosystem/go-grpc-middleware) to apply retry logic to our gRPC calls.

标签:retry,calls,service,gRPC,Pattern,three,patterns,context,Microservice
From: https://www.cnblogs.com/zhangzhihui/p/17841645.html

相关文章

  • Microservice - Load Balancing (LB)
        ......
  • Microservice - Hexagonal Architecture
    Hexagonalarchitecture(https://alistair.cockburn.us/hexagonal-architecture/),proposedbyAlistairCockburnin2005,isanarchitecturalpatternthataimstobuild looselycoupledapplicationcomponentsthatcanbeconnectedviaportsand adapters.Inth......
  • Microservice - Data Consistency
    Tohavedataconsistencyinadistributedsystem, youhavetwooptions:atwo-phasecommit(2PC)andsaga. 2PCcoordinatesallthe processesthatformdistributedatomictransactionsanddetermineswhetherthey shouldbecommittedoraborted.  As......
  • EF报错:Unable to create an object of type 'XXXXXXX'. For the different patterns s
    这个是在EF迁移的时候报错: 解决方案:修改你的MyDbcontext:  代码如下:publicclassStoreDbContexttFactory:IDesignTimeDbContextFactory<‘你的类名’>{public‘你的类名’CreateDbContext(string[]args){varoptionsBuilder=newDbContextOptionsBuilder<‘......
  • 设计模式(Design Pattern)记忆
    创建型记忆口诀:创公园,但见愁创工原,单建抽创建型工厂方法FactoryMethod原型Prototype单例Singleton建造者Builder抽象工厂AbstractFactory结构型记忆口口诀:姐想外祖,世代装桥结享外组,适代装桥结构型:享元Flyweight外观Facade组合Composite适配器Adapter装饰Decorat......
  • 无涯教程-Clojure - re-pattern函数
    re-pattern返回java.util.regex.Pattern的实例。然后将其用于其他模式匹配方法。re-pattern-语法(repatternpat)参数   - "pat"是需要形成的pattern。返回值 - 类型为java.util.regex.Pattern的模式对象。re-pattern-示例(nsclojure.examples.example......
  • From monolithic application to microservice framework
    challenges:1.versioncontrol AftertestingthebranchA,pullthelatestcode(mergedB)->confilcts?noconflicts?->regressiontesting 2. changetogolanguage3.upgradethelibrarygo1.12-go1.134.onlyonetestingserverwaitingformulti......
  • ExcelPatternTool 开箱即用的Excel工具包现已发布!
    目录ExcelPatternTool功能特点:快速开始使用说明常规类型高级类型Importable注解Exportable注解IImportOption导入选项IExportOption导出选项单元格样式StyleMapping样式映射使用数据库作为数据源示例Sample1:不同类型字段导出Sample2:高级类型导入和导出Sample3:员工健康体检工具已知......
  • c: Visitor Pattern
     /***@filevalidator.h*@authoryourname([email protected])*@brief观察者模式VisitorPattern来源:C现代编程集成开发环境、设计模式、极限编程、测试驱动开发、重构、持续集成日.花井志生著,杨文轩译,人民邮电出版社*@version0.1*@date2023-10-21......
  • 【愚公系列】2023年10月 二十三种设计模式(十九)-观察者模式(Observer Pattern)
    ......