github.com/ngicks/gokugen@v0.0.5/scheduler/README.md (about)

     1  # Scheduler
     2  
     3  In-memory scheduler.
     4  
     5  ## Needed Goroutines
     6  
     7  Scheduler needs 2+n goroutines, 1 for dispatch loop 1 for canceller loop and n for workers.
     8  
     9  ## Execution Delay
    10  
    11  The delay between scheduled time and actual work invocation is typically under 30 milli secs. But you do want to do your own benchmark at your own setup.
    12  
    13  ## Parts
    14  
    15  ### Task
    16  
    17  `Task` is defined as a minimum set of data relevant to scheduling, dispatching and executing. `Task` has scheduled-time and work, which is function to be executed on scheduled time, and some internal state like cancelled or done.
    18  
    19  `Task` will be stored in min-heap.
    20  
    21  ### Min-heap
    22  
    23  Scheduler stores tasks to the min-heap. It is a priority queue that prioritize least scheduled time, meaning earlist is most. It relies on [std container/heap](https://pkg.go.dev/container/heap@go1.18.3) implementation, which means element addition and retrieval is O(log n) where n = len of elements.
    24  
    25  ### TaskTimer
    26  
    27  TaskTimer is wrapper of min-heap.
    28  
    29  It sets timer to min element when task push / pop.
    30  
    31  Popped tasks are sent to Worker-s via a channel.
    32  
    33  ### Workers and WorkerPool
    34  
    35  Worker is executor of tasks. Does work on single task at a time.
    36  
    37  WorkerPool is, as its name says, a pool of Worker. It provides a way to dynamically increase and decrease workers. That number limits how many tasks can be worked on concurrently. Zero worker = no task can be sent on channel. So it should be at least 1.