@@ -16,7 +16,7 @@ use std::net::{TcpStream, Shutdown, SocketAddr};
16
16
use std:: io:: { self , Read , Write } ;
17
17
use std:: sync:: atomic:: { AtomicBool , Ordering } ;
18
18
use std:: sync:: mpsc;
19
- use std:: sync:: { Arc } ;
19
+ use std:: sync:: { Arc , Mutex } ;
20
20
use std:: net:: { TcpListener as StdTcpListener } ;
21
21
use std:: thread;
22
22
use std:: time:: Duration ;
@@ -1739,7 +1739,7 @@ fn skips_content_length_and_body_for_304_responses() {
1739
1739
struct Serve {
1740
1740
addr : SocketAddr ,
1741
1741
msg_rx : mpsc:: Receiver < Msg > ,
1742
- reply_tx : spmc:: Sender < Reply > ,
1742
+ reply_tx : Mutex < spmc:: Sender < Reply > > ,
1743
1743
shutdown_signal : Option < oneshot:: Sender < ( ) > > ,
1744
1744
thread : Option < thread:: JoinHandle < ( ) > > ,
1745
1745
}
@@ -1772,7 +1772,7 @@ impl Serve {
1772
1772
Ok ( buf)
1773
1773
}
1774
1774
1775
- fn reply ( & self ) -> ReplyBuilder {
1775
+ fn reply ( & self ) -> ReplyBuilder < ' _ > {
1776
1776
ReplyBuilder {
1777
1777
tx : & self . reply_tx
1778
1778
}
@@ -1782,43 +1782,45 @@ impl Serve {
1782
1782
type BoxError = Box < dyn std:: error:: Error + Send + Sync > ;
1783
1783
1784
1784
struct ReplyBuilder < ' a > {
1785
- tx : & ' a spmc:: Sender < Reply > ,
1785
+ tx : & ' a Mutex < spmc:: Sender < Reply > > ,
1786
1786
}
1787
1787
1788
1788
impl < ' a > ReplyBuilder < ' a > {
1789
1789
fn status ( self , status : hyper:: StatusCode ) -> Self {
1790
- self . tx . send ( Reply :: Status ( status) ) . unwrap ( ) ;
1790
+ self . tx . lock ( ) . unwrap ( ) . send ( Reply :: Status ( status) ) . unwrap ( ) ;
1791
1791
self
1792
1792
}
1793
1793
1794
1794
fn version ( self , version : hyper:: Version ) -> Self {
1795
- self . tx . send ( Reply :: Version ( version) ) . unwrap ( ) ;
1795
+ self . tx . lock ( ) . unwrap ( ) . send ( Reply :: Version ( version) ) . unwrap ( ) ;
1796
1796
self
1797
1797
}
1798
1798
1799
1799
fn header < V : AsRef < str > > ( self , name : & str , value : V ) -> Self {
1800
1800
let name = HeaderName :: from_bytes ( name. as_bytes ( ) ) . expect ( "header name" ) ;
1801
1801
let value = HeaderValue :: from_str ( value. as_ref ( ) ) . expect ( "header value" ) ;
1802
- self . tx . send ( Reply :: Header ( name, value) ) . unwrap ( ) ;
1802
+ self . tx . lock ( ) . unwrap ( ) . send ( Reply :: Header ( name, value) ) . unwrap ( ) ;
1803
1803
self
1804
1804
}
1805
1805
1806
1806
fn body < T : AsRef < [ u8 ] > > ( self , body : T ) {
1807
- self . tx . send ( Reply :: Body ( body. as_ref ( ) . to_vec ( ) . into ( ) ) ) . unwrap ( ) ;
1807
+ self . tx . lock ( ) . unwrap ( ) . send ( Reply :: Body ( body. as_ref ( ) . to_vec ( ) . into ( ) ) ) . unwrap ( ) ;
1808
1808
}
1809
1809
1810
1810
fn body_stream ( self , body : Body ) {
1811
- self . tx . send ( Reply :: Body ( body) ) . unwrap ( ) ;
1811
+ self . tx . lock ( ) . unwrap ( ) . send ( Reply :: Body ( body) ) . unwrap ( ) ;
1812
1812
}
1813
1813
1814
1814
fn error < E : Into < BoxError > > ( self , err : E ) {
1815
- self . tx . send ( Reply :: Error ( err. into ( ) ) ) . unwrap ( ) ;
1815
+ self . tx . lock ( ) . unwrap ( ) . send ( Reply :: Error ( err. into ( ) ) ) . unwrap ( ) ;
1816
1816
}
1817
1817
}
1818
1818
1819
1819
impl < ' a > Drop for ReplyBuilder < ' a > {
1820
1820
fn drop ( & mut self ) {
1821
- let _ = self . tx . send ( Reply :: End ) ;
1821
+ if let Ok ( mut tx) = self . tx . lock ( ) {
1822
+ let _ = tx. send ( Reply :: End ) ;
1823
+ }
1822
1824
}
1823
1825
}
1824
1826
@@ -2006,7 +2008,7 @@ impl ServeOptions {
2006
2008
2007
2009
Serve {
2008
2010
msg_rx : msg_rx,
2009
- reply_tx : reply_tx,
2011
+ reply_tx : Mutex :: new ( reply_tx) ,
2010
2012
addr : addr,
2011
2013
shutdown_signal : Some ( shutdown_tx) ,
2012
2014
thread : Some ( thread) ,
0 commit comments