šŸš€ Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more →
Socket
Sign inDemoInstall
Socket

github.com/mcfly722/gopackages/scheduler

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/mcfly722/gopackages/scheduler

v0.0.0-20221002101216-6539da77cc99
Source
Go
Version published
Created
Source

scheduler

This scheduler collects objects to queue according it's deadline time. Checking the first one deadline element in this queue, you can understand is there other deadlines in this queue occurs or not.

Usage

1. Create scheduler:

scheduler := NewScheduler()

2. Insert some elements with it deadline times:

now := time.Now()
scheduler.RegisterNewTimer(now.Add(2*time.Second), object2)
scheduler.RegisterNewTimer(now.Add(3*time.Second), object3)
scheduler.RegisterNewTimer(now.Add(3*time.Second), object3)
scheduler.RegisterNewTimer(now.Add(1*time.Second), object1)

3. Check the first one deadline.

If it reached, method scheduler.TakeFirstOutdated() returns object, otherwise it returns nil.

object := scheduler.TakeFirstOutdatedOrNil()
if object != nil {
  // timer for this object is outdated, object pulled from scheduler queue, now make some work with it
}

4. You can cancel particular object from queue

scheduler := scheduler.NewScheduler()
scheduler.RegisterNewTimer(time.Now(), 1)
scheduler.RegisterNewTimer(time.Now(), 1)
scheduler.RegisterNewTimer(time.Now(), 2)
scheduler.RegisterNewTimer(time.Now(), 1)
scheduler.RegisterNewTimer(time.Now(), 2)
scheduler.RegisterNewTimer(time.Now(), 2)
scheduler.CancelTimerFor(1)
scheduler.CancelTimerFor(2)

Several same objects in queue supported

5. Or cancel all scheduler timers

scheduler.CancelAllTimers()

Limitations and specific

  • Currently insertion of new deadline works for O(n) time (n - is the average size of deadlines queue)
    For large schedulers which have more than 1000 timers, it would be nice to use more optimized insertion algorithm with O(log(n)) time.
  • All operations with scheduler are thread safe.

FAQs

Package last updated on 02 Oct 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts