【发布时间】: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