-
Notifications
You must be signed in to change notification settings - Fork 80
/
tchan-example.go
366 lines (292 loc) · 8.49 KB
/
tchan-example.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// @generated Code generated by thrift-gen. Do not modify.
// Package example is generated code used to make or handle TChannel calls using Thrift.
package example
import (
"fmt"
athrift "github.com/uber/tchannel-go/thirdparty/github.com/apache/thrift/lib/go/thrift"
"github.com/uber/tchannel-go/thrift"
)
// Interfaces for the service and client for the services defined in the IDL.
// TChanBase is the interface that defines the server handler and client interface.
type TChanBase interface {
BaseCall(ctx thrift.Context) error
}
// TChanFirst is the interface that defines the server handler and client interface.
type TChanFirst interface {
TChanBase
AppError(ctx thrift.Context) error
Echo(ctx thrift.Context, msg string) (string, error)
Healthcheck(ctx thrift.Context) (*HealthCheckRes, error)
}
// TChanSecond is the interface that defines the server handler and client interface.
type TChanSecond interface {
Test(ctx thrift.Context) error
}
// Implementation of a client and service handler.
type tchanBaseClient struct {
thriftService string
client thrift.TChanClient
}
func NewTChanBaseInheritedClient(thriftService string, client thrift.TChanClient) *tchanBaseClient {
return &tchanBaseClient{
thriftService,
client,
}
}
// NewTChanBaseClient creates a client that can be used to make remote calls.
func NewTChanBaseClient(client thrift.TChanClient) TChanBase {
return NewTChanBaseInheritedClient("Base", client)
}
func (c *tchanBaseClient) BaseCall(ctx thrift.Context) error {
var resp BaseBaseCallResult
args := BaseBaseCallArgs{}
success, err := c.client.Call(ctx, c.thriftService, "BaseCall", &args, &resp)
if err == nil && !success {
switch {
default:
err = fmt.Errorf("received no result or unknown exception for BaseCall")
}
}
return err
}
type tchanBaseServer struct {
handler TChanBase
}
// NewTChanBaseServer wraps a handler for TChanBase so it can be
// registered with a thrift.Server.
func NewTChanBaseServer(handler TChanBase) thrift.TChanServer {
return &tchanBaseServer{
handler,
}
}
func (s *tchanBaseServer) Service() string {
return "Base"
}
func (s *tchanBaseServer) Methods() []string {
return []string{
"BaseCall",
}
}
func (s *tchanBaseServer) Handle(ctx thrift.Context, methodName string, protocol athrift.TProtocol) (bool, athrift.TStruct, error) {
switch methodName {
case "BaseCall":
return s.handleBaseCall(ctx, protocol)
default:
return false, nil, fmt.Errorf("method %v not found in service %v", methodName, s.Service())
}
}
func (s *tchanBaseServer) handleBaseCall(ctx thrift.Context, protocol athrift.TProtocol) (bool, athrift.TStruct, error) {
var req BaseBaseCallArgs
var res BaseBaseCallResult
if err := req.Read(protocol); err != nil {
return false, nil, err
}
err :=
s.handler.BaseCall(ctx)
if err != nil {
return false, nil, err
} else {
}
return err == nil, &res, nil
}
type tchanFirstClient struct {
TChanBase
thriftService string
client thrift.TChanClient
}
func NewTChanFirstInheritedClient(thriftService string, client thrift.TChanClient) *tchanFirstClient {
return &tchanFirstClient{
NewTChanBaseInheritedClient(thriftService, client),
thriftService,
client,
}
}
// NewTChanFirstClient creates a client that can be used to make remote calls.
func NewTChanFirstClient(client thrift.TChanClient) TChanFirst {
return NewTChanFirstInheritedClient("First", client)
}
func (c *tchanFirstClient) AppError(ctx thrift.Context) error {
var resp FirstAppErrorResult
args := FirstAppErrorArgs{}
success, err := c.client.Call(ctx, c.thriftService, "AppError", &args, &resp)
if err == nil && !success {
switch {
default:
err = fmt.Errorf("received no result or unknown exception for AppError")
}
}
return err
}
func (c *tchanFirstClient) Echo(ctx thrift.Context, msg string) (string, error) {
var resp FirstEchoResult
args := FirstEchoArgs{
Msg: msg,
}
success, err := c.client.Call(ctx, c.thriftService, "Echo", &args, &resp)
if err == nil && !success {
switch {
default:
err = fmt.Errorf("received no result or unknown exception for Echo")
}
}
return resp.GetSuccess(), err
}
func (c *tchanFirstClient) Healthcheck(ctx thrift.Context) (*HealthCheckRes, error) {
var resp FirstHealthcheckResult
args := FirstHealthcheckArgs{}
success, err := c.client.Call(ctx, c.thriftService, "Healthcheck", &args, &resp)
if err == nil && !success {
switch {
default:
err = fmt.Errorf("received no result or unknown exception for Healthcheck")
}
}
return resp.GetSuccess(), err
}
type tchanFirstServer struct {
thrift.TChanServer
handler TChanFirst
}
// NewTChanFirstServer wraps a handler for TChanFirst so it can be
// registered with a thrift.Server.
func NewTChanFirstServer(handler TChanFirst) thrift.TChanServer {
return &tchanFirstServer{
NewTChanBaseServer(handler),
handler,
}
}
func (s *tchanFirstServer) Service() string {
return "First"
}
func (s *tchanFirstServer) Methods() []string {
return []string{
"AppError",
"Echo",
"Healthcheck",
"BaseCall",
}
}
func (s *tchanFirstServer) Handle(ctx thrift.Context, methodName string, protocol athrift.TProtocol) (bool, athrift.TStruct, error) {
switch methodName {
case "AppError":
return s.handleAppError(ctx, protocol)
case "Echo":
return s.handleEcho(ctx, protocol)
case "Healthcheck":
return s.handleHealthcheck(ctx, protocol)
case "BaseCall":
return s.TChanServer.Handle(ctx, methodName, protocol)
default:
return false, nil, fmt.Errorf("method %v not found in service %v", methodName, s.Service())
}
}
func (s *tchanFirstServer) handleAppError(ctx thrift.Context, protocol athrift.TProtocol) (bool, athrift.TStruct, error) {
var req FirstAppErrorArgs
var res FirstAppErrorResult
if err := req.Read(protocol); err != nil {
return false, nil, err
}
err :=
s.handler.AppError(ctx)
if err != nil {
return false, nil, err
} else {
}
return err == nil, &res, nil
}
func (s *tchanFirstServer) handleEcho(ctx thrift.Context, protocol athrift.TProtocol) (bool, athrift.TStruct, error) {
var req FirstEchoArgs
var res FirstEchoResult
if err := req.Read(protocol); err != nil {
return false, nil, err
}
r, err :=
s.handler.Echo(ctx, req.Msg)
if err != nil {
return false, nil, err
} else {
res.Success = &r
}
return err == nil, &res, nil
}
func (s *tchanFirstServer) handleHealthcheck(ctx thrift.Context, protocol athrift.TProtocol) (bool, athrift.TStruct, error) {
var req FirstHealthcheckArgs
var res FirstHealthcheckResult
if err := req.Read(protocol); err != nil {
return false, nil, err
}
r, err :=
s.handler.Healthcheck(ctx)
if err != nil {
return false, nil, err
} else {
res.Success = r
}
return err == nil, &res, nil
}
type tchanSecondClient struct {
thriftService string
client thrift.TChanClient
}
func NewTChanSecondInheritedClient(thriftService string, client thrift.TChanClient) *tchanSecondClient {
return &tchanSecondClient{
thriftService,
client,
}
}
// NewTChanSecondClient creates a client that can be used to make remote calls.
func NewTChanSecondClient(client thrift.TChanClient) TChanSecond {
return NewTChanSecondInheritedClient("Second", client)
}
func (c *tchanSecondClient) Test(ctx thrift.Context) error {
var resp SecondTestResult
args := SecondTestArgs{}
success, err := c.client.Call(ctx, c.thriftService, "Test", &args, &resp)
if err == nil && !success {
switch {
default:
err = fmt.Errorf("received no result or unknown exception for Test")
}
}
return err
}
type tchanSecondServer struct {
handler TChanSecond
}
// NewTChanSecondServer wraps a handler for TChanSecond so it can be
// registered with a thrift.Server.
func NewTChanSecondServer(handler TChanSecond) thrift.TChanServer {
return &tchanSecondServer{
handler,
}
}
func (s *tchanSecondServer) Service() string {
return "Second"
}
func (s *tchanSecondServer) Methods() []string {
return []string{
"Test",
}
}
func (s *tchanSecondServer) Handle(ctx thrift.Context, methodName string, protocol athrift.TProtocol) (bool, athrift.TStruct, error) {
switch methodName {
case "Test":
return s.handleTest(ctx, protocol)
default:
return false, nil, fmt.Errorf("method %v not found in service %v", methodName, s.Service())
}
}
func (s *tchanSecondServer) handleTest(ctx thrift.Context, protocol athrift.TProtocol) (bool, athrift.TStruct, error) {
var req SecondTestArgs
var res SecondTestResult
if err := req.Read(protocol); err != nil {
return false, nil, err
}
err :=
s.handler.Test(ctx)
if err != nil {
return false, nil, err
} else {
}
return err == nil, &res, nil
}