Skip to content

Commit

Permalink
Merge pull request #115 from chenyukang/yukang-add-reuse
Browse files Browse the repository at this point in the history
Add reuse address and port for CI issues
  • Loading branch information
quake authored Jun 29, 2024
2 parents f84cb3d + bba1704 commit cdaf341
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ ractor = "0.9.7"
arcode = "0.2.4"
nom = "7.1.3"
regex = "1.10.5"
socket2 = "0.5.7"

[profile.release]
panic = "abort"
Expand Down
29 changes: 28 additions & 1 deletion src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,41 @@ use tokio::sync::mpsc::Sender;

pub type InvoiceCommandWithReply = (InvoiceCommand, Sender<crate::Result<String>>);

fn build_server(addr: &str) -> Server {
#[cfg(not(release))]
{
// Use socket2 to set reuse address and reuse port,
// so that we can restart the server without waiting for the port to be released.
// it will avoid the error: "Address already in use" in CI.
use socket2::{Domain, Socket, Type};
let addr = addr.parse().unwrap();
let domain = Domain::for_address(addr);
let socket = Socket::new(domain, Type::STREAM, None).unwrap();
socket.set_nonblocking(true).unwrap();
socket.set_reuse_address(true).expect("set reuse address");
socket.set_reuse_port(true).expect("set reuse port");

socket.bind(&addr.into()).unwrap();
socket.listen(4096).unwrap();

jsonrpsee::server::Server::builder()
.build_from_tcp(socket)
.unwrap()
}
#[cfg(release)]
{
Server::builder().build(addr).await.unwrap()
}
}

pub async fn start_rpc<S: ChannelActorStateStore + InvoiceStore + Clone + Send + Sync + 'static>(
config: RpcConfig,
ckb_network_actor: Option<ActorRef<NetworkActorMessage>>,
cch_command_sender: Option<Sender<CchCommand>>,
store: S,
) -> ServerHandle {
let listening_addr = config.listening_addr.as_deref().unwrap_or("[::]:0");
let server = Server::builder().build(listening_addr).await.unwrap();
let server = build_server(listening_addr);
let mut methods = InvoiceRpcServerImpl::new(store.clone()).into_rpc();
if let Some(ckb_network_actor) = ckb_network_actor {
let peer = PeerRpcServerImpl::new(ckb_network_actor.clone());
Expand Down

0 comments on commit cdaf341

Please sign in to comment.