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
impl Session
Sourcepub fn execute(&mut self, request: Request) -> Result<Response, NanoGetError>
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
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}Sourcepub fn execute_ref(
&mut self,
request: &Request,
) -> Result<Response, NanoGetError>
pub fn execute_ref( &mut self, request: &Request, ) -> Result<Response, NanoGetError>
Executes a borrowed request by cloning it internally.
Sourcepub fn execute_pipelined(
&mut self,
requests: &[Request],
) -> Result<Vec<Response>, NanoGetError>
pub fn execute_pipelined( &mut self, requests: &[Request], ) -> Result<Vec<Response>, NanoGetError>
Sends multiple requests in one HTTP/1.1 pipeline.
Requirements:
ConnectionPolicy::Reusemust be enabled- all requests must target the same underlying connection
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
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§
impl Freeze for Session
impl !RefUnwindSafe for Session
impl Send for Session
impl !Sync for Session
impl Unpin for Session
impl UnsafeUnpin for Session
impl !UnwindSafe for Session
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more