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

Expose host and port in BinlogRequest builder. #96

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
56 changes: 56 additions & 0 deletions src/packets/binlog_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ use super::{BinlogDumpFlags, ComBinlogDump, ComBinlogDumpGtid, Sid};
/// Binlog request representation. Please consult MySql documentation.
///
/// This struct is a helper builder for [`ComBinlogDump`] and [`ComBinlogDumpGtid`].
///
/// `server_id`, `host`, `port` are inspectable Source server side with:
/// `SHOW SLAVE HOSTS` mysql 5.7 or `SHOW REPLICAS` on mysql 8.x.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct BinlogRequest<'a> {
/// Server id of a slave.
Expand All @@ -25,6 +28,14 @@ pub struct BinlogRequest<'a> {
flags: BinlogDumpFlags,
/// Filename of the binlog on the master.
filename: Cow<'a, [u8]>,
/// Replicat/Slave hostname
///
/// Defaults to the Empty string.
hostname: Cow<'a, [u8]>,
/// Replicat/Slave port
///
/// Defaults to 0.
port: u16,
/// Position in the binlog-file to start the stream with.
///
/// If `use_gtid` is `false`, then the value will be truncated to u32.
Expand All @@ -43,6 +54,8 @@ impl<'a> BinlogRequest<'a> {
filename: Default::default(),
pos: 4,
sids: vec![],
hostname: Default::default(),
port: 0,
}
}

Expand All @@ -56,6 +69,23 @@ impl<'a> BinlogRequest<'a> {
self.use_gtid
}

/// Returns the hostname to report to the Source server used for replication.
///
/// Purely informative it's not an information used for connection.
/// Be sure to set something meaningful.
pub fn hostname_raw(&'a self) -> &'a [u8] {
self.hostname.as_ref()
}

/// Returns the hostname to report to the Source server used for replication,
/// as a UTF-8 string (lossy converted).
///
/// Purely informative it's not an information used for connection.
/// Be sure to set something meaningful.
pub fn hostname(&'a self) -> Cow<'a, str> {
String::from_utf8_lossy(self.hostname.as_ref())
}

/// If `use_gtid` is `false`, then all flags except `BINLOG_DUMP_NON_BLOCK` will be truncated
/// (defaults to empty).
pub fn flags(&self) -> BinlogDumpFlags {
Expand All @@ -80,6 +110,13 @@ impl<'a> BinlogRequest<'a> {
self.pos
}

/// Port to report to the Source used for Replication.
///
/// Purely informative be sure to define the same as the one used for connection.
pub fn port(&self) -> u16 {
self.port
}

/// If `use_gtid` is `false`, then this value will be ignored (defaults to an empty vector).
pub fn sids(&self) -> &[Sid<'_>] {
&self.sids
Expand All @@ -91,6 +128,25 @@ impl<'a> BinlogRequest<'a> {
self
}

/// Returns modified `self` with the given `host` value,
///
/// The host value is purely informative and used in mysql replica inspection statements.
pub fn with_hostname(mut self, hostname: impl Into<Cow<'a, [u8]>>) -> Self {
self.hostname = hostname.into();
self
}

/// Returns modified `self` with the given `port` value to show in
///
/// ## Warning
///
/// Setting a reporting port different of the real port used to stream
/// the binlog will lead replica inspections sql statement to show the port setted here!
pub fn with_port(mut self, port: u16) -> Self {
self.port = port;
self
}

/// Returns modified `self` with the given value of the `use_gtid` field.
pub fn with_use_gtid(mut self, use_gtid: bool) -> Self {
self.use_gtid = use_gtid;
Expand Down