@@ -15,6 +15,7 @@ use crate::backends::pprof::Pprof;
1515use crate :: backends:: Backend ;
1616use crate :: error:: Result ;
1717use crate :: session:: Session ;
18+ use crate :: session:: SessionManager ;
1819use crate :: timer:: Timer ;
1920
2021/// Represent PyroscopeAgent Configuration
@@ -93,7 +94,9 @@ impl PyroscopeAgentBuilder {
9394
9495 /// Set the agent backend. Default is pprof.
9596 pub fn backend < T : ' static > ( self , backend : T ) -> Self
96- where T : Backend {
97+ where
98+ T : Backend ,
99+ {
97100 Self {
98101 backend : Arc :: new ( Mutex :: new ( backend) ) ,
99102 ..self
@@ -125,11 +128,15 @@ impl PyroscopeAgentBuilder {
125128 // Start Timer
126129 let timer = Timer :: default ( ) . initialize ( ) ?;
127130
131+ // Start the SessionManager
132+ let session_manager = SessionManager :: new ( ) ?;
133+
128134 // Return PyroscopeAgent
129135 Ok ( PyroscopeAgent {
130136 backend : self . backend ,
131137 config : self . config ,
132138 timer,
139+ session_manager,
133140 tx : None ,
134141 handle : None ,
135142 running : Arc :: new ( ( Mutex :: new ( false ) , Condvar :: new ( ) ) ) ,
@@ -142,6 +149,7 @@ impl PyroscopeAgentBuilder {
142149pub struct PyroscopeAgent {
143150 pub backend : Arc < Mutex < dyn Backend > > ,
144151 timer : Timer ,
152+ session_manager : SessionManager ,
145153 tx : Option < Sender < u64 > > ,
146154 handle : Option < JoinHandle < Result < ( ) > > > ,
147155 running : Arc < ( Mutex < bool > , Condvar ) > ,
@@ -156,6 +164,16 @@ impl Drop for PyroscopeAgent {
156164 // Stop Timer
157165 self . timer . drop_listeners ( ) . unwrap ( ) ; // Drop listeners
158166 self . timer . handle . take ( ) . unwrap ( ) . join ( ) . unwrap ( ) . unwrap ( ) ; // Wait for the Timer thread to finish
167+
168+ // Wait for main thread to finish
169+ self . handle . take ( ) . unwrap ( ) . join ( ) . unwrap ( ) . unwrap ( ) ;
170+ self . session_manager
171+ . handle
172+ . take ( )
173+ . unwrap ( )
174+ . join ( )
175+ . unwrap ( )
176+ . unwrap ( ) ;
159177 }
160178}
161179
@@ -188,11 +206,13 @@ impl PyroscopeAgent {
188206
189207 let config = self . config . clone ( ) ;
190208
209+ let stx = self . session_manager . tx . clone ( ) ;
210+
191211 self . handle = Some ( std:: thread:: spawn ( move || {
192212 while let Ok ( time) = rx. recv ( ) {
193213 let report = backend. lock ( ) ?. report ( ) ?;
194- // start a new session
195- Session :: new ( time, config. clone ( ) , report) ?. send ( ) ?;
214+ // Send new Session to SessionManager
215+ stx . send ( Session :: new ( time, config. clone ( ) , report) ?) ?;
196216
197217 if time == 0 {
198218 let ( lock, cvar) = & * pair;
0 commit comments