Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aj/bound event bus #240

Merged
merged 6 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions bottlecap/src/event_bus/bus.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
use std::sync::mpsc;
use std::sync::mpsc::Sender;
use std::sync::mpsc::{self, SyncSender};

use crate::events;

use crate::event_bus::constants::MAX_EVENTS;

pub struct EventBus {
tx: Sender<events::Event>,
tx: SyncSender<events::Event>,
pub rx: mpsc::Receiver<events::Event>,
}

impl EventBus {
pub fn run() -> EventBus {
let (tx, rx) = mpsc::channel();
let (tx, rx) = mpsc::sync_channel(MAX_EVENTS);
EventBus { tx, rx }
}

pub fn get_sender_copy(&self) -> Sender<events::Event> {
pub fn get_sender_copy(&self) -> SyncSender<events::Event> {
self.tx.clone()
}
}
1 change: 1 addition & 0 deletions bottlecap/src/event_bus/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub const MAX_EVENTS: usize = 100;
duncanista marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions bottlecap/src/event_bus/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod bus;
mod constants;
6 changes: 4 additions & 2 deletions bottlecap/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,10 @@ fn build_function_arn(account_id: &str, region: &str, function_name: &str) -> St

fn main() -> Result<()> {
// First load the configuration
let lambda_directory = env::var("LAMBDA_TASK_ROOT")
.expect("unable to read environment variable: LAMBDA_TASK_ROOT");
let lambda_directory = match env::var("LAMBDA_TASK_ROOT") {
Ok(val) => val,
Err(_) => "/var/task".to_string(),
};
let config = match config::get_config(Path::new(&lambda_directory)) {
Ok(config) => Arc::new(config),
Err(e) => {
Expand Down
6 changes: 3 additions & 3 deletions bottlecap/src/metrics/dogstatsd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::mpsc::Sender;
use std::sync::mpsc::SyncSender;

use tracing::{error, info};

Expand All @@ -24,7 +24,7 @@ pub struct DogStatsDConfig {
}

impl DogStatsD {
pub fn run(config: &DogStatsDConfig, event_bus: Sender<events::Event>) -> DogStatsD {
pub fn run(config: &DogStatsDConfig, event_bus: SyncSender<events::Event>) -> DogStatsD {
let aggr: Arc<Mutex<Aggregator<{ constants::CONTEXTS }>>> = Arc::new(Mutex::new(
Aggregator::<{ constants::CONTEXTS }>::new().expect("failed to create aggregator"),
));
Expand All @@ -45,7 +45,7 @@ impl DogStatsD {
fn run_server(
host: &str,
port: u16,
event_bus: Sender<events::Event>,
event_bus: SyncSender<events::Event>,
aggregator: Arc<Mutex<Aggregator<{ constants::CONTEXTS }>>>,
) -> std::thread::JoinHandle<()> {
let addr = format!("{}:{}", host, port);
Expand Down
10 changes: 5 additions & 5 deletions bottlecap/src/telemetry/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::telemetry::events::TelemetryEvent;

use std::collections::HashMap;
use std::error::Error;
use std::sync::mpsc::Sender;
use std::sync::mpsc::SyncSender;
use std::{
io::{Read, Write},
net::{TcpListener, TcpStream},
Expand Down Expand Up @@ -118,7 +118,7 @@ pub struct TelemetryListenerConfig {
impl TelemetryListener {
pub fn run(
config: &TelemetryListenerConfig,
event_bus: Sender<events::Event>,
event_bus: SyncSender<events::Event>,
) -> Result<TelemetryListener, Box<dyn Error>> {
let addr = format!("{}:{}", &config.host, &config.port);
let listener = TcpListener::bind(addr)?;
Expand Down Expand Up @@ -152,7 +152,7 @@ impl TelemetryListener {
fn handle_stream(
stream: &mut impl Read,
mut buf: [u8; 262144],
event_bus: Sender<events::Event>,
event_bus: SyncSender<events::Event>,
) -> Result<(), Box<dyn Error>> {
// Read into buffer
#![allow(clippy::unused_io_amount)]
Expand Down Expand Up @@ -294,7 +294,7 @@ mod tests {
let mut stream = MockTcpStream {
data: "POST /path HTTP/1.1\r\nContent-Length: 335\r\nHeader1: Value1\r\n\r\n[{\"time\":\"2024-04-25T17:35:59.944Z\",\"type\":\"platform.initStart\",\"record\":{\"initializationType\":\"on-demand\",\"phase\":\"init\",\"runtimeVersion\":\"nodejs:20.v22\",\"runtimeVersionArn\":\"arn:aws:lambda:us-east-1::runtime:da57c20c4b965d5b75540f6865a35fc8030358e33ec44ecfed33e90901a27a72\",\"functionName\":\"hello-world\",\"functionVersion\":\"$LATEST\"}}]".to_string().into_bytes(),
};
let (tx, rx) = std::sync::mpsc::channel();
let (tx, rx) = std::sync::mpsc::sync_channel(3);
let buf = [0; 262144];
let result = TelemetryListener::handle_stream(&mut stream, buf, tx);
let event = rx.recv().expect("No events received");
Expand Down Expand Up @@ -323,7 +323,7 @@ mod tests {
let mut stream = MockTcpStream {
data: $value.to_string().into_bytes(),
};
let (tx, _) = std::sync::mpsc::channel();
let (tx, _) = std::sync::mpsc::sync_channel(4);
let buf = [0; 262144];
TelemetryListener::handle_stream(&mut stream, buf, tx).unwrap()
}
Expand Down