1414
1515//! Defines where to connect to and how to connect to a remote server
1616
17+ use crate :: connectors:: { l4:: BindTo , L4Connect } ;
18+ use crate :: protocols:: l4:: socket:: SocketAddr ;
19+ use crate :: protocols:: tls:: CaType ;
20+ #[ cfg( unix) ]
21+ use crate :: protocols:: ConnFdReusable ;
22+ use crate :: protocols:: TcpKeepalive ;
23+ use crate :: utils:: tls:: { get_organization_unit, CertKey } ;
1724use ahash:: AHasher ;
25+ use derivative:: Derivative ;
1826use pingora_error:: {
1927 ErrorType :: { InternalError , SocketError } ,
2028 OrErr , Result ,
@@ -30,14 +38,7 @@ use std::os::windows::io::AsRawSocket;
3038use std:: path:: { Path , PathBuf } ;
3139use std:: sync:: Arc ;
3240use std:: time:: Duration ;
33-
34- use crate :: connectors:: { l4:: BindTo , L4Connect } ;
35- use crate :: protocols:: l4:: socket:: SocketAddr ;
36- use crate :: protocols:: tls:: CaType ;
37- #[ cfg( unix) ]
38- use crate :: protocols:: ConnFdReusable ;
39- use crate :: protocols:: TcpKeepalive ;
40- use crate :: utils:: tls:: { get_organization_unit, CertKey } ;
41+ use tokio:: net:: TcpSocket ;
4142
4243pub use crate :: protocols:: tls:: ALPN ;
4344
@@ -203,6 +204,17 @@ pub trait Peer: Display + Clone {
203204 fn get_tracer ( & self ) -> Option < Tracer > {
204205 None
205206 }
207+
208+ /// Returns a hook that should be run before an upstream TCP connection is connected.
209+ ///
210+ /// This hook can be used to set additional socket options.
211+ fn upstream_tcp_sock_tweak_hook (
212+ & self ,
213+ ) -> Option < & Arc < dyn Fn ( & TcpSocket ) -> Result < ( ) > + Send + Sync + ' static > > {
214+ self . get_peer_options ( ) ?
215+ . upstream_tcp_sock_tweak_hook
216+ . as_ref ( )
217+ }
206218}
207219
208220/// A simple TCP or TLS peer without many complicated settings.
@@ -303,7 +315,9 @@ impl Scheme {
303315/// The preferences to connect to a remote server
304316///
305317/// See [`Peer`] for the meaning of the fields
306- #[ derive( Clone , Debug ) ]
318+ #[ non_exhaustive]
319+ #[ derive( Clone , Derivative ) ]
320+ #[ derivative( Debug ) ]
307321pub struct PeerOptions {
308322 pub bind_to : Option < BindTo > ,
309323 pub connection_timeout : Option < Duration > ,
@@ -335,6 +349,9 @@ pub struct PeerOptions {
335349 pub tracer : Option < Tracer > ,
336350 // A custom L4 connector to use to establish new L4 connections
337351 pub custom_l4 : Option < Arc < dyn L4Connect + Send + Sync > > ,
352+ #[ derivative( Debug = "ignore" ) ]
353+ pub upstream_tcp_sock_tweak_hook :
354+ Option < Arc < dyn Fn ( & TcpSocket ) -> Result < ( ) > + Send + Sync + ' static > > ,
338355}
339356
340357impl PeerOptions {
@@ -363,6 +380,7 @@ impl PeerOptions {
363380 tcp_fast_open : false ,
364381 tracer : None ,
365382 custom_l4 : None ,
383+ upstream_tcp_sock_tweak_hook : None ,
366384 }
367385 }
368386
0 commit comments