Skip to content

Commit 72fcfb0

Browse files
committed
feat: add infinite retry mechanism for ThreadWinds registration
1 parent c7774b4 commit 72fcfb0

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

threadwinds-ingestion/main.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/utmstack/UTMStack/threadwinds-ingestion/config"
1212
"github.com/utmstack/UTMStack/threadwinds-ingestion/internal/client"
1313
"github.com/utmstack/UTMStack/threadwinds-ingestion/internal/scheduler"
14+
"github.com/utmstack/UTMStack/threadwinds-ingestion/utils"
1415
)
1516

1617
func main() {
@@ -57,14 +58,27 @@ func main() {
5758
}
5859

5960
if twConfig.APIKey == "" || twConfig.APISecret == "" {
60-
catcher.Info("ThreadWinds not configured, registering in platform...", nil)
61+
catcher.Info("ThreadWinds not configured, will attempt registration with retry...", nil)
6162

62-
regResp, err := cmClient.RegisterUserReporter(adminEmail)
63+
var regResp *client.RegistrationResponse
64+
65+
registerFunc := func() error {
66+
resp, err := cmClient.RegisterUserReporter(adminEmail)
67+
if err != nil {
68+
return err
69+
}
70+
regResp = resp
71+
return nil
72+
}
73+
74+
err = utils.InfiniteRetryIfXError(registerFunc, "404", "Not Found", "connection refused")
6375
if err != nil {
64-
catcher.Error("failed to register in ThreadWinds Platform", err, nil)
76+
catcher.Error("failed to register in ThreadWinds Platform after retries", err, nil)
6577
os.Exit(1)
6678
}
6779

80+
catcher.Info("ThreadWinds registration successful", nil)
81+
6882
err = backendClient.SaveThreadWindsCredentials(ctx,
6983
regResp.APIKey,
7084
regResp.APISecret,
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package utils
2+
3+
import (
4+
"strings"
5+
"time"
6+
7+
"github.com/threatwinds/go-sdk/catcher"
8+
)
9+
10+
const (
11+
wait = 5 * time.Second
12+
)
13+
14+
func InfiniteRetryIfXError(f func() error, exceptions ...string) error {
15+
var xErrorWasLogged bool
16+
17+
for {
18+
err := f()
19+
if err != nil && is(err, exceptions...) {
20+
if !xErrorWasLogged {
21+
_ = catcher.Error("An error occurred (%s), will keep retrying indefinitely...", err, nil)
22+
xErrorWasLogged = true
23+
}
24+
time.Sleep(wait)
25+
continue
26+
}
27+
28+
return err
29+
}
30+
}
31+
32+
func is(e error, args ...string) bool {
33+
for _, arg := range args {
34+
if strings.Contains(e.Error(), arg) {
35+
return true
36+
}
37+
}
38+
return false
39+
}

0 commit comments

Comments
 (0)