Skip to content
Permalink
Browse files

超时&重试

  • Loading branch information...
hb-chen committed Oct 13, 2019
1 parent 703851f commit 2f0baa1f5117d44809be07d6435fe36aeb1b9990
Showing with 26 additions and 0 deletions.
  1. +11 −0 example/api/main.go
  2. +15 −0 example/srv/handler/example.go
@@ -3,6 +3,7 @@ package main
import (
"net"
"net/http"
"time"

"github.com/hb-go/micro-quick-start/example/api/client"
"github.com/hb-go/micro-quick-start/example/api/handler"
@@ -62,6 +63,16 @@ func main() {
panic(err)
}()

// 重试&超时
service.Init(
func(o *micro.Options) {
o.Client.Init(
mc.Retries(3),
mc.RequestTimeout(time.Millisecond*100),
)
},
)

// Register Handler
example.RegisterExampleHandler(
service.Server(),
@@ -2,17 +2,32 @@ package handler

import (
"context"
"time"

"github.com/micro/go-micro/errors"
"github.com/micro/go-micro/util/log"

example "github.com/hb-go/micro-quick-start/example/srv/proto/example"
)

type Example struct{}

var count int

// Call is a single request handler called via client.Call or the generated client code
func (e *Example) Call(ctx context.Context, req *example.Request, rsp *example.Response) error {
log.Log("Received Example.Call request")
count++
log.Log("Received Example.Call request, count: %v", count)

if count%4 == 0 {
log.Log("Received Example.Call request 错误")
return errors.New("go.micro.srv.example.call", "错误", 500)
}

log.Log("Received Example.Call request 超时")
time.Sleep(time.Millisecond * 100)

rsp.Msg = "Hello " + req.Name + " v1 value1"
return nil
}

0 comments on commit 2f0baa1

Please sign in to comment.
You can’t perform that action at this time.