Documentation ¶
Overview ¶
Example (Idler) ¶
package main import ( "sync" "time" "github.com/sthaha/delayed" "github.com/sthaha/delayed/testutils" ) type idlerstat int const ( running idlerstat = 0 idled idlerstat = 1 ) var stat = running func status() idlerstat { return stat } var unidleRequested = false var fn delayed.Fn func unidle() { info := testutils.Logger("unidle") info("-------------------------------") if unidleRequested { info("Already scheduled to unidle, ignoring this call") return } unidleRequested = true info("unidle: going to delayed unidle") fn.Reset(3*time.Second, func() { info("unidle: unidling ...") if status() == running { info("unidle: No need to idle since it is already unidled") return } stat = running unidleRequested = false }) } func idle() { info := testutils.Logger("idle") info("-------------------------------") if unidleRequested { info("Ignoring Idle request since unidle has been requested earlier") return } info("going to delayed idle") fn.Reset(3*time.Second, func() { if status() == idled { info("No need to idle since it is already idled") return } info("idling ... - START") time.Sleep(500 * time.Millisecond) info("idling ... - DONE") stat = idled }) } func simulateIdler(wg *sync.WaitGroup) { defer wg.Done() idle() time.Sleep(200 * time.Millisecond) idle() time.Sleep(200 * time.Millisecond) unidle() time.Sleep(200 * time.Millisecond) idle() time.Sleep(200 * time.Millisecond) unidle() time.Sleep(5 * time.Second) idle() } func main() { info := testutils.Logger("main") wg := &sync.WaitGroup{} wg.Add(1) info("starting test run") simulateIdler(wg) wg.Wait() info("done") // HACK: output below is used as hack to run the test // run this using go test -race -run Example }
Output:
Example (Long_running_fn) ¶
package main import ( "fmt" "sync/atomic" "time" "github.com/sthaha/delayed" "github.com/sthaha/delayed/testutils" ) func main() { // debug := testutils.Logger("main") var count uint64 fn, _ := delayed.Call(100*time.Millisecond, func() { debug := testutils.Logger("first") debug("going to execute") atomic.AddUint64(&count, 1) // simulate some activity while a Reset could be called from // else where in the code time.Sleep(500 * time.Millisecond) debug("execute done") }) // give enough time for the call to run time.Sleep(110 * time.Millisecond) debug("going to schedule another") fn.Reset(100*time.Millisecond, func() { debug := testutils.Logger("second") debug("going to execute") atomic.AddUint64(&count, 1) time.Sleep(200 * time.Millisecond) debug("execute done") }) time.Sleep(110 * time.Millisecond) called := atomic.LoadUint64(&count) fmt.Printf("not mutex protected thus count is %d\n", called) // wait for both to finish time.Sleep(800 * time.Millisecond) called = atomic.LoadUint64(&count) fmt.Printf("expect calls to be %d\n", called) }
Output: not mutex protected thus count is 2 expect calls to be 2
Example (Long_running_fn_fixed) ¶
package main import ( "fmt" "sync" "sync/atomic" "time" "github.com/sthaha/delayed" "github.com/sthaha/delayed/testutils" ) func main() { debug := testutils.Logger("main") var count uint64 var m sync.Mutex fn, _ := delayed.Call(100*time.Millisecond, func() { m.Lock() defer m.Unlock() debug := testutils.Logger("first") debug("going to execute") atomic.AddUint64(&count, 1) // simulate some activity while a Reset could be called from // else where in the code time.Sleep(500 * time.Millisecond) debug("execute done") }) // give enough time for the call to run time.Sleep(110 * time.Millisecond) debug("going to schedule another") fn.Reset(100*time.Millisecond, func() { m.Lock() defer m.Unlock() debug := testutils.Logger("second") debug("going to execute") atomic.AddUint64(&count, 1) time.Sleep(200 * time.Millisecond) debug("execute done") }) time.Sleep(110 * time.Millisecond) called := atomic.LoadUint64(&count) fmt.Printf("mutex protected thus count is %d\n", called) // wait for both to finish time.Sleep(800 * time.Millisecond) called = atomic.LoadUint64(&count) fmt.Printf("expect calls to be %d\n", called) time.Sleep(800 * time.Millisecond) }
Output: mutex protected thus count is 1 expect calls to be 2
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Callable ¶ added in v0.3.0
type Callable interface { Call() error Cancel() bool Reset(time.Duration, func()) error ResetDelay(time.Duration) error ResetFunc(func()) error }
Callable is the interface a delayed function call must satisfy
type Fn ¶
type Fn struct {
// contains filtered or unexported fields
}
Fn represents a fun to called after some duration has elapsed
func Call ¶
Call executes a fn after the duration and returns a handle to the function so that it can be cancelled or overridden
func (*Fn) ResetDelay ¶ added in v0.2.0
ResetDelay resets the delay and starts again
Click to show internal directories.
Click to hide internal directories.