Skip to content

Add Client Workaround With a Load Balancer #220

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
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ let environment = Environment::builder()
.build()
```

##### Building the environment with a load balancer

```rust,no_run
use rabbitmq_stream_client::Environment;


let environment = Environment::builder()
.load_balancer_mode(true)
.build()
```



##### Publishing messages

```rust,no_run
Expand Down
9 changes: 9 additions & 0 deletions src/client/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct ClientOptions {
pub(crate) v_host: String,
pub(crate) heartbeat: u32,
pub(crate) max_frame_size: u32,
pub(crate) load_balancer_mode: bool,
pub(crate) tls: TlsConfiguration,
pub(crate) collector: Arc<dyn MetricsCollector>,
}
Expand Down Expand Up @@ -39,6 +40,7 @@ impl Default for ClientOptions {
v_host: "/".to_owned(),
heartbeat: 60,
max_frame_size: 1048576,
load_balancer_mode: false,
collector: Arc::new(NopMetricsCollector {}),
tls: TlsConfiguration {
enabled: false,
Expand Down Expand Up @@ -117,6 +119,11 @@ impl ClientOptionsBuilder {
self
}

pub fn load_balancer_mode(mut self, load_balancer_mode: bool) -> Self {
self.0.load_balancer_mode = load_balancer_mode;
self
}

pub fn build(self) -> ClientOptions {
self.0
}
Expand Down Expand Up @@ -145,6 +152,7 @@ mod tests {
client_keys_path: String::from(""),
})
.collector(Arc::new(NopMetricsCollector {}))
.load_balancer_mode(true)
.build();
assert_eq!(options.host, "test");
assert_eq!(options.port, 8888);
Expand All @@ -154,5 +162,6 @@ mod tests {
assert_eq!(options.heartbeat, 10000);
assert_eq!(options.max_frame_size, 1);
assert_eq!(options.tls.enabled, true);
assert_eq!(options.load_balancer_mode, true);
}
}
27 changes: 20 additions & 7 deletions src/consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,26 @@ impl ConsumerBuilder {
metadata.replicas,
stream
);
client = Client::connect(ClientOptions {
host: replica.host.clone(),
port: replica.port as u16,
..self.environment.options.client_options
})
.await?;
let load_balancer_mode = self.environment.options.client_options.load_balancer_mode;
if load_balancer_mode {
let options = self.environment.options.client_options.clone();
loop {
let temp_client = Client::connect(options.clone()).await?;
let mapping = temp_client.connection_properties().await;
let advertised_host = mapping.get("advertised_host").unwrap();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the unwrap, if the property advertised_host is not found return an error.
Same for the producer

if *advertised_host == replica.host.clone() {
client = temp_client;
break;
}
}
} else {
client = Client::connect(ClientOptions {
host: replica.host.clone(),
port: replica.port as u16,
..self.environment.options.client_options
})
.await?;
}
}
} else {
return Err(ConsumerCreateError::StreamDoesNotExist {
Expand All @@ -100,7 +114,6 @@ impl ConsumerBuilder {
waker: AtomicWaker::new(),
metrics_collector: collector,
});

let msg_handler = ConsumerMessageHandler(consumer.clone());
client.set_handler(msg_handler).await;

Expand Down
5 changes: 5 additions & 0 deletions src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ impl EnvironmentBuilder {
self.0.client_options.collector = Arc::new(collector);
self
}

pub fn load_balancer_mode(mut self, load_balancer_mode: bool) -> EnvironmentBuilder {
self.0.client_options.load_balancer_mode = load_balancer_mode;
self
}
}
#[derive(Clone, Default)]
pub struct EnvironmentOptions {
Expand Down
29 changes: 22 additions & 7 deletions src/producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,28 @@ impl<T> ProducerBuilder<T> {
metadata.leader,
stream
);
client.close().await?;
client = Client::connect(ClientOptions {
host: metadata.leader.host.clone(),
port: metadata.leader.port as u16,
..self.environment.options.client_options
})
.await?;
let load_balancer_mode = self.environment.options.client_options.load_balancer_mode;
if load_balancer_mode {
// Producer must connect to leader node
let options: ClientOptions = self.environment.options.client_options.clone();
loop {
let temp_client = Client::connect(options.clone()).await?;
let mapping = temp_client.connection_properties().await;
let advertised_host = mapping.get("advertised_host").unwrap();
if *advertised_host == metadata.leader.host.clone() {
client = temp_client;
break;
}
}
} else {
client.close().await?;
client = Client::connect(ClientOptions {
host: metadata.leader.host.clone(),
port: metadata.leader.port as u16,
..self.environment.options.client_options
})
.await?
};
} else {
return Err(ProducerCreateError::StreamDoesNotExist {
stream: stream.into(),
Expand Down