forked from pinterest/bender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thrift.go
44 lines (35 loc) · 1.02 KB
/
thrift.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
package bender
import (
"git.apache.org/thrift.git/lib/go/thrift"
"bytes"
)
type ThriftClientExecutor func(*Request, thrift.TTransport) error
func NewThriftRequestExec(tFac thrift.TTransportFactory, clientExec ThriftClientExecutor) RequestExecutor {
addr := "asterix-staging001:3636"
return func(_ int64, request *Request) error {
socket, err := thrift.NewTSocket(addr)
if err != nil {
return err
}
defer socket.Close()
transport := tFac.GetTransport(socket)
if err := transport.Open(); err != nil {
return err
}
defer transport.Close()
return clientExec(request, transport)
}
}
func DeserializeThriftMessage(buf *bytes.Buffer, ts thrift.TStruct) (string, thrift.TMessageType, int32, error) {
transport := thrift.NewStreamTransportR(buf)
protocol := thrift.NewTBinaryProtocol(transport, false, false)
name, typeId, seqId, err := protocol.ReadMessageBegin()
if err != nil {
return "", 0, 0, err
}
err = ts.Read(protocol)
if err != nil {
return "", 0, 0, err
}
return name, typeId, seqId, nil
}