Skip to main content

ToxiproxyTemplate

Struct ToxiproxyTemplate 

Source
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

Source

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.

Source

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.

Source

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);
Source

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.

Source

pub fn auto_remove(self) -> Self

Enable auto-remove when the container stops.

Source

pub fn custom_image( self, image: impl Into<String>, tag: impl Into<String>, ) -> Self

Use a custom image and tag.

Source

pub fn platform(self, platform: impl Into<String>) -> Self

Set the platform for the container (e.g., “linux/arm64”, “linux/amd64”).

Source

pub fn api_ready_timeout(self, timeout: Duration) -> Self

Set how long to wait for the control API to become ready (default: 30s).

Source

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.

Source

pub async fn create_proxy( &self, name: impl Into<String>, listen: impl Into<String>, upstream: impl Into<String>, ) -> Result<ProxyInfo>

Register a new proxy.

  • name identifies the proxy in subsequent calls.
  • listen is the address the proxy listens on inside the container. Use 0.0.0.0:<port> with a published proxy_port so host clients can connect through it.
  • upstream is the address the proxy forwards to (e.g. redis:6379 when 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).

Source

pub async fn add_toxic( &self, proxy: &str, name: impl Into<String>, stream: ToxicStream, toxic: Toxic, ) -> Result<()>

Add a toxic to an existing proxy.

  • proxy is the proxy name passed to create_proxy.
  • name identifies the toxic for later removal.
  • stream selects the direction the toxic applies to.
  • toxic is 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.

Source

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.

Source

pub async fn list_proxies(&self) -> Result<Vec<ProxyInfo>>

List all registered proxies.

§Errors

Returns an error if the control API request fails or the response cannot be parsed.

Source

pub async fn reset(&self) -> Result<()>

Reset all proxies and remove all toxics.

This re-enables every proxy and clears all toxics, returning Toxiproxy to a clean state without restarting the container.

§Errors

Returns an error if the control API request fails or returns a non-success status.

Trait Implementations§

Source§

impl Template for ToxiproxyTemplate

Source§

fn name(&self) -> &str

Get the template name
Source§

fn config(&self) -> &TemplateConfig

Get the template configuration
Source§

fn config_mut(&mut self) -> &mut TemplateConfig

Get a mutable reference to the configuration
Source§

fn build_command(&self) -> RunCommand

Build the RunCommand for this template
Source§

fn start<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<String>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Start the container with this template
Source§

fn start_and_wait<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<String>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Start the container and wait for it to be ready
Source§

fn stop<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Stop the container
Source§

fn remove<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Remove the container
Source§

fn is_running<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Check if the container is running
Source§

fn logs<'life0, 'life1, 'async_trait>( &'life0 self, follow: bool, tail: Option<&'life1 str>, ) -> Pin<Box<dyn Future<Output = Result<CommandOutput>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Get container logs
Source§

fn exec<'life0, 'life1, 'async_trait>( &'life0 self, command: Vec<&'life1 str>, ) -> Pin<Box<dyn Future<Output = Result<ExecOutput>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Execute a command in the running container
Source§

fn wait_for_ready<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Wait for the container to be ready Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more