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

有关于优雅停机的示例吗 #23

Open
fengxiqaq opened this issue Sep 27, 2024 · 1 comment
Open

有关于优雅停机的示例吗 #23

fengxiqaq opened this issue Sep 27, 2024 · 1 comment

Comments

@fengxiqaq
Copy link

在哪里实现了相关代码该怎么使用?谢谢解答。

@kwsc98
Copy link
Owner

kwsc98 commented Oct 12, 2024

    async fn monitor(
        port: String,
        route: &'static RpcServerFilter,
        http_codec: Arc<FusenHttpCodec>,
        handler_context: Arc<HandlerContext>,
        mut shutdown: Shutdown,
        shutdown_complete_tx: mpsc::Sender<()>,
    ) -> crate::Result<()> {
        let notify_shutdown = broadcast::channel(1).0;
        let listener = TcpListener::bind(&format!("0.0.0.0:{}", port)).await?;
        let mut builder = hyper_util::server::conn::auto::Builder::new(TokioExecutor::new());
        builder.http2().max_concurrent_streams(None);
        builder.http1().keep_alive(true);
        let builder = Arc::new(builder);
        loop {
            let tcp_stream = tokio::select! {
                _ = shutdown.recv() => {
                    drop(notify_shutdown);
                    drop(shutdown_complete_tx);
                    return Ok(());
                },
                res = listener.accept() => res
            };
            match tcp_stream {
                Ok(stream) => {
                    let stream_handler = StreamHandler {
                        builder: builder.clone(),
                        tcp_stream: stream.0,
                        route,
                        http_codec: http_codec.clone(),
                        handler_context: handler_context.clone(),
                        shutdown: notify_shutdown.subscribe(),
                        _shutdown_complete: shutdown_complete_tx.clone(),
                    };
                    debug!("socket stream connect, addr: {:?}", stream.1);
                    tokio::spawn(stream_handler.run_http());
                }
                Err(err) => error!("tcp connect, err: {:?}", err),
            }
        }
    }

可以参考这段代码,每次服务端新接受到socket连接时,我们都会利用管道维护状态信息,利用broadcast管道监听停机信号,然后利用mpsc管道,维护当前运行中的tcp连接。包括再停机时,也会先向注册中心进行服务注销,然后延迟关闭

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants