【问题标题】:Reactive non-blocking I/O call web socket will stack overflow?反应式非阻塞 I/O 调用 web socket 会堆栈溢出吗?
【发布时间】:2019-12-02 04:21:17
【问题描述】:

在像tomcat servlet这样的传统多线程模型中,我们会同步调用web socket,这样即使速度很慢,我们也可以控制速率。

thread: {
        Obj request;
        // There are at most several socket for threads num
        Obj response = syncClient.blockingWebRequest(request);
        // ...
        logicHandle();
        // ...
        return response;
    }

但是在响应式非阻塞 I/O 中,我们会异步调用 socket,所以如果请求太多,当前会调用很多 web socket。操作系统套接字堆栈可以容纳它吗?那么缓冲区呢?

eventLoop: {
        Mono request;
        // Non-blocking IO continuously receives and establishes socket
        Mono response = asyncClient.nonBlockingWebRequest(request);
        response.onSubscribe(()->{
            // ...
            logicHandle();
            // ...
        });
        return response;
    }

例如同时建立数百万个socket连接。

  • socket 需要 10 秒。
  • CPU 计算时间为 1 毫秒。

在第一个套接字返回之前会建立 10,000 个套接字连接吗?

eventLoop: {
        // eventLoop handles one in 1ms
        Mono request;
        // Non-blocking IO continuously receives and establishes socket
        Mono response = asyncClient.nonBlockingWebRequest(request);
        // This will callback after 10s
        response.onSubscribe(()->{
            // ...
            logicHandle();
            // ...
        });
        // eventLoop continue
        return response;
    }

非常感谢您回答我的问题!

【问题讨论】:

  • 你能告诉我们你的代码吗?
  • 感谢您的建议,我在描述中添加了伪代码

标签: netty spring-webflux reactive


【解决方案1】:

毫无疑问,如果速率非常高,最终缓冲区会被填满。

缓冲区溢出会通过TCP从接收端反馈到发送端。

背压是一种解决方案。

需要明确的是,如果像 RSocket 这样的协议,连接将被重用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 2015-02-24
    相关资源
    最近更新 更多