Removing uncessary functions which previously handled initializing thrift protocol. Changed how which protocol is chosen based upon configuration.
This commit is contained in:
parent
ecd59f7a8d
commit
d67b8ca1d7
1 changed files with 43 additions and 71 deletions
114
realis.go
114
realis.go
|
@ -50,22 +50,22 @@ type Client struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type clientConfig struct {
|
type clientConfig struct {
|
||||||
username, password string
|
username, password string
|
||||||
url string
|
url string
|
||||||
timeout time.Duration
|
timeout time.Duration
|
||||||
binTransport, jsonTransport bool
|
transportProtocol TransportProtocol
|
||||||
cluster *Cluster
|
cluster *Cluster
|
||||||
backoff Backoff
|
backoff Backoff
|
||||||
transport thrift.TTransport
|
transport thrift.TTransport
|
||||||
protoFactory thrift.TProtocolFactory
|
protoFactory thrift.TProtocolFactory
|
||||||
logger *LevelLogger
|
logger *LevelLogger
|
||||||
InsecureSkipVerify bool
|
InsecureSkipVerify bool
|
||||||
certsPath string
|
certsPath string
|
||||||
clientKey, clientCert string
|
clientKey, clientCert string
|
||||||
options []ClientOption
|
options []ClientOption
|
||||||
debug bool
|
debug bool
|
||||||
trace bool
|
trace bool
|
||||||
zkOptions []ZKOpt
|
zkOptions []ZKOpt
|
||||||
}
|
}
|
||||||
|
|
||||||
var defaultBackoff = Backoff{
|
var defaultBackoff = Backoff{
|
||||||
|
@ -75,6 +75,14 @@ var defaultBackoff = Backoff{
|
||||||
Jitter: 0.1,
|
Jitter: 0.1,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TransportProtocol int
|
||||||
|
|
||||||
|
const (
|
||||||
|
unset TransportProtocol = iota
|
||||||
|
json
|
||||||
|
binary
|
||||||
|
)
|
||||||
|
|
||||||
type ClientOption func(*clientConfig)
|
type ClientOption func(*clientConfig)
|
||||||
|
|
||||||
// clientConfig sets for options in clientConfig.
|
// clientConfig sets for options in clientConfig.
|
||||||
|
@ -118,13 +126,13 @@ func ZKUrl(url string) ClientOption {
|
||||||
|
|
||||||
func ThriftJSON() ClientOption {
|
func ThriftJSON() ClientOption {
|
||||||
return func(config *clientConfig) {
|
return func(config *clientConfig) {
|
||||||
config.jsonTransport = true
|
config.transportProtocol = json
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ThriftBinary() ClientOption {
|
func ThriftBinary() ClientOption {
|
||||||
return func(config *clientConfig) {
|
return func(config *clientConfig) {
|
||||||
config.binTransport = true
|
config.transportProtocol = binary
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -243,11 +251,6 @@ func NewClient(options ...ClientOption) (*Client, error) {
|
||||||
|
|
||||||
config.logger.DebugPrintln("Number of options applied to clientConfig: ", len(options))
|
config.logger.DebugPrintln("Number of options applied to clientConfig: ", len(options))
|
||||||
|
|
||||||
// Set default Transport to JSON if needed.
|
|
||||||
if !config.jsonTransport && !config.binTransport {
|
|
||||||
config.jsonTransport = true
|
|
||||||
}
|
|
||||||
|
|
||||||
var url string
|
var url string
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
@ -279,21 +282,23 @@ func NewClient(options ...ClientOption) (*Client, error) {
|
||||||
return nil, errors.Wrap(err, "unable to create realis object, invalid url")
|
return nil, errors.Wrap(err, "unable to create realis object, invalid url")
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.jsonTransport {
|
switch config.transportProtocol {
|
||||||
trans, err := newTJSONTransport(url, config.timeout, config)
|
case binary:
|
||||||
if err != nil {
|
|
||||||
return nil, NewTemporaryError(errors.Wrap(err, "error creating realis"))
|
|
||||||
}
|
|
||||||
config.transport = trans
|
|
||||||
config.protoFactory = thrift.NewTJSONProtocolFactory()
|
|
||||||
|
|
||||||
} else if config.binTransport {
|
|
||||||
trans, err := newTBinTransport(url, config.timeout, config)
|
trans, err := newTBinTransport(url, config.timeout, config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, NewTemporaryError(errors.Wrap(err, "error creating realis"))
|
return nil, NewTemporaryError(errors.Wrap(err, "error creating realis"))
|
||||||
}
|
}
|
||||||
config.transport = trans
|
config.transport = trans
|
||||||
config.protoFactory = thrift.NewTBinaryProtocolFactoryDefault()
|
config.protoFactory = thrift.NewTBinaryProtocolFactoryDefault()
|
||||||
|
case json:
|
||||||
|
fallthrough
|
||||||
|
default:
|
||||||
|
trans, err := newTJSONTransport(url, config.timeout, config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, NewTemporaryError(errors.Wrap(err, "error creating realis"))
|
||||||
|
}
|
||||||
|
config.transport = trans
|
||||||
|
config.protoFactory = thrift.NewTJSONProtocolFactory()
|
||||||
}
|
}
|
||||||
|
|
||||||
config.logger.Printf("gorealis clientConfig url: %+v\n", url)
|
config.logger.Printf("gorealis clientConfig url: %+v\n", url)
|
||||||
|
@ -368,7 +373,12 @@ func defaultTTransport(url string, timeout time.Duration, config *clientConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
trans, err := thrift.NewTHttpClientWithOptions(url,
|
trans, err := thrift.NewTHttpClientWithOptions(url,
|
||||||
thrift.THttpClientOptions{Client: &http.Client{Timeout: timeout, Transport: &transport, Jar: jar}})
|
thrift.THttpClientOptions{
|
||||||
|
Client: &http.Client{
|
||||||
|
Timeout: timeout,
|
||||||
|
Transport: &transport,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "error creating transport")
|
return nil, errors.Wrap(err, "error creating transport")
|
||||||
|
@ -381,44 +391,6 @@ func defaultTTransport(url string, timeout time.Duration, config *clientConfig)
|
||||||
return trans, nil
|
return trans, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a default configuration of the transport layer, requires a URL to test connection with.
|
|
||||||
// Uses HTTP Post as transport layer and Thrift JSON as the wire protocol by default.
|
|
||||||
func newDefaultConfig(url string, timeout time.Duration, config *clientConfig) (*clientConfig, error) {
|
|
||||||
return newTJSONConfig(url, timeout, config)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Creates a realis clientConfig object using HTTP Post and Thrift JSON protocol to communicate with Aurora.
|
|
||||||
func newTJSONConfig(url string, timeout time.Duration, config *clientConfig) (*clientConfig, error) {
|
|
||||||
trans, err := defaultTTransport(url, timeout, config)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(err, "error creating realis clientConfig")
|
|
||||||
}
|
|
||||||
|
|
||||||
httpTrans := (trans).(*thrift.THttpClient)
|
|
||||||
httpTrans.SetHeader("Content-Type", "application/x-thrift")
|
|
||||||
httpTrans.SetHeader("User-Agent", "gorealis v"+VERSION)
|
|
||||||
|
|
||||||
return &clientConfig{transport: trans, protoFactory: thrift.NewTJSONProtocolFactory()}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Creates a realis clientConfig clientConfig using HTTP Post and Thrift Binary protocol to communicate with Aurora.
|
|
||||||
func newTBinaryConfig(url string, timeout time.Duration, config *clientConfig) (*clientConfig, error) {
|
|
||||||
trans, err := defaultTTransport(url, timeout, config)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(err, "error creating realis clientConfig")
|
|
||||||
}
|
|
||||||
|
|
||||||
httpTrans := (trans).(*thrift.THttpClient)
|
|
||||||
httpTrans.DelHeader("Content-Type") // Workaround for using thrift HttpPostClient
|
|
||||||
|
|
||||||
httpTrans.SetHeader("Accept", "application/vnd.apache.thrift.binary")
|
|
||||||
httpTrans.SetHeader("Content-Type", "application/vnd.apache.thrift.binary")
|
|
||||||
httpTrans.SetHeader("User-Agent", "gorealis v"+VERSION)
|
|
||||||
|
|
||||||
return &clientConfig{transport: trans, protoFactory: thrift.NewTBinaryProtocolFactoryDefault()}, nil
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func basicAuth(username, password string) string {
|
func basicAuth(username, password string) string {
|
||||||
auth := username + ":" + password
|
auth := username + ":" + password
|
||||||
return base64.StdEncoding.EncodeToString([]byte(auth))
|
return base64.StdEncoding.EncodeToString([]byte(auth))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue