pub struct ToxiproxyTemplate { /* private fields */ }Expand description
Toxiproxy template for TCP fault injection.
Runs the ghcr.io/shopify/toxiproxy container and exposes a typed client over
its :8474 control API. Register proxies with create_proxy
and add toxics with add_toxic.
Proxies should listen on 0.0.0.0:<port> and that port must be published (via
proxy_port) so host clients can connect through the proxy.
Implementations§
Source§impl ToxiproxyTemplate
impl ToxiproxyTemplate
Sourcepub fn new(name: impl Into<String>) -> Self
pub fn new(name: impl Into<String>) -> Self
Create a new Toxiproxy template.
The control API is published on port 8474 by default. Use
proxy_port to also publish the ports your proxies
listen on so host clients can reach them.
Sourcepub fn control_port(self, port: u16) -> Self
pub fn control_port(self, port: u16) -> Self
Set the host port for the control API (default: 8474).
This maps the host port to the container’s 8474 control port.
Sourcepub fn proxy_port(self, port: u16) -> Self
pub fn proxy_port(self, port: u16) -> Self
Publish a proxy port so host clients can connect through a proxy.
A proxy that listens on 0.0.0.0:<port> inside the container is only
reachable from the host if that port is published. Call this once per port
you intend to proxy on. The same port is used on both the host and
container sides so the published address matches the proxy’s listen
address.
§Example
use docker_wrapper::template::toxiproxy::ToxiproxyTemplate;
let toxiproxy = ToxiproxyTemplate::new("chaos")
.proxy_port(16379)
.proxy_port(15432);Sourcepub fn network(self, network: impl Into<String>) -> Self
pub fn network(self, network: impl Into<String>) -> Self
Connect to a specific Docker network.
Use this to place Toxiproxy on the same network as the upstream containers it proxies, so it can reach them by container name.
Sourcepub fn auto_remove(self) -> Self
pub fn auto_remove(self) -> Self
Enable auto-remove when the container stops.
Sourcepub fn custom_image(
self,
image: impl Into<String>,
tag: impl Into<String>,
) -> Self
pub fn custom_image( self, image: impl Into<String>, tag: impl Into<String>, ) -> Self
Use a custom image and tag.
Sourcepub fn platform(self, platform: impl Into<String>) -> Self
pub fn platform(self, platform: impl Into<String>) -> Self
Set the platform for the container (e.g., “linux/arm64”, “linux/amd64”).
Sourcepub fn api_ready_timeout(self, timeout: Duration) -> Self
pub fn api_ready_timeout(self, timeout: Duration) -> Self
Set how long to wait for the control API to become ready (default: 30s).
Sourcepub async fn wait_for_control_api(&self) -> Result<()>
pub async fn wait_for_control_api(&self) -> Result<()>
Wait for the Toxiproxy control API to start responding.
Polls GET /version on the control port until it returns success or the
configured timeout elapses. Call this after start
(or start_and_wait) before registering
proxies.
§Errors
Returns an error if the control API does not respond within the timeout.
Sourcepub async fn create_proxy(
&self,
name: impl Into<String>,
listen: impl Into<String>,
upstream: impl Into<String>,
) -> Result<ProxyInfo>
pub async fn create_proxy( &self, name: impl Into<String>, listen: impl Into<String>, upstream: impl Into<String>, ) -> Result<ProxyInfo>
Register a new proxy.
nameidentifies the proxy in subsequent calls.listenis the address the proxy listens on inside the container. Use0.0.0.0:<port>with a publishedproxy_portso host clients can connect through it.upstreamis the address the proxy forwards to (e.g.redis:6379when on a shared network).
§Errors
Returns an error if the control API request fails or returns a non-success status (for example, if a proxy with the same name already exists).
Sourcepub async fn add_toxic(
&self,
proxy: &str,
name: impl Into<String>,
stream: ToxicStream,
toxic: Toxic,
) -> Result<()>
pub async fn add_toxic( &self, proxy: &str, name: impl Into<String>, stream: ToxicStream, toxic: Toxic, ) -> Result<()>
Add a toxic to an existing proxy.
proxyis the proxy name passed tocreate_proxy.nameidentifies the toxic for later removal.streamselects the direction the toxic applies to.toxicis the typed fault to inject.
The toxic is applied with full toxicity (1.0), so it affects every
connection.
§Errors
Returns an error if the control API request fails or returns a non-success status.
Sourcepub async fn remove_toxic(&self, proxy: &str, toxic: &str) -> Result<()>
pub async fn remove_toxic(&self, proxy: &str, toxic: &str) -> Result<()>
Remove a toxic from a proxy.
§Errors
Returns an error if the control API request fails or returns a non-success status.