pools

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ObjectPool

type ObjectPool[T any] struct {
	// contains filtered or unexported fields
}

ObjectPool 基于 sync.Pool 实现的线程安全的对象池

  • 一些高频临时生成使用的对象可以通过 ObjectPool 进行管理,例如属性计算等

func NewObjectPool

func NewObjectPool[T any](generator func() *T, releaser func(data *T)) *ObjectPool[*T]

NewObjectPool 创建一个 ObjectPool

Example
package main

import (
	"fmt"
	"github.com/kercylan98/minotaur/toolkit/collection"
	"github.com/kercylan98/minotaur/toolkit/pools"
)

func main() {
	var p = pools.NewObjectPool[map[int]int](func() *map[int]int {
		return &map[int]int{}
	}, func(data *map[int]int) {
		collection.ClearMap(*data)
	})

	m := *p.Get()
	m[1] = 1
	p.Put(&m)
	fmt.Println(m)
}
Output:

map[]

func (*ObjectPool[T]) Get

func (op *ObjectPool[T]) Get() T

Get 获取一个对象

func (*ObjectPool[T]) Put

func (op *ObjectPool[T]) Put(data T)

Put 将使用完成的对象放回缓冲区

type SchedulerPool

type SchedulerPool struct {
	// contains filtered or unexported fields
}

SchedulerPool 是一个线程安全的 chrono.Scheduler 对象池

func NewSchedulerPool

func NewSchedulerPool(capacity int, tick time.Duration, wheelSize int64, hungry ...bool) *SchedulerPool

NewSchedulerPool 创建一个 SchedulerPool 对象

  • capacity: 调度器池容量
  • tick: 时间轮刻度
  • wheelSize: 时间轮大小
  • hungry: 是否饥饿模式,饥饿模式下,调度器池默认将创建 capacity 指定数量的调度器

func (*SchedulerPool) Get

func (p *SchedulerPool) Get() *chrono.Scheduler

Get 获取一个调度器

func (*SchedulerPool) Put

func (p *SchedulerPool) Put(scheduler *chrono.Scheduler)

Put 将使用完成的调度器放回缓冲区,如果缓冲区已满,则关闭调度器并释放资源

Jump to

Keyboard shortcuts

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