forked from uber/tchannel-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
153 lines (131 loc) · 4.31 KB
/
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright (c) 2015 Uber Technologies, Inc.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package thrift
import (
"github.com/apache/thrift/lib/go/thrift"
"github.com/uber/tchannel-go"
"golang.org/x/net/context"
)
// client implements TChanClient and makes outgoing Thrift calls.
type client struct {
ch *tchannel.Channel
sc *tchannel.SubChannel
serviceName string
opts ClientOptions
}
// ClientOptions are options to customize the client.
type ClientOptions struct {
// HostPort specifies a specific server to hit.
HostPort string
}
// NewClient returns a Client that makes calls over the given tchannel to the given Hyperbahn service.
func NewClient(ch *tchannel.Channel, serviceName string, opts *ClientOptions) TChanClient {
client := &client{
ch: ch,
sc: ch.GetSubChannel(serviceName),
serviceName: serviceName,
}
if opts != nil {
client.opts = *opts
}
return client
}
func (c *client) startCall(ctx context.Context, method string, callOptions *tchannel.CallOptions) (*tchannel.OutboundCall, error) {
if c.opts.HostPort != "" {
return c.ch.BeginCall(ctx, c.opts.HostPort, c.serviceName, method, callOptions)
}
return c.sc.BeginCall(ctx, method, callOptions)
}
func writeArgs(call *tchannel.OutboundCall, headers map[string]string, req thrift.TStruct) error {
writer, err := call.Arg2Writer()
if err != nil {
return err
}
if err := writeHeaders(writer, headers); err != nil {
return err
}
if err := writer.Close(); err != nil {
return err
}
writer, err = call.Arg3Writer()
if err != nil {
return err
}
wp := getProtocolWriter(writer)
if err := req.Write(wp.protocol); err != nil {
thriftProtocolPool.Put(wp)
return err
}
thriftProtocolPool.Put(wp)
return writer.Close()
}
// readResponse reads the response struct into resp, and returns:
// (response headers, whether there was an application error, unexpected error).
func readResponse(response *tchannel.OutboundCallResponse, resp thrift.TStruct) (map[string]string, bool, error) {
reader, err := response.Arg2Reader()
if err != nil {
return nil, false, err
}
headers, err := readHeaders(reader)
if err != nil {
return nil, false, err
}
if err := reader.Close(); err != nil {
return nil, false, err
}
success := !response.ApplicationError()
reader, err = response.Arg3Reader()
if err != nil {
return headers, success, err
}
wp := getProtocolReader(reader)
if err := resp.Read(wp.protocol); err != nil {
thriftProtocolPool.Put(wp)
return headers, success, err
}
thriftProtocolPool.Put(wp)
return headers, success, reader.Close()
}
func (c *client) Call(ctx Context, thriftService, methodName string, req, resp thrift.TStruct) (bool, error) {
var (
headers = ctx.Headers()
respHeaders map[string]string
isOK bool
)
err := c.ch.RunWithRetry(ctx, func(ctx context.Context, rs *tchannel.RequestState) error {
respHeaders, isOK = nil, false
call, err := c.startCall(ctx, thriftService+"::"+methodName, &tchannel.CallOptions{
Format: tchannel.Thrift,
RequestState: rs,
})
if err != nil {
return err
}
if err := writeArgs(call, headers, req); err != nil {
return err
}
respHeaders, isOK, err = readResponse(call.Response(), resp)
return err
})
if err != nil {
return false, err
}
ctx.SetResponseHeaders(respHeaders)
return isOK, nil
}