Skip to main content

Proxy

Struct Proxy 

Source
pub struct Proxy { /* private fields */ }
Expand description

HTTP Proxy server that can be embedded into other applications

This struct encapsulates the proxy state and allows programmatic control over the proxy lifecycle.

§Example

use tiny_proxy::{Config, Proxy};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let config = Config::from_file("file.caddy")?;
    let proxy = Proxy::new(config);
    proxy.start("127.0.0.1:8080").await?;
    Ok(())
}

Implementations§

Source§

impl Proxy

Source

pub fn new(config: Config) -> Self

Create a new proxy instance with the given configuration

§Arguments
  • config - Configuration loaded from file or constructed programmatically
§Returns

A new Proxy instance ready to be started

Source

pub async fn start(&self, addr: &str) -> Result<()>

Start the proxy server on the specified address

This method blocks indefinitely, handling incoming connections. To run the proxy in the background, spawn it in a tokio task.

§Arguments
  • addr - Address to listen on (e.g., “127.0.0.1:8080” or “0.0.0.0:8080”)
§Example
proxy.start("127.0.0.1:8080").await?;

To run in background:

let handle = tokio::spawn(async move {
    if let Err(e) = proxy.start("127.0.0.1:8080").await {
        eprintln!("Proxy error: {}", e);
    }
});
Source

pub async fn start_with_addr(&self, addr: SocketAddr) -> Result<()>

Start the proxy server with a parsed SocketAddr

This is a convenience method if you already have a parsed SocketAddr.

§Arguments
  • addr - Parsed SocketAddr to listen on
Source

pub fn config(&self) -> &Config

Get a reference to current configuration

This allows inspection of current proxy configuration without modifying it.

§Returns

Reference to current Config

Source

pub fn max_concurrency(&self) -> usize

Get current concurrency limit

§Returns

Current maximum number of concurrent connections

Source

pub fn set_max_concurrency(&mut self, max: usize)

Update concurrency limit at runtime

§Arguments
  • max - New maximum number of concurrent connections
§Note

This updates the semaphore immediately. New connections will use the new limit, but existing connections are not affected.

Source

pub fn update_config(&mut self, config: Config)

Update the configuration

This allows hot-reload of configuration without restarting the proxy. New connections will use the updated configuration immediately.

§Arguments
  • config - New configuration to use
§Note

This operation is atomic for new connections. Existing connections will continue to use their original configuration.

Auto Trait Implementations§

§

impl Freeze for Proxy

§

impl !RefUnwindSafe for Proxy

§

impl Send for Proxy

§

impl Sync for Proxy

§

impl Unpin for Proxy

§

impl UnsafeUnpin for Proxy

§

impl !UnwindSafe for Proxy

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, 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
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,