Skip to content

Commit 00ad41a

Browse files
authored
feat: generate random routehint to make load balancing easier (#134)
1 parent ed79d8a commit 00ad41a

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

restful.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ import (
99
"encoding/json"
1010
"fmt"
1111
"io"
12+
"math/rand"
1213
"mime/multipart"
1314
"net"
1415
"net/http"
1516
"strings"
1617
"time"
1718

18-
"github.com/google/uuid"
19-
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
20-
2119
"github.com/avast/retry-go"
20+
"github.com/google/uuid"
2221
"github.com/pkg/errors"
22+
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
2323
)
2424

2525
type AuthMethod string
@@ -178,6 +178,7 @@ func NewAPIClientFromConfig(cfg *Config) *APIClient {
178178
password: cfg.Password,
179179
sessionState: &sessionState,
180180
sessionStateRaw: &sessionStateRaw,
181+
routeHint: randRouteHint(),
181182

182183
accessTokenLoader: initAccessTokenLoader(cfg),
183184
statsTracker: cfg.StatsTracker,
@@ -476,7 +477,7 @@ func (c *APIClient) startQueryRequest(ctx context.Context, request *QueryRequest
476477
// fmt.Printf("start query %v %v\n", c.GetQueryID(), request.SQL)
477478

478479
if !c.inActiveTransaction() {
479-
c.routeHint = ""
480+
c.routeHint = randRouteHint()
480481
}
481482

482483
path := "/v1/query"
@@ -498,7 +499,7 @@ func (c *APIClient) startQueryRequest(ctx context.Context, request *QueryRequest
498499
// e.g. transaction state need to be updated if commit fail
499500
c.applySessionState(&resp)
500501
// save route hint for the next following http requests
501-
if len(respHeaders) > 0 {
502+
if len(respHeaders) > 0 && len(respHeaders.Get(DatabendRouteHintHeader)) > 0 {
502503
c.routeHint = respHeaders.Get(DatabendRouteHintHeader)
503504
}
504505
return &resp, nil
@@ -720,3 +721,12 @@ func (c *APIClient) UploadToStageByAPI(ctx context.Context, stage *StageLocation
720721

721722
return nil
722723
}
724+
725+
func randRouteHint() string {
726+
charset := "abcdef0123456789"
727+
b := make([]byte, 16)
728+
for i := range b {
729+
b[i] = charset[rand.Intn(len(charset))]
730+
}
731+
return string(b)
732+
}

0 commit comments

Comments
 (0)