This repository has been archived by the owner on Jun 8, 2021. It is now read-only.
/
highway_client.go
executable file
·93 lines (78 loc) · 2.02 KB
/
highway_client.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package highway
import (
"context"
"sync"
"github.com/go-chassis/go-chassis/core/client"
"github.com/go-chassis/go-chassis/core/common"
"github.com/go-chassis/go-chassis/core/invocation"
)
//const timeout
const (
//Name is a variable of type string
Name = "highway"
DefaultConnectTimeOut = 60
DefaultSendTimeOut = 300
)
//highwayClient
//Deprecated
type highwayClient struct {
once sync.Once
opts client.Options
reqMutex sync.Mutex // protects following
}
//NewHighwayClient is a function
//Deprecated
func NewHighwayClient(options client.Options) (client.ProtocolClient, error) {
rc := &highwayClient{
once: sync.Once{},
opts: options,
}
c := client.ProtocolClient(rc)
return c, nil
}
func (c *highwayClient) String() string {
return "highway_client"
}
func (c *highwayClient) Close() error {
return nil
}
// GetOptions method return opts
func (c *highwayClient) GetOptions() client.Options {
return c.opts
}
// ReloadConfigs
func (c *highwayClient) ReloadConfigs(opts client.Options) {
c.opts = opts
}
func invocation2Req(inv *invocation.Invocation) *Request {
highwayReq := &Request{}
highwayReq.MsgID = uint64(int(GenerateMsgID()))
highwayReq.MethodName = inv.OperationID
highwayReq.Schema = inv.SchemaID
highwayReq.Arg = inv.Args
highwayReq.SvcName = inv.MicroServiceName
return highwayReq
}
func (c *highwayClient) Call(ctx context.Context, addr string, inv *invocation.Invocation, rsp interface{}) error {
connParams := &ConnParams{}
connParams.TLSConfig = c.opts.TLSConfig
connParams.Addr = addr
connParams.Timeout = DefaultConnectTimeOut
baseClient, err := CachedClients.GetClient(connParams)
if err != nil {
return err
}
tmpRsp := &Response{0, Ok, "", 0, rsp, nil}
highwayReq := invocation2Req(inv)
//Current only twoway
highwayReq.TwoWay = true
highwayReq.Attachments = common.FromContext(ctx)
err = baseClient.Send(highwayReq, tmpRsp, DefaultSendTimeOut)
if err != nil {
return err
}
return nil
}
func init() {
client.InstallPlugin(Name, NewHighwayClient)
}