Producer处理客户端请求线程模型分析
Producer线程模型分析
线程传递过程:网络eventloop线程接收请求->传给group线程处理->group处理完后->返还给网络线程去响应
eventloop线程找业务线程:通过查找operationMeta获取,operationMeta是存储处理业务逻辑元数据信息,代码AbstractRestInvocation中体现这一过程
try {
operationMeta.getExecutor().execute(() -> {
synchronized (this.requestEx) {
try {
if (isInQueueTimeout()) {
throw new InvocationException(Status.INTERNAL_SERVER_ERROR, "Timeout when processing the request.");
}
if (requestEx.getAttribute(RestConst.REST_REQUEST) != requestEx) {
// already timeout
// in this time, request maybe recycled and reused by web container, do not use requestEx
LOGGER.error("Rest request already timeout, abandon execute, method {}, operation {}.",
operationMeta.getHttpMethod(),
operationMeta.getMicroserviceQualifiedName());
return;
}runOnExecutor();
} catch (InvocationException e) {
LOGGER.error("Invocation failed, cause={}", e.getMessage());
sendFailResponse(e);
} catch (Throwable e) {
LOGGER.error("Processing rest server request error", e);
sendFailResponse(e);
}
}
});
} catch (Throwable e) {
LOGGER.error("failed to schedule invocation, message={}, executor={}.", e.getMessage(), e.getClass().getName());
sendFailResponse(e);
}
业务线程找eventloop线程:eventloop网络线程在放到responseEx上下文中,这个在VertxRestDispatcher可以看到responseEx预置了网络线程
HttpServletResponseEx responseEx = new VertxServerResponseToHttpServletResponse(context.response());
//vertx context 设置
public VertxServerResponseToHttpServletResponse(HttpServerResponse serverResponse) {
this.context = Vertx.currentContext();
this.serverResponse = serverResponse;
Objects.requireNonNull(context, "must run in vertx context.");
}
线程切换过程详细
1.接收请求的vertx eventloop线程
文章图片
2.从vertx eventloop线程转入业务线程处理
文章图片
3.进入ProducerOperationHandler的handler,仍然是业务线程group
文章图片
4.进入ProducerOperationHandler的syncInvoke的,最终进入sendResponseQuietly,线程仍然是group
// ProducerOperationHandler的asyncResp
public void syncInvoke(Invocation invocation, SwaggerProducerOperation producerOperation, AsyncResponse asyncResp) {
....
asyncResp.handle(response);
}//上述的asyncResp正是AbstractRestInvocation传进去的sendResponseQuietlyprotected void doInvoke() throws Throwable {
invocation.onStartHandlersRequest();
invocation.next(resp -> sendResponseQuietly(resp));
}
文章图片
5.响应 group线程,通过驱动responseEx去响应,把响应交由eventloop线程去处理,eventloop线程在responseEx的context,即context.runOnContext(V -> internalFlushBuffer());
// responseEx
protected void onExecuteHttpServerFiltersFinish(Response response, Throwable e) {
...
responseEx.flushBuffer();
...
}// 进入VertxServerResponseToHttpServletResponse的flushBuffer
public void flushBuffer() {
if (context == Vertx.currentContext()) {
internalFlushBuffer();
return;
}
context.runOnContext(V -> internalFlushBuffer());
}//通过serverResponse.end(bodyBuffer)发送响应,vertx框架做的事情public void internalFlushBuffer() {
...
serverResponse.end(bodyBuffer);
}
【Producer处理客户端请求线程模型分析】
文章图片
推荐阅读
- Java|Java OpenCV图像处理之SIFT角点检测详解
- 事件处理程序
- 爬虫数据处理HTML转义字符
- Android|Android BLE蓝牙连接异常处理
- 【冷处理】亲子时间管理检视Day63
- C#中类的异常处理详解
- JAVA图像处理系列(四)——噪声
- Promise详解
- Python|Python 基于datetime库的日期时间数据处理
- 情感障碍的成因及处理方法