Skip to content

Do not use a separate thread for scheduling deferred tasks if possible. #817

@trustin

Description

@trustin

For simplicity, current MultithreadEventLoopGroup implementation starts a dedicated separate scheduler thread to schedule the tasks submitted via schedule*() methods. However, this is often unnecessary because SingleThreadEventLoop can adjust its wait time dynamically.

Before:

Scheduler thread:
  - Keeps watching the scheduled task queue and calls SingleThreadEventLoop.execute() on time.

After

SingleThreadEventLoop:
  - Determine how long should the thread wait at maximum to run the scheduled task with 
    closest dead line.
  - poll() on a task queue or select() on a selector with the timeout calculated above.
  - If waken up early due to other events like selector.wakeup(), we can always re-calculate 
    the new timeout.

The advantage of this change is that it reduces the latency between the scheduler and the thread that executes the scheduled task. (from non-zero to zero) .. and it's more elegant.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions