Skip to content

Commit e350e84

Browse files
authored
fix: pass queryid in client (#87)
1 parent ad2c9d2 commit e350e84

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

connection.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"time"
1515

1616
"github.com/avast/retry-go"
17+
"github.com/google/uuid"
1718
)
1819

1920
const (
@@ -37,6 +38,8 @@ type DatabendConn struct {
3738
func (dc *DatabendConn) exec(ctx context.Context, query string, args ...driver.Value) (driver.Result, error) {
3839
respCh := make(chan QueryResponse)
3940
errCh := make(chan error)
41+
42+
ctx = checkQueryID(ctx)
4043
go func() {
4144
err := dc.rest.QuerySync(ctx, query, args, respCh)
4245
errCh <- err
@@ -62,6 +65,7 @@ func (dc *DatabendConn) exec(ctx context.Context, query string, args ...driver.V
6265

6366
func (dc *DatabendConn) query(ctx context.Context, query string, args ...driver.Value) (driver.Rows, error) {
6467
var r0 *QueryResponse
68+
ctx = checkQueryID(ctx)
6569
err := retry.Do(
6670
func() error {
6771
r, err := dc.rest.DoQuery(ctx, query, args)
@@ -206,3 +210,13 @@ func (dc *DatabendConn) Rollback() error {
206210
dc.Close()
207211
return nil
208212
}
213+
214+
// checkQueryID checks if query_id exists in context, if not, generate a new one
215+
func checkQueryID(ctx context.Context) context.Context {
216+
if _, ok := ctx.Value(ContextKeyQueryID).(string); ok {
217+
return ctx
218+
}
219+
queryId := uuid.NewString()
220+
ctx = context.WithValue(ctx, ContextKeyQueryID, queryId)
221+
return ctx
222+
}

0 commit comments

Comments
 (0)