Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix: use exponential backoff for retrying posting webhook event (#4389)
Tianzhou (天舟)1b7995e8ddff
diff --git a/backend/common/retry.go b/backend/common/retry.gonew file mode 100644index 0000000000..cfcf98d52d--- /dev/null+++ b/backend/common/retry.go@@ -0,0 +1,19 @@+package common++import (+ "time"++ "github.com/cenkalti/backoff/v4"+)++const (+ // Just choose an empirical timeout value. The default 15 miniutes seems too long.+ timeout = 5 * time.Minute+)++// Retry uses exponential backoff with timeout.+func Retry(fn func() error) error {+ b := backoff.NewExponentialBackOff()+ b.MaxElapsedTime = timeout+ return backoff.Retry(fn, b)+}diff --git a/backend/component/activity/manager.go b/backend/component/activity/manager.goindex bf44afe81b..c56bcc3262 100644--- a/backend/component/activity/manager.go+++ b/backend/component/activity/manager.go@@ -9,6 +9,7 @@ import ("strconv""time"+ "github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/common/log""github.com/bytebase/bytebase/backend/component/config"api "github.com/bytebase/bytebase/backend/legacyapi"@@ -169,19 +170,17 @@ func postWebhookList(webhookCtx webhook.Context, webhookList []*api.ProjectWebhofor _, hook := range webhookList {webhookCtx.URL = hook.URLwebhookCtx.CreatedTs = time.Now().Unix()- const maxRetries = 3- for retries := 0; retries < maxRetries; retries++ {- if err := webhook.Post(hook.Type, webhookCtx); err != nil {- // The external webhook endpoint might be invalid which is out of our code control, so we just emit a warning- log.Warn("Failed to post webhook event on activity",- zap.String("webhook type", hook.Type),- zap.String("webhook name", hook.Name),- zap.String("activity type", webhookCtx.ActivityType),- zap.String("title", webhookCtx.Title),- zap.Error(err))- } else {- break- }+ if err := common.Retry(func() error {+ return webhook.Post(hook.Type, webhookCtx)+ }); err != nil {+ // The external webhook endpoint might be invalid which is out of our code control, so we just emit a warning+ log.Warn("Failed to post webhook event on activity",+ zap.String("webhook type", hook.Type),+ zap.String("webhook name", hook.Name),+ zap.String("activity type", webhookCtx.ActivityType),+ zap.String("title", webhookCtx.Title),+ zap.Error(err))+ return}}}diff --git a/go.mod b/go.modindex ab52f1e822..d1dda72e64 100644--- a/go.mod+++ b/go.mod@@ -12,6 +12,7 @@ require (github.com/aws/aws-sdk-go-v2/service/s3 v1.29.1github.com/blang/semver/v4 v4.0.0github.com/casbin/casbin/v2 v2.56.0+ github.com/cenkalti/backoff/v4 v4.2.0github.com/github/gh-ost v1.1.5github.com/go-sql-driver/mysql v1.6.0github.com/golang-jwt/jwt/v4 v4.4.2@@ -87,7 +88,6 @@ require (github.com/benbjohnson/clock v1.3.0 // indirectgithub.com/beorn7/perks v1.0.1 // indirectgithub.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect- github.com/cenkalti/backoff/v4 v4.2.0 // indirectgithub.com/census-instrumentation/opencensus-proto v0.3.0 // indirectgithub.com/cespare/xxhash/v2 v2.1.2 // indirectgithub.com/cncf/udpa/go v0.0.0-20210930031921-04548b0d99d4 // indirect