Push mutexes down into pool and cookie store. (#193)
Previously, Agent stored most of its state in one big Arc<Mutex<AgentState>>. This separates the Arc from the Mutexes. Now, Agent is a thin wrapper around an Arc<AgentState>. The individual components that need locking, ConnectionPool and CookieStore, now are responsible for their own locking. There were a couple of reasons for this. Internal components that needed an Agent were often instead carrying around an Arc<Mutex<AgentState>>. This felt like the components were too intertwined: those other components shouldn't have to care quite so much about how Agent is implemented. Also, this led to compromises of convenience: the Proxy on Agent wound up stored inside the `Arc<Mutex<AgentState>>` even though it didn't need locking. It was more convenient that way because that was what Request and Unit had access too. The other reason to push things down like this is that it can reduce lock contention. Mutations to the cookie store don't need to lock the connection pool, and vice versa. This was a secondary concern, since I haven't actually profiled these things and found them to be a problem, but it's a happy result of the refactoring. Now all the components outside of Agent take an Agent instead of AgentState. In the process I removed `Agent.cookie()`. Its API was hard to use correctly, since it didn't distinguish between cookies on different hosts. And it would have required updates as part of this refactoring. I'm open to reinstating some similar functionality with a refreshed API. I kept `Agent.set_cookie`, but updated its method signature to take a URL as well as a cookie. Many of ConnectionPool's methods went from `&mut self` to `&self`, because ConnectionPool is now using interior mutability.
This commit is contained in:
committed by
GitHub
parent
75bc803cf1
commit
703ca41960
@@ -1,12 +1,13 @@
|
||||
use std::fmt;
|
||||
use std::io::Read;
|
||||
use std::sync::{Arc, Mutex};
|
||||
#[cfg(any(feature = "native-tls", feature = "tls"))]
|
||||
use std::sync::Arc;
|
||||
use std::time;
|
||||
|
||||
use qstring::QString;
|
||||
use url::{form_urlencoded, Url};
|
||||
|
||||
use crate::agent::{self, Agent, AgentState};
|
||||
use crate::agent::{self, Agent};
|
||||
use crate::body::BodySize;
|
||||
use crate::body::{Payload, SizedReader};
|
||||
use crate::error::Error;
|
||||
@@ -31,7 +32,7 @@ pub type Result<T> = std::result::Result<T, Error>;
|
||||
/// ```
|
||||
#[derive(Clone, Default)]
|
||||
pub struct Request {
|
||||
pub(crate) agent: Arc<Mutex<AgentState>>,
|
||||
pub(crate) agent: Agent,
|
||||
|
||||
// via agent
|
||||
pub(crate) method: String,
|
||||
@@ -73,7 +74,7 @@ impl fmt::Debug for Request {
|
||||
impl Request {
|
||||
pub(crate) fn new(agent: &Agent, method: String, url: String) -> Request {
|
||||
Request {
|
||||
agent: Arc::clone(&agent.state),
|
||||
agent: agent.clone(),
|
||||
method,
|
||||
url,
|
||||
headers: agent.headers.clone(),
|
||||
@@ -596,7 +597,7 @@ impl Request {
|
||||
pub(crate) fn proxy(&self) -> Option<Proxy> {
|
||||
if let Some(proxy) = &self.proxy {
|
||||
Some(proxy.clone())
|
||||
} else if let Some(proxy) = &self.agent.lock().unwrap().proxy {
|
||||
} else if let Some(proxy) = &self.agent.state.proxy {
|
||||
Some(proxy.clone())
|
||||
} else {
|
||||
None
|
||||
|
||||
Reference in New Issue
Block a user