File: ticker.go

package info (click to toggle)
golang-github-savsgio-gotils 0.0~git20250408.196191e-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 176 kB
  • sloc: makefile: 2
file content (31 lines) | stat: -rw-r--r-- 482 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package time

import (
	"sync"
	"time"
)

var tickerPool = sync.Pool{}

// AcquireTicker returns a ticker from the pool if possible.
func AcquireTicker(d time.Duration) *time.Ticker {
	v := tickerPool.Get()
	if v == nil {
		return time.NewTicker(d)
	}

	t, ok := v.(*time.Ticker)
	if !ok {
		panic("unexpected type of time.Ticker")
	}

	t.Reset(d)

	return t
}

// ReleaseTicker returns a ticker into the pool.
func ReleaseTicker(tm *time.Ticker) {
	tm.Stop()
	tickerPool.Put(tm)
}