-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy paththriftclient.go
45 lines (37 loc) · 1.01 KB
/
thriftclient.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
/*
* Copyright © 2017 Xiao Zhang <[email protected]>.
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file.
*/
package turbo
import (
"github.com/apache/thrift/lib/go/thrift"
)
type thriftClient struct {
thriftService map[string]interface{}
transport thrift.TTransport
factory thrift.TProtocolFactory
}
func (t *thriftClient) init(addr string, clientCreator thriftClientCreator) {
if t.thriftService != nil {
return
}
log.Debugf("connecting thrift addr: %s", addr)
t.connect(addr)
t.thriftService = clientCreator(t.transport, t.factory)
}
func (t *thriftClient) connect(hostPort string) {
tSocket, err := thrift.NewTSocket(hostPort)
logPanicIf(err)
t.transport, err = thrift.NewTTransportFactory().GetTransport(tSocket)
logPanicIf(err)
err = t.transport.Open()
logPanicIf(err)
t.factory = thrift.NewTBinaryProtocolFactoryDefault()
}
func (t *thriftClient) close() error {
if t.transport == nil {
return nil
}
return t.transport.Close()
}