Skip to content

Fix multi uris connection #310

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

Merged
merged 4 commits into from
Jun 12, 2024
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
20 changes: 17 additions & 3 deletions pkg/stream/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func NewEnvironment(options *EnvironmentOptions) (*Environment, error) {
if len(options.ConnectionParameters) == 0 {
options.ConnectionParameters = []*Broker{newBrokerDefault()}
}

for _, parameter := range options.ConnectionParameters {
var connectionError error
for idx, parameter := range options.ConnectionParameters {

if parameter.Uri != "" {
u, err := url.Parse(parameter.Uri)
Expand All @@ -78,13 +78,27 @@ func NewEnvironment(options *EnvironmentOptions) (*Environment, error) {
parameter.mergeWithDefault()

client.broker = parameter

connectionError = client.connect()
if connectionError == nil {
break
} else {
nextIfThereIs := ""
if idx < len(options.ConnectionParameters)-1 {
nextIfThereIs = "Trying the next broker..."
}
logs.LogError("New environment creation. Can't connect to the broker: %s port: %s. %s",
parameter.Host, parameter.Port, nextIfThereIs)

}
}

return &Environment{
options: options,
producers: newProducers(options.MaxProducersPerClient),
consumers: newConsumerEnvironment(options.MaxConsumersPerClient),
closed: false,
}, client.connect()
}, connectionError
}
func (env *Environment) newReconnectClient() (*Client, error) {
broker := env.options.ConnectionParameters[0]
Expand Down
53 changes: 53 additions & 0 deletions pkg/stream/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,59 @@ var _ = Describe("Environment test", func() {
Expect(errWrong).To(HaveOccurred())
})

It("Multi Uris/Multi with some not reachable end-points ", func() {
// To connect the client is enough to have one valid endpoint
// even the other endpoints are not reachable
// https://github.com/rabbitmq/rabbitmq-stream-go-client/issues/309

env, err := NewEnvironment(NewEnvironmentOptions().
SetUris([]string{
"rabbitmq-stream://guest:guest@localhost:5552/%2f",
"rabbitmq-stream://guest:guest@wrong:5552/%2f",
}))
Expect(err).NotTo(HaveOccurred())
Expect(env.Close()).NotTo(HaveOccurred())

env, err = NewEnvironment(NewEnvironmentOptions().
SetUris([]string{
"rabbitmq-stream://guest:guest@wrong:5552/%2f",
"rabbitmq-stream://guest:guest@localhost:5552/%2f",
}))
Expect(err).NotTo(HaveOccurred())
Expect(env.Close()).NotTo(HaveOccurred())

env, err = NewEnvironment(NewEnvironmentOptions().
SetUris([]string{
"rabbitmq-stream://guest:guest@wrong:5552/%2f",
"rabbitmq-stream://guest:guest@localhost:5552/%2f",
"rabbitmq-stream://guest:guest@wrong:5552/%2f",
}))
Expect(err).NotTo(HaveOccurred())
Expect(env.Close()).NotTo(HaveOccurred())

env, err = NewEnvironment(NewEnvironmentOptions().
SetUris([]string{
"rabbitmq-stream://guest:guest@wrong:5552/%2f",
"rabbitmq-stream://guest:guest@localhost:5552/%2f",
"rabbitmq-stream://guest:guest@wrong:5552/%2f",
"rabbitmq-stream://guest:guest@localhost:5552/%2f",
}))
Expect(err).NotTo(HaveOccurred())
Expect(env.Close()).NotTo(HaveOccurred())

// in this case all the endpoints are not reachable
// so it will fail
_, err = NewEnvironment(NewEnvironmentOptions().
SetUris([]string{
"rabbitmq-stream://guest:guest@wrong:5552/%2f",
"rabbitmq-stream://guest:guest@wrong:5552/%2f",
"rabbitmq-stream://guest:guest@wrong:5552/%2f",
"rabbitmq-stream://guest:guest@wrong:5552/%2f",
}))
Expect(err).To(HaveOccurred())

})

It("Fail TLS connection", func() {
_, err := NewEnvironment(NewEnvironmentOptions().
SetTLSConfig(&tls.Config{InsecureSkipVerify: true}).
Expand Down