-
Notifications
You must be signed in to change notification settings - Fork 80
/
tchan-hyperbahn.go
122 lines (100 loc) · 3.31 KB
/
tchan-hyperbahn.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
// @generated Code generated by thrift-gen. Do not modify.
// Package hyperbahn is generated code used to make or handle TChannel calls using Thrift.
package hyperbahn
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.
// TChanHyperbahn is the interface that defines the server handler and client interface.
type TChanHyperbahn interface {
Discover(ctx thrift.Context, query *DiscoveryQuery) (*DiscoveryResult_, error)
}
// Implementation of a client and service handler.
type tchanHyperbahnClient struct {
thriftService string
client thrift.TChanClient
}
func NewTChanHyperbahnInheritedClient(thriftService string, client thrift.TChanClient) *tchanHyperbahnClient {
return &tchanHyperbahnClient{
thriftService,
client,
}
}
// NewTChanHyperbahnClient creates a client that can be used to make remote calls.
func NewTChanHyperbahnClient(client thrift.TChanClient) TChanHyperbahn {
return NewTChanHyperbahnInheritedClient("Hyperbahn", client)
}
func (c *tchanHyperbahnClient) Discover(ctx thrift.Context, query *DiscoveryQuery) (*DiscoveryResult_, error) {
var resp HyperbahnDiscoverResult
args := HyperbahnDiscoverArgs{
Query: query,
}
success, err := c.client.Call(ctx, c.thriftService, "discover", &args, &resp)
if err == nil && !success {
switch {
case resp.NoPeersAvailable != nil:
err = resp.NoPeersAvailable
case resp.InvalidServiceName != nil:
err = resp.InvalidServiceName
default:
err = fmt.Errorf("received no result or unknown exception for discover")
}
}
return resp.GetSuccess(), err
}
type tchanHyperbahnServer struct {
handler TChanHyperbahn
}
// NewTChanHyperbahnServer wraps a handler for TChanHyperbahn so it can be
// registered with a thrift.Server.
func NewTChanHyperbahnServer(handler TChanHyperbahn) thrift.TChanServer {
return &tchanHyperbahnServer{
handler,
}
}
func (s *tchanHyperbahnServer) Service() string {
return "Hyperbahn"
}
func (s *tchanHyperbahnServer) Methods() []string {
return []string{
"discover",
}
}
func (s *tchanHyperbahnServer) Handle(ctx thrift.Context, methodName string, protocol athrift.TProtocol) (bool, athrift.TStruct, error) {
switch methodName {
case "discover":
return s.handleDiscover(ctx, protocol)
default:
return false, nil, fmt.Errorf("method %v not found in service %v", methodName, s.Service())
}
}
func (s *tchanHyperbahnServer) handleDiscover(ctx thrift.Context, protocol athrift.TProtocol) (bool, athrift.TStruct, error) {
var req HyperbahnDiscoverArgs
var res HyperbahnDiscoverResult
if err := req.Read(protocol); err != nil {
return false, nil, err
}
r, err :=
s.handler.Discover(ctx, req.Query)
if err != nil {
switch v := err.(type) {
case *NoPeersAvailable:
if v == nil {
return false, nil, fmt.Errorf("Handler for noPeersAvailable returned non-nil error type *NoPeersAvailable but nil value")
}
res.NoPeersAvailable = v
case *InvalidServiceName:
if v == nil {
return false, nil, fmt.Errorf("Handler for invalidServiceName returned non-nil error type *InvalidServiceName but nil value")
}
res.InvalidServiceName = v
default:
return false, nil, err
}
} else {
res.Success = r
}
return err == nil, &res, nil
}