timerpool

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2023 License: MIT-0 Imports: 2 Imported by: 0

Documentation

Overview

Package timerpool implements a global timer pool.

Example
package main

import (
	"context"
	"fmt"
	"sync"
	"time"

	"github.com/env25/mpdlrc/internal/timerpool"
)

func main() {
	var wg sync.WaitGroup

	ctx1 := context.TODO()

	wg.Add(2)

	timer1 := timerpool.Get(time.Second)
	go func() {
		defer wg.Done()

		select {
		case <-ctx1.Done():
			timerpool.Put(timer1, false)
			return
		case <-timer1.C:
			timerpool.Put(timer1, true)
		}

		fmt.Println("1")
	}()

	ctx2, cancel2 := context.WithCancel(ctx1)

	timer2 := timerpool.Get(time.Second)
	go func() {
		defer wg.Done()

		select {
		case <-ctx2.Done():
			timerpool.Put(timer2, false)
			return
		case <-timer2.C:
			timerpool.Put(timer2, true)
		}

		fmt.Println("2")
	}()

	cancel2()

	wg.Wait()

}
Output:

1

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

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

Get returns a timer for the given duration d from the pool.

func Put

func Put(timer *time.Timer, consumed bool)

Put returns t to the pool. Use true as consumed if you have received from C.

timer cannot be accessed after returning to the pool.

Types

This section is empty.

Jump to

Keyboard shortcuts

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