timerpool

package module
v0.0.0-...-d7ed527 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2019 License: MIT Imports: 2 Imported by: 0

README

GoDoc Go Report Card code-coverage

timerpool

timer pool for high performance time out control (pooled time.AfterFunc/time.NewTimer)

use example with context

ctx, cancel := context.WithCancel(parentCtx)
t := GetTimerWithAfterFunc(time.Second*5, cancel)
PutTimerWithAfterFunc(t)
cancel()

use example with timer.C

timer:=GetTimer(time.Second*5)
select {
case <-timer.C:
	// timeout
	PutTimer(timer)
	return
	// other case ...
}
PutTimer(timer)

benchmark result

go test -run TimeoutControl -bench TimeoutControl
goos: darwin
goarch: amd64
BenchmarkTimeoutControlViaContextWithTimeout-8                   2272261               523 ns/op
BenchmarkTimeoutControlViaContextWithCancelAndRawTimer-8         4370292               267 ns/op
BenchmarkTimeoutControlViaContextWithCancelAndTimerPool-8        4662602               253 ns/op
BenchmarkTimeoutControlViaRawTimer-8                             4829988               245 ns/op
BenchmarkTimeoutControlViaTimerPool-8                            9144037               126 ns/op

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetTimer

func GetTimer(d time.Duration) *time.Timer

GetTimer get a timer from pool or create from time.NewTimer

func GetTimerWithAfterFunc

func GetTimerWithAfterFunc(d time.Duration, f func()) *timerWithAfterFunc

GetTimerWithAfterFunc get a timer from pool or create from time.AfterFunc

func PutTimer

func PutTimer(t *time.Timer)

PutTimer stop a timer and return into pool

func PutTimerWithAfterFunc

func PutTimerWithAfterFunc(t *timerWithAfterFunc)

PutTimerWithAfterFunc stop a timer and return into pool

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL