Skip to content

Commit

Permalink
chore: add NATS retry
Browse files Browse the repository at this point in the history
  • Loading branch information
WitoDelnat committed Oct 22, 2024
1 parent 7e51736 commit ad99718
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ require (
github.com/agnivade/levenshtein v1.1.1 // indirect
github.com/alecthomas/chroma v0.10.0 // indirect
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/avast/retry-go/v4 v4.6.0 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0/go.mod h1:t2tdK
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/atomicgo/cursor v0.0.1/go.mod h1:cBON2QmmrysudxNBFthvMtN32r3jxVRIvzkUiF/RuIk=
github.com/avast/retry-go/v4 v4.6.0 h1:K9xNA+KeB8HHc2aWFuLb25Offp+0iVRXEvFx8IinRJA=
github.com/avast/retry-go/v4 v4.6.0/go.mod h1:gvWlPhBVsvBbLkVGDg/KwvBv0bEkCOLRRSHKIr2PyOE=
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
Expand Down
12 changes: 11 additions & 1 deletion pkg/event/bus/nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"sync"
"time"

"github.com/avast/retry-go/v4"
"github.com/nats-io/nats.go"

"github.com/kubeshop/testkube/pkg/api/v1/testkube"
"github.com/kubeshop/testkube/pkg/event/kind/common"
"github.com/kubeshop/testkube/pkg/log"
"github.com/kubeshop/testkube/pkg/utils"
)

var (
Expand Down Expand Up @@ -80,7 +82,15 @@ func NewNATSEncodedConnection(cfg ConnectionConfig, opts ...nats.Option) (*nats.
func NewNATSConnection(cfg ConnectionConfig, opts ...nats.Option) (*nats.Conn, error) {
opts = append(opts, optsFromConfig(cfg)...)

nc, err := nats.Connect(cfg.NatsURI, opts...)
log.DefaultLogger.Infoln("Connecting to NATS")
nc, err := retry.DoWithData(
func() (*nats.Conn, error) {
return nats.Connect(cfg.NatsURI, opts...)
},
retry.DelayType(retry.FixedDelay),
retry.Delay(utils.DefaultRetryDelay),
retry.Attempts(20),
)
if err != nil {
log.DefaultLogger.Fatalw("error connecting to nats", "error", err)
return nil, err
Expand Down
3 changes: 3 additions & 0 deletions pkg/utils/consts.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package utils

import "time"

const (
// DefaultDockerRegistry is the default registry used when no registry is specified in the image name.
DefaultDockerRegistry = "https://index.docker.io/v1/"
DefaultRetryDelay = time.Second * 3
)

0 comments on commit ad99718

Please sign in to comment.