@@ -26,7 +26,6 @@ import (
2626 "errors"
2727 "fmt"
2828 "log"
29- "net"
3029 "reflect"
3130 "sort"
3231 "strings"
@@ -68,6 +67,7 @@ type Config struct {
6867 sqlDialect string
6968 Version Version
7069 Database string
70+ TLSConfig * TLSConfig
7171}
7272
7373type Session struct {
@@ -100,13 +100,10 @@ func (s *Session) Open(enableRPCCompression bool, connectionTimeoutInMs int) err
100100
101101 var err error
102102
103- // In thrift 0.14.1, this func returns two values; in newer versions, it returns one.
104- s .trans = thrift .NewTSocketConf (net .JoinHostPort (s .config .Host , s .config .Port ), & thrift.TConfiguration {
105- ConnectTimeout : time .Duration (connectionTimeoutInMs ) * time .Millisecond , // Use 0 for no timeout
106- })
107- // s.trans = thrift.NewTFramedTransport(s.trans) // deprecated
108- tmp_conf := thrift.TConfiguration {MaxFrameSize : thrift .DEFAULT_MAX_FRAME_SIZE }
109- s .trans = thrift .NewTFramedTransportConf (s .trans , & tmp_conf )
103+ s .trans , err = newTransport (s .config .Host , s .config .Port , connectionTimeoutInMs , s .config .TLSConfig )
104+ if err != nil {
105+ return err
106+ }
110107 if ! s .trans .IsOpen () {
111108 err = s .trans .Open ()
112109 if err != nil {
@@ -154,6 +151,7 @@ type ClusterConfig struct {
154151 ConnectRetryMax int
155152 sqlDialect string
156153 Database string
154+ TLSConfig * TLSConfig
157155}
158156
159157func (s * Session ) OpenCluster (enableRPCCompression bool ) error {
@@ -1328,24 +1326,23 @@ func newClusterSessionWithSqlDialect(clusterConfig *ClusterConfig) (Session, err
13281326 var err error
13291327 for i := range session .endPointList {
13301328 ep := session .endPointList [i ]
1331- session .trans = thrift .NewTSocketConf (net .JoinHostPort (ep .Host , ep .Port ), & thrift.TConfiguration {
1332- ConnectTimeout : time .Duration (0 ), // Use 0 for no timeout
1333- })
1334- // session.trans = thrift.NewTFramedTransport(session.trans) // deprecated
1335- tmp_conf := thrift.TConfiguration {MaxFrameSize : thrift .DEFAULT_MAX_FRAME_SIZE }
1336- session .trans = thrift .NewTFramedTransportConf (session .trans , & tmp_conf )
1329+ session .trans , err = newTransport (ep .Host , ep .Port , 0 , clusterConfig .TLSConfig )
1330+ if err != nil {
1331+ log .Println (err )
1332+ continue
1333+ }
13371334 if ! session .trans .IsOpen () {
13381335 err = session .trans .Open ()
13391336 if err != nil {
13401337 log .Println (err )
13411338 } else {
13421339 session .config = getConfig (ep .Host , ep .Port ,
1343- clusterConfig .UserName , clusterConfig .Password , clusterConfig .FetchSize , clusterConfig .TimeZone , clusterConfig .ConnectRetryMax , clusterConfig .Database , clusterConfig .sqlDialect )
1340+ clusterConfig .UserName , clusterConfig .Password , clusterConfig .FetchSize , clusterConfig .TimeZone , clusterConfig .ConnectRetryMax , clusterConfig .Database , clusterConfig .sqlDialect , clusterConfig . TLSConfig )
13441341 break
13451342 }
13461343 }
13471344 }
1348- if ! session .trans .IsOpen () {
1345+ if session . trans == nil || ! session .trans .IsOpen () {
13491346 return session , fmt .Errorf ("no server can connect" )
13501347 }
13511348 return session , nil
@@ -1354,18 +1351,14 @@ func newClusterSessionWithSqlDialect(clusterConfig *ClusterConfig) (Session, err
13541351func (s * Session ) initClusterConn (node endPoint ) error {
13551352 var err error
13561353
1357- s .trans = thrift .NewTSocketConf (net .JoinHostPort (node .Host , node .Port ), & thrift.TConfiguration {
1358- ConnectTimeout : time .Duration (0 ), // Use 0 for no timeout
1359- })
1360- if err == nil {
1361- // s.trans = thrift.NewTFramedTransport(s.trans) // deprecated
1362- tmp_conf := thrift.TConfiguration {MaxFrameSize : thrift .DEFAULT_MAX_FRAME_SIZE }
1363- s .trans = thrift .NewTFramedTransportConf (s .trans , & tmp_conf )
1364- if ! s .trans .IsOpen () {
1365- err = s .trans .Open ()
1366- if err != nil {
1367- return err
1368- }
1354+ s .trans , err = newTransport (node .Host , node .Port , 0 , s .config .TLSConfig )
1355+ if err != nil {
1356+ return err
1357+ }
1358+ if ! s .trans .IsOpen () {
1359+ err = s .trans .Open ()
1360+ if err != nil {
1361+ return err
13691362 }
13701363 }
13711364
@@ -1398,7 +1391,7 @@ func (s *Session) initClusterConn(node endPoint) error {
13981391 return err
13991392}
14001393
1401- func getConfig (host string , port string , userName string , passWord string , fetchSize int32 , timeZone string , connectRetryMax int , database string , sqlDialect string ) * Config {
1394+ func getConfig (host string , port string , userName string , passWord string , fetchSize int32 , timeZone string , connectRetryMax int , database string , sqlDialect string , tlsConfig * TLSConfig ) * Config {
14021395 return & Config {
14031396 Host : host ,
14041397 Port : port ,
@@ -1409,6 +1402,7 @@ func getConfig(host string, port string, userName string, passWord string, fetch
14091402 ConnectRetryMax : connectRetryMax ,
14101403 sqlDialect : sqlDialect ,
14111404 Database : database ,
1405+ TLSConfig : tlsConfig ,
14121406 }
14131407}
14141408
0 commit comments