This repository was archived by the owner on Feb 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 299
Expand file tree
/
Copy pathclient.go
More file actions
51 lines (42 loc) · 1.19 KB
/
client.go
File metadata and controls
51 lines (42 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"context"
"github.com/stack-labs/stack-rpc"
proto "github.com/stack-labs/stack-rpc-tutorials/examples/proto/service/rpc"
"github.com/stack-labs/stack-rpc/client"
log "github.com/stack-labs/stack-rpc/logger"
"github.com/stack-labs/stack-rpc/pkg/metadata"
)
type logWrapper struct {
client.Client
}
func (l *logWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
log.Infof("[Call] 请求服务:%s.%s", req.Service(), req.Endpoint())
newMd, b := metadata.FromContext(ctx)
if !b {
newMd = metadata.Metadata{}
}
newMd["client-wrapped"] = "client-wrapped-value"
ctx = metadata.NewContext(ctx, newMd)
return l.Client.Call(ctx, req, rsp)
}
func NewClientWrapper() client.Wrapper {
return func(cli client.Client) client.Client {
return &logWrapper{cli}
}
}
func main() {
service := stack.NewService(
stack.Name("wrap.client.cli"),
stack.WrapClient(
NewClientWrapper(),
),
)
service.Init()
cl := proto.NewGreeterService("wrap.client.service", service.Client())
rsp, err := cl.Hello(context.Background(), &proto.HelloRequest{Name: "StackLabs"})
if err != nil {
panic(err)
}
log.Info(rsp.Greeting)
}