@@ -62,6 +62,7 @@ type Session struct {
6262 sessionId int64
6363 trans thrift.TTransport
6464 requestStatementId int64
65+ protocolFactory thrift.TProtocolFactory
6566}
6667
6768type endPoint struct {
@@ -83,7 +84,6 @@ func (s *Session) Open(enableRPCCompression bool, connectionTimeoutInMs int) err
8384 s .config .ConnectRetryMax = DefaultConnectRetryMax
8485 }
8586
86- var protocolFactory thrift.TProtocolFactory
8787 var err error
8888
8989 // in thrift 0.14.1, this func returns two values; in thrift 0.15.0, it returns one.
@@ -99,13 +99,10 @@ func (s *Session) Open(enableRPCCompression bool, connectionTimeoutInMs int) err
9999 return err
100100 }
101101 }
102- if enableRPCCompression {
103- protocolFactory = thrift .NewTCompactProtocolFactory ()
104- } else {
105- protocolFactory = thrift .NewTBinaryProtocolFactoryDefault ()
106- }
107- iprot := protocolFactory .GetProtocol (s .trans )
108- oprot := protocolFactory .GetProtocol (s .trans )
102+ s .protocolFactory = getProtocolFactory (enableRPCCompression )
103+ iprot := s .protocolFactory .GetProtocol (s .trans )
104+ oprot := s .protocolFactory .GetProtocol (s .trans )
105+
109106 s .client = rpc .NewIClientRPCServiceClient (thrift .NewTStandardClient (iprot , oprot ))
110107 req := rpc.TSOpenSessionReq {ClientProtocol : rpc .TSProtocolVersion_IOTDB_SERVICE_PROTOCOL_V3 , ZoneId : s .config .TimeZone , Username : s .config .UserName ,
111108 Password : & s .config .Password }
@@ -147,16 +144,11 @@ func (s *Session) OpenCluster(enableRPCCompression bool) error {
147144 s .config .ConnectRetryMax = DefaultConnectRetryMax
148145 }
149146
150- var protocolFactory thrift.TProtocolFactory
151147 var err error
152148
153- if enableRPCCompression {
154- protocolFactory = thrift .NewTCompactProtocolFactory ()
155- } else {
156- protocolFactory = thrift .NewTBinaryProtocolFactoryDefault ()
157- }
158- iprot := protocolFactory .GetProtocol (s .trans )
159- oprot := protocolFactory .GetProtocol (s .trans )
149+ s .protocolFactory = getProtocolFactory (enableRPCCompression )
150+ iprot := s .protocolFactory .GetProtocol (s .trans )
151+ oprot := s .protocolFactory .GetProtocol (s .trans )
160152 s .client = rpc .NewIClientRPCServiceClient (thrift .NewTStandardClient (iprot , oprot ))
161153 req := rpc.TSOpenSessionReq {ClientProtocol : rpc .TSProtocolVersion_IOTDB_SERVICE_PROTOCOL_V3 , ZoneId : s .config .TimeZone , Username : s .config .UserName ,
162154 Password : & s .config .Password }
@@ -170,14 +162,22 @@ func (s *Session) OpenCluster(enableRPCCompression bool) error {
170162 return err
171163}
172164
173- func (s * Session ) Close () (r * common.TSStatus , err error ) {
165+ func getProtocolFactory (enableRPCCompression bool ) thrift.TProtocolFactory {
166+ if enableRPCCompression {
167+ return thrift .NewTCompactProtocolFactoryConf (& thrift.TConfiguration {})
168+ } else {
169+ return thrift .NewTBinaryProtocolFactoryConf (& thrift.TConfiguration {})
170+ }
171+ }
172+
173+ func (s * Session ) Close () error {
174174 req := rpc .NewTSCloseSessionReq ()
175175 req .SessionId = s .sessionId
176- _ , err = s .client .CloseSession (context .Background (), req )
176+ _ , err : = s .client .CloseSession (context .Background (), req )
177177 if err != nil {
178- return nil , err
178+ return err
179179 }
180- return nil , s .trans .Close ()
180+ return s .trans .Close ()
181181}
182182
183183/*
@@ -1085,7 +1085,7 @@ func NewSession(config *Config) Session {
10851085 return Session {config : config }
10861086}
10871087
1088- func NewClusterSession (clusterConfig * ClusterConfig ) Session {
1088+ func NewClusterSession (clusterConfig * ClusterConfig ) ( Session , error ) {
10891089 session := Session {}
10901090 node := endPoint {}
10911091 for i := 0 ; i < len (clusterConfig .NodeUrls ); i ++ {
@@ -1113,9 +1113,9 @@ func NewClusterSession(clusterConfig *ClusterConfig) Session {
11131113 }
11141114 }
11151115 if ! session .trans .IsOpen () {
1116- log . Fatal ( "No Server Can Connect " )
1116+ return session , fmt . Errorf ( "no server can connect " )
11171117 }
1118- return session
1118+ return session , nil
11191119}
11201120
11211121func (s * Session ) initClusterConn (node endPoint ) error {
@@ -1148,10 +1148,8 @@ func (s *Session) initClusterConn(node endPoint) error {
11481148 s .config .ConnectRetryMax = DefaultConnectRetryMax
11491149 }
11501150
1151- var protocolFactory thrift.TProtocolFactory
1152- protocolFactory = thrift .NewTBinaryProtocolFactoryDefault ()
1153- iprot := protocolFactory .GetProtocol (s .trans )
1154- oprot := protocolFactory .GetProtocol (s .trans )
1151+ iprot := s .protocolFactory .GetProtocol (s .trans )
1152+ oprot := s .protocolFactory .GetProtocol (s .trans )
11551153 s .client = rpc .NewIClientRPCServiceClient (thrift .NewTStandardClient (iprot , oprot ))
11561154 req := rpc.TSOpenSessionReq {ClientProtocol : rpc .TSProtocolVersion_IOTDB_SERVICE_PROTOCOL_V3 , ZoneId : s .config .TimeZone , Username : s .config .UserName ,
11571155 Password : & s .config .Password }
0 commit comments