@@ -127,9 +127,26 @@ func (c *APIClient) GetQueryID() string {
127127 return fmt .Sprintf ("%s.%d" , c .SessionID , c .QuerySeq )
128128}
129129
130+ func (c * APIClient ) NeedSticky () bool {
131+ if c .sessionState != nil {
132+ return c .sessionState .NeedSticky
133+ }
134+ return false
135+ }
136+
137+ func (c * APIClient ) NeedKeepAlive () bool {
138+ if c .sessionState != nil {
139+ return c .sessionState .NeedKeepAlive
140+ }
141+ return false
142+ }
143+
130144func NewAPIHttpClientFromConfig (cfg * Config ) * http.Client {
145+ jar := NewIgnoreDomainCookieJar ()
146+ jar .SetCookies (nil , []* http.Cookie {{Name : "cookie_enabled" , Value : "true" }})
131147 cli := & http.Client {
132148 Timeout : cfg .Timeout ,
149+ Jar : jar ,
133150 }
134151 if cfg .EnableOpenTelemetry {
135152 cli .Transport = otelhttp .NewTransport (http .DefaultTransport )
@@ -148,7 +165,7 @@ func NewAPIClientFromConfig(cfg *Config) *APIClient {
148165
149166 // if role is set in config, we'd prefer to limit it as the only effective role,
150167 // so you could limit the privileges by setting a role with limited privileges.
151- // however this can be overridden by executing `SET SECONDARY ROLES ALL` in the
168+ // however, this can be overridden by executing `SET SECONDARY ROLES ALL` in the
152169 // query.
153170 // secondaryRoles now have two viable values:
154171 // - nil: means enabling ALL the granted roles of the user
@@ -202,7 +219,7 @@ func initAccessTokenLoader(cfg *Config) AccessTokenLoader {
202219 return nil
203220}
204221
205- func (c * APIClient ) doRequest (ctx context.Context , method , path string , req interface {}, resp interface {}, respHeaders * http.Header ) error {
222+ func (c * APIClient ) doRequest (ctx context.Context , method , path string , req interface {}, needSticky bool , resp interface {}, respHeaders * http.Header ) error {
206223 if c .doRequestFunc != nil {
207224 return c .doRequestFunc (method , path , req , resp )
208225 }
@@ -226,6 +243,9 @@ func (c *APIClient) doRequest(ctx context.Context, method, path string, req inte
226243 maxRetries := 2
227244 for i := 1 ; i <= maxRetries ; i ++ {
228245 headers , err := c .makeHeaders (ctx )
246+ if needSticky {
247+ headers .Set (DatabendQueryStickyNode , c .NodeID )
248+ }
229249 if err != nil {
230250 return errors .Wrap (err , "failed to make request headers" )
231251 }
@@ -484,7 +504,7 @@ func (c *APIClient) startQueryRequest(ctx context.Context, request *QueryRequest
484504 respHeaders http.Header
485505 )
486506 err := c .doRetry (func () error {
487- return c .doRequest (ctx , "POST" , path , request , & resp , & respHeaders )
507+ return c .doRequest (ctx , "POST" , path , request , c . NeedSticky (), & resp , & respHeaders )
488508 }, Query ,
489509 )
490510 if err != nil {
@@ -520,7 +540,7 @@ func (c *APIClient) PollQuery(ctx context.Context, nextURI string) (*QueryRespon
520540 var result QueryResponse
521541 err := c .doRetry (
522542 func () error {
523- return c .doRequest (ctx , "GET" , nextURI , nil , & result , nil )
543+ return c .doRequest (ctx , "GET" , nextURI , nil , true , & result , nil )
524544 },
525545 Page ,
526546 )
@@ -539,7 +559,7 @@ func (c *APIClient) KillQuery(ctx context.Context, response *QueryResponse) erro
539559 ctx , cancel := context .WithTimeout (ctx , 30 * time .Second )
540560 defer cancel ()
541561 _ = c .doRetry (func () error {
542- return c .doRequest (ctx , "GET" , response .KillURI , nil , nil , nil )
562+ return c .doRequest (ctx , "GET" , response .KillURI , nil , true , nil , nil )
543563 }, Kill ,
544564 )
545565 }
@@ -551,7 +571,7 @@ func (c *APIClient) CloseQuery(ctx context.Context, response *QueryResponse) err
551571 ctx , cancel := context .WithTimeout (ctx , 30 * time .Second )
552572 defer cancel ()
553573 _ = c .doRetry (func () error {
554- return c .doRequest (ctx , "GET" , response .FinalURI , nil , nil , nil )
574+ return c .doRequest (ctx , "GET" , response .FinalURI , nil , true , nil , nil )
555575 }, Final ,
556576 )
557577 }
@@ -723,6 +743,14 @@ func (c *APIClient) UploadToStageByAPI(ctx context.Context, stage *StageLocation
723743 return nil
724744}
725745
746+ func (c * APIClient ) Logout (ctx context.Context ) error {
747+ if c .NeedKeepAlive () {
748+ req := & struct {}{}
749+ return c .doRequest (ctx , "POST" , "/v1/session/logout/" , req , c .NeedSticky (), nil , nil )
750+ }
751+ return nil
752+ }
753+
726754func randRouteHint () string {
727755 charset := "abcdef0123456789"
728756 b := make ([]byte , 16 )
0 commit comments