Skip to content

Commit

Permalink
temp fix for subscription memory issues
Browse files Browse the repository at this point in the history
still need real fix

Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
jb55 committed Feb 7, 2024
1 parent 3dec975 commit 9a9c157
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ bindgen = "0.69.1"
bindgen = []

[dependencies]
env_logger = "0.10.1"
flatbuffers = "23.5.26"
libc = "0.2.151"
log = "0.4.20"
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"

[dev-dependencies]
hex = "0.4.3"
2 changes: 1 addition & 1 deletion nostrdb
Submodule nostrdb updated 2 files
+7 −1 ndb.c
+6 −8 src/nostrdb.c
2 changes: 1 addition & 1 deletion shell.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{ pkgs ? import <nixpkgs> {} }:
with pkgs;
mkShell {
nativeBuildInputs = [ rustPlatform.bindgenHook cargo rustc rustfmt libiconv pkg-config ];
nativeBuildInputs = [ rustPlatform.bindgenHook cargo rustc rustfmt libiconv pkg-config valgrind ];

LIBCLANG_PATH="${pkgs.llvmPackages.libclang.lib}/lib";
}
12 changes: 9 additions & 3 deletions src/filter.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::bindings;
use crate::Note;
use std::ffi::CString;
use std::marker::PhantomPinned;
use std::os::raw::c_char;
use std::ptr::null_mut;
use tracing::debug;

#[derive(Debug)]
pub struct Filter {
Expand All @@ -17,7 +19,7 @@ impl bindings::ndb_filter {

impl Filter {
pub fn new() -> Filter {
let null = std::ptr::null_mut();
let null = null_mut();
let mut filter_data = bindings::ndb_filter {
elem_buf: bindings::cursor {
start: null,
Expand All @@ -30,7 +32,7 @@ impl Filter {
end: null,
},
num_elements: 0,
current: std::ptr::null_mut(),
current: null_mut(),
elements: [
null_mut(),
null_mut(),
Expand Down Expand Up @@ -115,7 +117,7 @@ impl Filter {
unsafe { bindings::ndb_filter_end_field(self.as_mut_ptr()) }
}

pub fn authors<'a>(self, authors: Vec<&'a [u8; 32]>) -> Filter {
pub fn authors(self, authors: Vec<&[u8; 32]>) -> Filter {
self.start_authors_field();
for author in authors {
self.add_id_element(author);
Expand Down Expand Up @@ -170,8 +172,12 @@ impl Filter {
}
}

/*
// This is unsafe.. but we still need a way to free the memory on these
impl Drop for Filter {
fn drop(&mut self) {
debug!("dropping filter {:?}", self);
unsafe { bindings::ndb_filter_destroy(self.as_mut_ptr()) };
}
}
*/
15 changes: 6 additions & 9 deletions src/ndb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl Ndb {
Ok(())
}

pub fn subscribe<'a>(&self, filter: &'a Filter) -> Result<Subscription<'a>> {
pub fn subscribe(&self, filter: Filter) -> Result<Subscription> {
unsafe {
let id = bindings::ndb_subscribe(self.as_ptr(), filter.as_mut_ptr(), 1);
if id == 0 {
Expand All @@ -92,7 +92,7 @@ impl Ndb {
}
}

pub fn poll_for_notes<'a>(&self, sub: &Subscription<'a>, max_notes: u32) -> Vec<u64> {
pub fn poll_for_notes(&self, sub: &Subscription, max_notes: u32) -> Vec<u64> {
let mut vec = vec![];
vec.reserve_exact(max_notes as usize);
let sub_id = sub.id;
Expand All @@ -110,11 +110,7 @@ impl Ndb {
vec
}

pub async fn wait_for_notes<'a>(
&self,
sub: &Subscription<'a>,
max_notes: u32,
) -> Result<Vec<u64>> {
pub async fn wait_for_notes(&self, sub: &Subscription, max_notes: u32) -> Result<Vec<u64>> {
let ndb = self.clone();
let sub_id = sub.id;
let handle = task::spawn_blocking(move || {
Expand Down Expand Up @@ -256,11 +252,12 @@ mod tests {
async fn subscribe_event_works() {
let db = "target/testdbs/subscribe";
test_util::cleanup_db(&db);
tracing_subscriber::fmt::init();

{
let ndb = Ndb::new(db, &Config::new()).expect("ndb");
let filter = Filter::new().kinds(vec![1]);
let sub = ndb.subscribe(&filter).expect("sub_id");
let sub = ndb.subscribe(filter).expect("sub_id");
let waiter = ndb.wait_for_notes(&sub, 1);
ndb.process_event(r#"["EVENT","b",{"id": "702555e52e82cc24ad517ba78c21879f6e47a7c0692b9b20df147916ae8731a3","pubkey": "32bf915904bfde2d136ba45dde32c88f4aca863783999faea2e847a8fafd2f15","created_at": 1702675561,"kind": 1,"tags": [],"content": "hello, world","sig": "2275c5f5417abfd644b7bc74f0388d70feb5d08b6f90fa18655dda5c95d013bfbc5258ea77c05b7e40e0ee51d8a2efa931dc7a0ec1db4c0a94519762c6625675"}]"#).expect("process ok");
let res = waiter.await.expect("await ok");
Expand All @@ -276,7 +273,7 @@ mod tests {
{
let ndb = Ndb::new(db, &Config::new()).expect("ndb");
let filter = Filter::new().kinds(vec![1]);
let sub = ndb.subscribe(&filter).expect("sub_id");
let sub = ndb.subscribe(filter).expect("sub_id");
ndb.process_event(r#"["EVENT","b",{"id": "702555e52e82cc24ad517ba78c21879f6e47a7c0692b9b20df147916ae8731a3","pubkey": "32bf915904bfde2d136ba45dde32c88f4aca863783999faea2e847a8fafd2f15","created_at": 1702675561,"kind": 1,"tags": [],"content": "hello, world","sig": "2275c5f5417abfd644b7bc74f0388d70feb5d08b6f90fa18655dda5c95d013bfbc5258ea77c05b7e40e0ee51d8a2efa931dc7a0ec1db4c0a94519762c6625675"}]"#).expect("process ok");
// this is too fast, we should have nothing
let res = ndb.poll_for_notes(&sub, 1);
Expand Down
4 changes: 2 additions & 2 deletions src/subscription.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::Filter;

pub struct Subscription<'a> {
pub filter: &'a Filter,
pub struct Subscription {
pub filter: Filter,
pub id: u64,
}

0 comments on commit 9a9c157

Please sign in to comment.