-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(temporal): use retry logic when connecting
- Loading branch information
1 parent
3ef1a37
commit ccba0a9
Showing
3 changed files
with
36 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,18 @@ | ||
package internal | ||
|
||
import ( | ||
"time" | ||
|
||
"code.tjo.space/mentos1386/zdravko/internal/config" | ||
"code.tjo.space/mentos1386/zdravko/pkg/retry" | ||
"go.temporal.io/sdk/client" | ||
) | ||
|
||
func ConnectToTemporal(cfg *config.Config) (client.Client, error) { | ||
c, err := client.Dial(client.Options{HostPort: cfg.Temporal.ServerHost}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return c, nil | ||
// Try to connect to the Temporal Server | ||
return retry.Retry(5, 6*time.Second, func() (client.Client, error) { | ||
return client.Dial(client.Options{ | ||
HostPort: cfg.Temporal.ServerHost, | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package retry | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
) | ||
|
||
// https://stackoverflow.com/questions/67069723/keep-retrying-a-function-in-golang | ||
func Retry[T any](attempts int, sleep time.Duration, f func() (T, error)) (result T, err error) { | ||
for i := 0; i < attempts; i++ { | ||
if i > 0 { | ||
log.Println("retrying after error:", err) | ||
time.Sleep(sleep) | ||
sleep *= 2 | ||
} | ||
result, err = f() | ||
if err == nil { | ||
return result, nil | ||
} | ||
} | ||
return result, fmt.Errorf("after %d attempts, last error: %s", attempts, err) | ||
} |