Created
January 6, 2023 14:42
-
-
Save ChillFish8/f2de6a6b5245e0bc10992a8a687414a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use datacake_node::{ClusterExtension, DatacakeNode}; | |
use async_trait::async_trait; | |
pub struct MyExtension; | |
#[async_trait] | |
impl ClusterExtension for MyExtension { | |
type Output = (); | |
type Error = MyError; | |
async fn init_extension( | |
self, | |
node: &DatacakeNode, | |
) -> Result<Self::Output, Self::Error> { | |
// In here we can setup our system using the live node. | |
// This gives us things like the cluster clock and RPC server: | |
println!("Creating my extension!"); | |
let timestamp = node.clock().get_time().await; | |
println!("My timestamp: {timestamp}"); | |
Ok(()) | |
} | |
} | |
pub struct MyError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::net::SocketAddr; | |
use datacake_node::{ConnectionConfig, DCAwareSelector, DatacakeNodeBuilder}; | |
#[tokio::main] | |
async fn main() -> anyhow::Result<()> { | |
let bind_addr = "127.0.0.1:8000".parse::<SocketAddr>().unwrap(); | |
// We setup our connection config for the node passing in the bind address, public address and seed nodes. | |
// Here we're just using the bind address as our public address with no seed, but in the real world | |
// this will be a different value when deployed across several servers with seeds to contact. | |
let connection_cfg = ConnectionConfig::new(bind_addr, bind_addr, Vec::<String>::new()); | |
// Our builder lets us configure the node. | |
// | |
// We can configure the node selector, data center of the node, cluster ID, etc... | |
let my_node = DatacakeNodeBuilder::<DCAwareSelector>::new(1, connection_cfg).connect().await?; | |
// Now we're connected we can add any extensions at runtime, our RPC server will already be | |
// running and setup. | |
// | |
// Check out the `datacake-eventual-consistency` implementation for a demo. | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment