Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait for nats server when starting middleware #2572

Merged
merged 2 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/unreleased/waitForNats.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Bugfix: wait for nats server on middleware start

Use a retry mechanism to connect to the nats server when it is not ready yet

https://github.com/cs3org/reva/pull/2572
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/beevik/etree v1.1.0
github.com/bluele/gcache v0.0.2
github.com/c-bata/go-prompt v0.2.5
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/ceph/go-ceph v0.13.0
github.com/cheggaaa/pb v1.0.29
github.com/coreos/go-oidc v2.2.1+incompatible
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ github.com/bmizerany/pat v0.0.0-20170815010413-6226ea591a40/go.mod h1:8rLXio+Wji
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/c-bata/go-prompt v0.2.5 h1:3zg6PecEywxNn0xiqcXHD96fkbxghD+gdB2tbsYfl+Y=
github.com/c-bata/go-prompt v0.2.5/go.mod h1:vFnjEGDIIA/Lib7giyE4E9c50Lvl8j0S+7FVlAwDAVw=
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/cenkalti/backoff/v4 v4.1.0/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/cenkalti/backoff/v4 v4.1.1/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/census-instrumentation/opencensus-proto v0.2.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down
19 changes: 18 additions & 1 deletion pkg/events/server/nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
package server

import (
"fmt"

"github.com/asim/go-micro/plugins/events/nats/v4"
"github.com/cenkalti/backoff"
"go-micro.dev/v4/events"

stanServer "github.com/nats-io/nats-streaming-server/server"
Expand All @@ -38,6 +41,20 @@ func RunNatsServer(opts ...Option) error {
}

// NewNatsStream returns a streaming client used by `Consume` and `Publish` methods
// retries exponentially to connect to a nats server
func NewNatsStream(opts ...nats.Option) (events.Stream, error) {
return nats.NewStream(opts...)
b := backoff.NewExponentialBackOff()
var stream events.Stream
o := func() error {
s, err := nats.NewStream(opts...)
if err != nil {
// TODO: should we get the standard logger here? if yes: How?
fmt.Printf("can't connect to nats (stan) server, retrying in %s\n", b.NextBackOff())
}
stream = s
return err
}

err := backoff.Retry(o, b)
return stream, err
}