当前位置:网站首页>Grp: implement GRP timeout interceptor

Grp: implement GRP timeout interceptor

2022-06-24 01:47:00 Trespass

Introduce

How does this article describe rk-boot Quickly build gRPC Timeout interceptor .

What is? gRPC Timeout interceptor ?

Interceptors will intercept gRPC request , And return the timeout error according to the policy .

Please visit the following address for a complete tutorial :

install

go get github.com/rookie-ninja/rk-boot
go get github.com/rookie-ninja/rk-grpc

Quick start

Use rk-boot start-up gRPC service .

Support global timeout and API Timeout settings .

1. establish boot.yaml

boot.yaml File tell rk-boot How to start gRPC service .

In order to verify , We started commonService,commonService Contains a series of commonly used API, for example /rk/v1/gc.

Set the global timeout to 5 second , Give Way GC Timeout positioning for 1 millisecond ,GC Usually more than 1 millisecond .

---
grpc:
  - name: greeter                                   # Required
    port: 8080                                      # Required
    enabled: true                                   # Required
    commonService:
      enabled: true                                 # Optional, Enable common service for testing
    interceptors:
      timeout:
        enabled: true                               # Optional, default: false
        timeoutMs: 5000                             # Optional, default: 5000
        paths: 
          - path: "/rk.api.v1.RkCommonService/Gc"   # Optional, default: ""
            timeoutMs: 1                            # Optional, default: 5000

2. establish main.go

// Copyright (c) 2021 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
package main

import (
	"context"
	"github.com/rookie-ninja/rk-boot"
	_ "github.com/rookie-ninja/rk-grpc/boot"
)

// Application entrance.
func main() {
	// Create a new boot instance.
	boot := rkboot.NewBoot()

	// Bootstrap
	boot.Bootstrap(context.Background())

	// Wait for shutdown sig
	boot.WaitForShutdownSig(context.Background())
}

3. start-up main.go

$ go run main.go

4. verification

send out GC request .

$ grpcurl -plaintext localhost:8080 rk.api.v1.RkCommonService.Gc
ERROR:
  Code: Canceled
  Message: Request timed out!
  Details:
  1)	{"@type":"type.googleapis.com/rk.api.v1.ErrorDetail","code":1,"message":"[from-grpc] Request timed out!","status":"Canceled"}
$ curl -X GET localhost:8080/rk/v1/gc
{
    "error":{
        "code":408,
        "status":"Request Timeout",
        "message":"Request timed out!",
        "details":[
            {
                "code":1,
                "status":"Canceled",
                "message":"[from-grpc] Request timed out!"
            }
        ]
    }
}
原网站

版权声明
本文为[Trespass ]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/11/20211115030541369X.html