Skip to content

Commit 6dfe698

Browse files
committed
imp(session): implementation for SessionManager
1 parent 3572000 commit 6dfe698

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

src/pyroscope.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::backends::pprof::Pprof;
1515
use crate::backends::Backend;
1616
use crate::error::Result;
1717
use crate::session::Session;
18+
use crate::session::SessionManager;
1819
use 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 {
142149
pub 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;

src/session.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,47 @@
44
// https://www.apache.org/licenses/LICENSE-2.0>. This file may not be copied, modified, or distributed
55
// except according to those terms.
66

7-
use std::{thread, thread::JoinHandle};
7+
use std::{
8+
sync::mpsc::{sync_channel, Receiver, SyncSender},
9+
thread,
10+
thread::JoinHandle,
11+
};
812

913
use crate::pyroscope::PyroscopeConfig;
1014
use crate::utils::merge_tags_with_app_name;
1115
use crate::Result;
1216

17+
/// SessionManager
18+
#[derive(Debug)]
19+
pub struct SessionManager {
20+
pub handle: Option<JoinHandle<Result<()>>>,
21+
pub tx: SyncSender<Session>,
22+
}
23+
24+
impl SessionManager {
25+
/// Create a new SessionManager
26+
pub fn new() -> Result<Self> {
27+
// Create a channel for sending and receiving sessions
28+
let (tx, rx): (SyncSender<Session>, Receiver<Session>) = sync_channel(10);
29+
30+
// Create a thread for the SessionManager
31+
let handle = Some(thread::spawn(move || {
32+
while let Ok(session) = rx.recv() {
33+
session.send()?;
34+
}
35+
Ok(())
36+
}));
37+
38+
Ok(SessionManager { handle, tx })
39+
}
40+
41+
/// Push a new session into the SessionManager
42+
pub fn push(&self, session: Session) -> Result<()> {
43+
self.tx.send(session)?;
44+
Ok(())
45+
}
46+
}
47+
1348
/// Pyroscope Session
1449
#[derive(Clone, Debug)]
1550
pub struct Session {

0 commit comments

Comments
 (0)