Skip to content

Commit cd35949

Browse files
authored
Merge pull request #9 from chainbound/feature/pubsub-v1
Implement pubsub v1
2 parents 10070e6 + cc9f79f commit cd35949

File tree

31 files changed

+2477
-88
lines changed

31 files changed

+2477
-88
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
/target
2+
.vscode/
3+
*.svg

Cargo.lock

Lines changed: 173 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
members = ["msg", "msg-socket", "msg-wire", "msg-transport"]
33

44
[workspace.package]
5-
version = "0.1.0"
5+
version = "0.1.0-alpha"
66
edition = "2021"
7-
rust-version = "1.70" # Remember to update .clippy.toml and README.md
7+
rust-version = "1.70" # Remember to update .clippy.toml and README.md
88
license = "MIT OR Apache-2.0"
9+
description = "A flexible and lightweight messaging library for distributed systems"
910
authors = ["Jonas Bostoen", "Nicolas Racchi"]
1011
homepage = "https://github.com/chainbound/msg-rs"
1112
repository = "https://github.com/chainbound/msg-rs"
@@ -20,7 +21,7 @@ async-trait = "0.1"
2021
tokio = { version = "1", features = ["full"] }
2122
tokio-util = { version = "0.7", features = ["codec"] }
2223
futures = "0.3"
23-
tokio-stream = "0.1"
24+
tokio-stream = { version = "0.1", features = ["sync"] }
2425
parking_lot = "0.12"
2526

2627
bytes = "1"
@@ -35,6 +36,9 @@ rustc-hash = "1"
3536
opt-level = 1
3637
overflow-checks = false
3738

39+
[profile.bench]
40+
debug = true
41+
3842
[profile.maxperf]
3943
inherits = "release"
4044
debug = false

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ It was built because we needed a Rust-native messaging library like those above.
2020

2121
- [ ] Multiple socket types
2222
- [x] Request/Reply
23-
- [ ] Publish/Subscribe
23+
- [x] Publish/Subscribe
2424
- [ ] Channel
2525
- [ ] Push/Pull
2626
- [ ] Survey/Respond
@@ -71,6 +71,39 @@ async fn main() {
7171
println!("Response: {:?}", res);
7272
}
7373
```
74+
75+
### Publish/Subscribe
76+
```rust
77+
use bytes::Bytes;
78+
use tokio_stream::StreamExt;
79+
80+
use msg::{PubSocket, SubSocket, Tcp};
81+
82+
#[tokio::main]
83+
async fn main() {
84+
// Initialize the publisher socket (server side) with a transport
85+
let mut pub_socket = PubSocket::new(Tcp::new());
86+
pub_socket.bind("0.0.0.0:4444").await.unwrap();
87+
88+
// Initialize the subscriber socket (client side) with a transport
89+
let mut sub_socket = SubSocket::new(Tcp::new());
90+
sub_socket.connect("0.0.0.0:4444").await.unwrap();
91+
92+
let topic = "some_interesting_topic".to_string();
93+
94+
// Subscribe to a topic
95+
sub_socket.subscribe(topic.clone()).await.unwrap();
96+
97+
tokio::spawn(async move {
98+
// Values are `bytes::Bytes`
99+
pub_socket.publish(topic, Bytes::from("hello_world")).await.unwrap();
100+
});
101+
102+
let msg = sub_socket.next().await.unwrap();
103+
println!("Received message: {:?}", msg);
104+
}
105+
```
106+
74107
## MSRV
75108
The minimum supported Rust version is 1.70.
76109

msg-socket/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1+
use std::net::SocketAddr;
2+
use tokio::io::{AsyncRead, AsyncWrite};
3+
4+
#[path = "pub/mod.rs"]
5+
mod pubs;
16
mod rep;
27
mod req;
8+
mod sub;
39

410
use bytes::Bytes;
11+
pub use pubs::{PubError, PubOptions, PubSocket};
512
pub use rep::*;
613
pub use req::*;
14+
pub use sub::*;
715

816
pub struct RequestId(u32);
917

@@ -24,3 +32,9 @@ impl RequestId {
2432
pub trait Authenticator: Send + Sync + Unpin + 'static {
2533
fn authenticate(&self, id: &Bytes) -> bool;
2634
}
35+
36+
pub(crate) struct AuthResult<S: AsyncRead + AsyncWrite> {
37+
id: Bytes,
38+
addr: SocketAddr,
39+
stream: S,
40+
}

0 commit comments

Comments
 (0)