From a7a89d5b5a4426fa5bf85f2d82f7385fd45dd9e3 Mon Sep 17 00:00:00 2001 From: XieHan Date: Tue, 16 Nov 2021 01:45:58 +0800 Subject: [PATCH] Defer deleting server task to increase performance --- src/factory/WFTask.h | 18 ------------ src/factory/WFTask.inl | 66 ++++++++++++++++++++++++++++++------------ 2 files changed, 48 insertions(+), 36 deletions(-) diff --git a/src/factory/WFTask.h b/src/factory/WFTask.h index 43da0b0b..a5de6dad 100644 --- a/src/factory/WFTask.h +++ b/src/factory/WFTask.h @@ -285,24 +285,6 @@ protected: virtual int receive_timeout() { return this->receive_timeo; } virtual int keep_alive_timeout() { return this->keep_alive_timeo; } -protected: - virtual SubTask *done() - { - SeriesWork *series = series_of(this); - - if (this->state == WFT_STATE_SYS_ERROR && this->error < 0) - { - this->state = WFT_STATE_SSL_ERROR; - this->error = -this->error; - } - - if (this->callback) - this->callback(this); - - delete this; - return series->pop(); - } - protected: int send_timeo; int receive_timeo; diff --git a/src/factory/WFTask.inl b/src/factory/WFTask.inl index 941fbd65..78a346bc 100644 --- a/src/factory/WFTask.inl +++ b/src/factory/WFTask.inl @@ -73,6 +73,24 @@ protected: return NULL; } +protected: + virtual SubTask *done() + { + SeriesWork *series = series_of(this); + + if (this->state == WFT_STATE_SYS_ERROR && this->error < 0) + { + this->state = WFT_STATE_SSL_ERROR; + this->error = -this->error; + } + + if (this->callback) + this->callback(this); + + delete this; + return series->pop(); + } + public: void set_prepare(std::function *)> prep) { @@ -101,6 +119,20 @@ protected: virtual CommMessageIn *message_in() { return &this->req; } virtual void handle(int state, int error); +protected: + /* CommSession::get_connection() is supposed to be called only in the + * implementations of it's virtual functions. As a server task, to call + * this function after process() and before callback() is very dangerous + * and should be blocked. */ + virtual WFConnection *get_connection() const + { + if (this->processor.task) + return (WFConnection *)this->CommSession::get_connection(); + + errno = EPERM; + return NULL; + } + protected: virtual void dispatch() { @@ -119,22 +151,23 @@ protected: this->subtask_done(); } - /* CommSession::get_connection() is supposed to be called only in the - * implementations of it's virtual functions. As a server task, to call - * this function after process() and before callback() is very dangerous - * and should be blocked. */ - virtual WFConnection *get_connection() const + virtual SubTask *done() { - if (this->processor.task) - return (WFConnection *)this->CommSession::get_connection(); + SeriesWork *series = series_of(this); - errno = EPERM; - return NULL; + if (this->state == WFT_STATE_SYS_ERROR && this->error < 0) + { + this->state = WFT_STATE_SSL_ERROR; + this->error = -this->error; + } + + if (this->callback) + this->callback(this); + + /* Defer deleting the task. */ + return series->pop(); } -protected: - CommService *service; - protected: class Processor : public SubTask { @@ -169,17 +202,15 @@ protected: SeriesWork(&task->processor, nullptr) { this->set_last_task(task); - this->service = task->service; - this->service->incref(); + this->task = task; } virtual ~Series() { - this->callback = nullptr; - this->service->decref(); + delete this->task; } - CommService *service; + WFServerTask *task; }; public: @@ -188,7 +219,6 @@ public: WFNetworkTask(NULL, scheduler, nullptr), processor(this, proc) { - this->service = service; } protected: