handle_one_connection()

本文详细介绍了MySQL中处理单个查询请求的过程。从初始化线程到认证用户,再到执行查询和清理连接,深入剖析了核心函数do_command(thd)的工作原理。

函数所在文件:sql/sql_parse.cc

该函数是处理单个查询请求的接口函数。

主要流程如下:

1.初始化线程

hread_scheduler.init_new_connection_thread()

2.在完成必要的检查和初始化后,进入一个for(;;)循环。

3.认证用户

if (login_connection(thd))
  goto end_thread;

4.初始化thd来接受查询

prepare_new_connection_state(thd);

5,执行查询代码

    while (!net->error && net->vio != 0 &&
           !(thd->killed == THD::KILL_CONNECTION))
    {
      if (do_command(thd))
	break;
    }

可见,do_command(thd)是实际处理查询的函数。

6.进入到连接结束和清理阶段

close_connection(thd, 0, 1);
    if (thread_scheduler.end_thread(thd,1))
      return 0;                                 // Probably no-threads
上述语句将调用mysqld.cc中的one_thread_per_connection_end() ,选择结束或缓存线程。
esp_err_t esp_http_client_perform(esp_http_client_handle_t client) { esp_err_t err; do { if (client->process_again) { esp_http_client_prepare(client); } switch (client->state) { /* In case of blocking esp_http_client_perform(), the following states will fall through one after the after; in case of non-blocking esp_http_client_perform(), if there is an error condition, like EINPROGRESS or EAGAIN, then the esp_http_client_perform() API will return ESP_ERR_HTTP_EAGAIN error. The user may call esp_http_client_perform API again, and for this reason, we maintain the states */ case HTTP_STATE_INIT: if ((err = esp_http_client_connect(client)) != ESP_OK) { if (client->is_async && err == ESP_ERR_HTTP_CONNECTING) { return ESP_ERR_HTTP_EAGAIN; } http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0); http_dispatch_event_to_event_loop(HTTP_EVENT_ERROR, &client, sizeof(esp_http_client_handle_t)); return err; } /* falls through */ case HTTP_STATE_CONNECTED: if ((err = esp_http_client_request_send(client, client->post_len)) != ESP_OK) { if (client->is_async && errno == EAGAIN) { return ESP_ERR_HTTP_EAGAIN; } http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0); http_dispatch_event_to_event_loop(HTTP_EVENT_ERROR, &client, sizeof(esp_http_client_handle_t)); return err; } /* falls through */ case HTTP_STATE_REQ_COMPLETE_HEADER: if ((err = esp_http_client_send_post_data(client)) != ESP_OK) { if (client->is_async && errno == EAGAIN) { return ESP_ERR_HTTP_EAGAIN; } http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0); http_dispatch_event_to_event_loop(HTTP_EVENT_ERROR, &client, sizeof(esp_http_client_handle_t)); return err; } /* falls through */ case HTTP_STATE_REQ_COMPLETE_DATA: /* Disable caching response body, as data should * be handled by application event handler */ client->cache_data_in_fetch_hdr = 0; int64_t ret = esp_http_client_fetch_headers(client); if (ret < 0) { if ((client->is_async && errno == EAGAIN) || ret == -ESP_ERR_HTTP_EAGAIN) { return ESP_ERR_HTTP_EAGAIN; } /* Enable caching after error condition because next * request could be performed using native APIs */ client->cache_data_in_fetch_hdr = 1; if (esp_transport_get_errno(client->transport) == ENOTCONN) { ESP_LOGW(TAG, "Close connection due to FIN received"); esp_http_client_close(client); http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0); http_dispatch_event_to_event_loop(HTTP_EVENT_ERROR, &client, sizeof(esp_http_client_handle_t)); return ESP_ERR_HTTP_CONNECTION_CLOSED; } http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0); http_dispatch_event_to_event_loop(HTTP_EVENT_ERROR, &client, sizeof(esp_http_client_handle_t)); return ESP_ERR_HTTP_FETCH_HEADER; } /* falls through */ case HTTP_STATE_RES_ON_DATA_START: /* Enable caching after fetch headers state because next * request could be performed using native APIs */ client->cache_data_in_fetch_hdr = 1; if ((err = esp_http_check_response(client)) != ESP_OK) { ESP_LOGE(TAG, "Error response"); http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0); http_dispatch_event_to_event_loop(HTTP_EVENT_ERROR, &client, sizeof(esp_http_client_handle_t)); return err; } while (client->response->is_chunked && !client->is_chunk_complete) { if (esp_http_client_get_data(client) <= 0) { if (client->is_async && errno == EAGAIN) { return ESP_ERR_HTTP_EAGAIN; } ESP_LOGD(TAG, "Read finish or server requests close"); break; } } while (client->response->data_process < client->response->content_length) { if (esp_http_client_get_data(client) <= 0) { if (client->is_async && errno == EAGAIN) { return ESP_ERR_HTTP_EAGAIN; } ESP_LOGD(TAG, "Read finish or server requests close"); break; } } http_dispatch_event(client, HTTP_EVENT_ON_FINISH, NULL, 0); http_dispatch_event_to_event_loop(HTTP_EVENT_ON_FINISH, &client, sizeof(esp_http_client_handle_t)); client->response->buffer->raw_len = 0; if (!http_should_keep_alive(client->parser)) { ESP_LOGD(TAG, "Close connection"); esp_http_client_close(client); } else { if (client->state > HTTP_STATE_CONNECTED) { client->state = HTTP_STATE_CONNECTED; client->first_line_prepared = false; } } break; default: break; } } while (client->process_again); return ESP_OK; }该函数怎么使用
最新发布
11-01
/home/sw/.local/lib/python3.10/site-packages/qpsolvers/conversions/ensure_sparse_matrices.py:24: UserWarning: Converted P to scipy.sparse.csc.csc_matrix For best performance, build P as a scipy.sparse.csc_matrix rather than as a numpy.ndarray warnings.warn( /home/sw/.local/lib/python3.10/site-packages/qpsolvers/conversions/ensure_sparse_matrices.py:24: UserWarning: Converted G to scipy.sparse.csc.csc_matrix For best performance, build G as a scipy.sparse.csc_matrix rather than as a numpy.ndarray warnings.warn( [-0.30434676 -0.78260976] ---------------------------------------- Exception occurred during processing of request from ('127.0.0.1', 52906) Traceback (most recent call last): File "/usr/lib/python3.10/socketserver.py", line 316, in _handle_request_noblock self.process_request(request, client_address) File "/usr/lib/python3.10/socketserver.py", line 347, in process_request self.finish_request(request, client_address) File "/usr/lib/python3.10/socketserver.py", line 360, in finish_request self.RequestHandlerClass(request, client_address, self) File "/home/sw/.local/lib/python3.10/site-packages/swift/SwiftRoute.py", line 343, in __init__ super(MyHttpRequestHandler, self).__init__( File "/usr/lib/python3.10/http/server.py", line 668, in __init__ super().__init__(*args, **kwargs) File "/usr/lib/python3.10/socketserver.py", line 747, in __init__ self.handle() File "/usr/lib/python3.10/http/server.py", line 433, in handle self.handle_one_request() File "/usr/lib/python3.10/http/server.py", line 421, in handle_one_request method() File "/home/sw/.local/lib/python3.10/site-packages/swift/SwiftRoute.py", line 391, in do_GET self.send_file_via_real_path() File "/home/sw/.local/lib/python3.10/site-packages/swift/SwiftRoute.py", line 419, in send_file_via_real_path self.copyfile(f, self.wfile) File "/usr/lib/python3.10/http/server.py", line 875, in copyfile shutil.copyfileobj(source, outputfile) File
03-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值