-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy paththrift-rpc.go
59 lines (46 loc) · 1.13 KB
/
thrift-rpc.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
// Copyright (C) 2015 Foursquare Labs Inc.
package main
import (
"net"
"github.com/apache/thrift/lib/go/thrift"
)
type TRpcServer struct {
server *thrift.TSimpleServer
socket *thrift.TServerSocket
}
func NewTRpcServer(listen string, handler thrift.TProcessor, prot thrift.TProtocolFactory) (*TRpcServer, error) {
transport, err := thrift.NewTServerSocket(listen)
if err != nil {
return nil, err
}
server := thrift.NewTSimpleServer4(
handler,
transport,
thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory()),
prot,
)
return &TRpcServer{server, transport}, nil
}
func (t *TRpcServer) Listen() error {
return t.socket.Listen()
}
func (t *TRpcServer) Serve() error {
return t.server.AcceptLoop()
}
func (t *TRpcServer) Addr() net.Addr {
return t.socket.Addr()
}
func (t *TRpcServer) Close() {
t.server.Stop()
t.socket.Close()
}
func (t *TRpcServer) GetClientTransport() (thrift.TTransport, error) {
transport, err := thrift.NewTSocket(t.Addr().String())
if err != nil {
return nil, err
}
if err = transport.Open(); err != nil {
return nil, err
}
return thrift.NewTFramedTransport(transport), nil
}