Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry on message #80

Merged
merged 2 commits into from Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 39 additions & 13 deletions datasource.go
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"net/http"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -225,19 +226,26 @@ func (ds *SQLDatasource) handleQuery(ctx context.Context, req backend.DataQuery,
// If there's a query error that didn't exceed the
// context deadline retry the query
if errors.Is(err, ErrorQuery) && !errors.Is(err, context.DeadlineExceeded) {
for i := 0; i < ds.driverSettings.Retries; i++ {
backend.Logger.Warn(fmt.Sprintf("query failed. retrying %d times", i))
db, err := ds.dbReconnect(dbConn, q, cacheKey)
if err != nil {
return nil, err
}

if ds.driverSettings.Pause > 0 {
time.Sleep(time.Duration(ds.driverSettings.Pause * int(time.Second)))
}
res, err = QueryDB(ctx, db, ds.c.Converters(), fillMode, q)
if err == nil {
return res, err
// only retry on messages that contain specific errors
if shouldRetry(ds.driverSettings.RetryOn, err.Error()) {
for i := 0; i < ds.driverSettings.Retries; i++ {
backend.Logger.Warn(fmt.Sprintf("query failed: %s. Retrying %d times", err.Error(), i))
db, err := ds.dbReconnect(dbConn, q, cacheKey)
if err != nil {
return nil, err
}

if ds.driverSettings.Pause > 0 {
time.Sleep(time.Duration(ds.driverSettings.Pause * int(time.Second)))
}
res, err = QueryDB(ctx, db, ds.c.Converters(), fillMode, q)
if err == nil {
return res, err
}
if !shouldRetry(ds.driverSettings.RetryOn, err.Error()) {
return res, err
}
backend.Logger.Warn(fmt.Sprintf("Retry failed: %s", err.Error()))
}
}
}
Expand Down Expand Up @@ -302,6 +310,15 @@ func (ds *SQLDatasource) checkWithRetries(conn dbConnection) (*backend.CheckHeal
if err == nil {
return result, err
}

if !shouldRetry(ds.driverSettings.RetryOn, err.Error()) {
break
}

if ds.driverSettings.Pause > 0 {
time.Sleep(time.Duration(ds.driverSettings.Pause * int(time.Second)))
}
backend.Logger.Warn(fmt.Sprintf("connect failed: %s. Retrying %d times", err.Error(), i))
}

// TODO: failed health checks don't return an error
Expand Down Expand Up @@ -332,3 +349,12 @@ func (ds *SQLDatasource) ping(conn dbConnection) error {

return conn.db.PingContext(ctx)
}

func shouldRetry(retryOn []string, err string) bool {
for _, r := range retryOn {
if strings.Contains(err, r) {
return true
}
}
return false
}
4 changes: 2 additions & 2 deletions datasource_test.go
Expand Up @@ -148,7 +148,7 @@ func Test_timeout_retries(t *testing.T) {
}
retries := 5
max := time.Duration(testTimeout) * time.Second
driverSettings := DriverSettings{Retries: retries, Timeout: max}
driverSettings := DriverSettings{Retries: retries, Timeout: max, RetryOn: []string{"deadline"}}
ds := &SQLDatasource{c: timeoutDriver, driverSettings: driverSettings}

key := defaultKey(dsUID)
Expand Down Expand Up @@ -190,7 +190,7 @@ func Test_error_retries(t *testing.T) {
}
retries := 5
max := time.Duration(10) * time.Second
driverSettings := DriverSettings{Retries: retries, Timeout: max, Pause: 1}
driverSettings := DriverSettings{Retries: retries, Timeout: max, Pause: 1, RetryOn: []string{"foo"}}
ds := &SQLDatasource{c: timeoutDriver, driverSettings: driverSettings}

key := defaultKey(dsUID)
Expand Down
1 change: 1 addition & 0 deletions driver.go
Expand Up @@ -16,6 +16,7 @@ type DriverSettings struct {
FillMode *data.FillMissing
Retries int
Pause int
RetryOn []string
}

// Driver is a simple interface that defines how to connect to a backend SQL datasource
Expand Down