Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: use exponential backoff for retrying posting webhook event (#4389)

Tianzhou (天舟)1b7995e8ddff
diff --git a/backend/common/retry.go b/backend/common/retry.go
new file mode 100644
index 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.go
index 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.ProjectWebho
for _, hook := range webhookList {
webhookCtx.URL = hook.URL
webhookCtx.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.mod
index 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.1
github.com/blang/semver/v4 v4.0.0
github.com/casbin/casbin/v2 v2.56.0
+ github.com/cenkalti/backoff/v4 v4.2.0
github.com/github/gh-ost v1.1.5
github.com/go-sql-driver/mysql v1.6.0
github.com/golang-jwt/jwt/v4 v4.4.2
@@ -87,7 +88,6 @@ require (
github.com/benbjohnson/clock v1.3.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
- github.com/cenkalti/backoff/v4 v4.2.0 // indirect
github.com/census-instrumentation/opencensus-proto v0.3.0 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cncf/udpa/go v0.0.0-20210930031921-04548b0d99d4 // indirect