/
client.go
52 lines (44 loc) · 1.17 KB
/
client.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
package hbase
import (
"io"
"sync"
"time"
"git.apache.org/thrift.git/lib/go/thrift"
)
var (
defaultTimeout = 30 * time.Second
defaultBufferSize = 8192
)
// clientCloser implements the interface of Hbase
type clientCloser struct {
*HbaseClient
// mu is used to lock the underlying conn to ensure thread safe
mu *sync.Mutex
conn *thrift.TSocket
}
func (c *clientCloser) Close() error {
return c.conn.Close()
}
// ThriftClientFactory is an thrift Client factory which creates a connection
// that uses a thrift codec.
func ThriftClientFactory(addr string) func() (io.Closer, error) {
return func() (io.Closer, error) {
var transport thrift.TTransport
socketTransport, err := thrift.NewTSocketTimeout(addr, defaultTimeout)
if err != nil {
return nil, err
}
transport = thrift.NewTFramedTransport(
thrift.NewTBufferedTransport(socketTransport, defaultBufferSize))
protocol := thrift.NewTBinaryProtocolFactoryDefault()
if err := transport.Open(); err != nil {
return nil, err
}
client := NewHbaseClientFactory(transport, protocol)
return &clientCloser{
mu: &sync.Mutex{},
conn: socketTransport,
HbaseClient: client,
}, nil
}
}