Skip to main content

Session

Struct Session 

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

Stateful request executor that can hold a persistent connection.

A session is useful when you want tighter control over connection reuse and pipelining than calling Client::execute repeatedly.

Implementations§

Source§

impl Session

Source

pub fn execute(&mut self, request: Request) -> Result<Response, NanoGetError>

Executes a request, applying configured redirects, cache behavior, auth retries, and proxy routing.

Examples found in repository?
examples/nano-get-bench.rs (line 54)
42fn bench_get(iterations: usize) -> Duration {
43    let response = b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nhello".to_vec();
44    let (base_url, handle) = spawn_persistent_server(iterations, response);
45
46    let client = Client::builder()
47        .connection_policy(ConnectionPolicy::Reuse)
48        .build();
49    let mut session = client.session();
50
51    let start = Instant::now();
52    for i in 0..iterations {
53        let req = Request::get(format!("{}/g{}", base_url, i)).expect("request");
54        let resp = session.execute(req).expect("execute");
55        assert_eq!(resp.body, b"hello");
56    }
57    let elapsed = start.elapsed();
58    handle.join().expect("join server");
59    elapsed
60}
61
62fn bench_head(iterations: usize) -> Duration {
63    let response = b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\nX-Bench: 1\r\n\r\n".to_vec();
64    let (base_url, handle) = spawn_persistent_server(iterations, response);
65
66    let client = Client::builder()
67        .connection_policy(ConnectionPolicy::Reuse)
68        .build();
69    let mut session = client.session();
70
71    let start = Instant::now();
72    for i in 0..iterations {
73        let req = Request::head(format!("{}/h{}", base_url, i)).expect("request");
74        let resp = session.execute(req).expect("execute");
75        assert!(resp.body.is_empty());
76    }
77    let elapsed = start.elapsed();
78    handle.join().expect("join server");
79    elapsed
80}
More examples
Hide additional examples
examples/session-reuse-and-pipelining/main.rs (line 20)
14fn main() -> Result<(), Box<dyn Error>> {
15    let client = Client::builder()
16        .connection_policy(ConnectionPolicy::Reuse)
17        .build();
18    let mut session = client.session();
19
20    let first = session.execute(Request::get("http://example.com/")?)?;
21    let second = session.execute(Request::get("http://example.com/index.html")?)?;
22    let pipelined = session.execute_pipelined(&[
23        Request::get("http://example.com/")?,
24        Request::get("http://example.com/index.html")?,
25    ])?;
26
27    println!("This example demonstrates the Session API shape for reuse and pipelining.");
28    summarize("sequential #1", &first);
29    summarize("sequential #2", &second);
30    summarize("pipeline #1", &pipelined[0]);
31    summarize("pipeline #2", &pipelined[1]);
32
33    Ok(())
34}
Source

pub fn execute_ref( &mut self, request: &Request, ) -> Result<Response, NanoGetError>

Executes a borrowed request by cloning it internally.

Source

pub fn execute_pipelined( &mut self, requests: &[Request], ) -> Result<Vec<Response>, NanoGetError>

Sends multiple requests in one HTTP/1.1 pipeline.

Requirements:

On success, responses are returned in request order.

Examples found in repository?
examples/session-reuse-and-pipelining/main.rs (lines 22-25)
14fn main() -> Result<(), Box<dyn Error>> {
15    let client = Client::builder()
16        .connection_policy(ConnectionPolicy::Reuse)
17        .build();
18    let mut session = client.session();
19
20    let first = session.execute(Request::get("http://example.com/")?)?;
21    let second = session.execute(Request::get("http://example.com/index.html")?)?;
22    let pipelined = session.execute_pipelined(&[
23        Request::get("http://example.com/")?,
24        Request::get("http://example.com/index.html")?,
25    ])?;
26
27    println!("This example demonstrates the Session API shape for reuse and pipelining.");
28    summarize("sequential #1", &first);
29    summarize("sequential #2", &second);
30    summarize("pipeline #1", &pipelined[0]);
31    summarize("pipeline #2", &pipelined[1]);
32
33    Ok(())
34}
More examples
Hide additional examples
examples/nano-get-bench.rs (line 99)
82fn bench_pipeline(total_requests: usize, pipeline_depth: usize) -> Duration {
83    let response = b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nhello".to_vec();
84    let (base_url, handle) = spawn_persistent_server(total_requests, response);
85
86    let client = Client::builder()
87        .connection_policy(ConnectionPolicy::Reuse)
88        .build();
89    let mut session = client.session();
90
91    let start = Instant::now();
92    let mut sent = 0usize;
93    while sent < total_requests {
94        let batch_size = (total_requests - sent).min(pipeline_depth);
95        let mut batch = Vec::with_capacity(batch_size);
96        for index in 0..batch_size {
97            batch.push(Request::get(format!("{}/p{}", base_url, sent + index)).expect("request"));
98        }
99        let responses = session.execute_pipelined(&batch).expect("execute");
100        assert_eq!(responses.len(), batch_size);
101        for response in responses {
102            assert_eq!(response.body, b"hello");
103        }
104        sent += batch_size;
105    }
106    let elapsed = start.elapsed();
107    handle.join().expect("join server");
108    elapsed
109}

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, 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.