hub

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2024 License: MIT Imports: 2 Imported by: 0

README

Hub

Go doc

暂无介绍...

目录导航

列出了该 package 下所有的函数及类型定义,可通过目录导航进行快捷跳转 ❤️

展开 / 折叠目录导航

包级函数定义

函数名称 描述
NewObjectPool 创建一个 ObjectPool

类型定义

类型 名称 描述
STRUCT ObjectPool 基于 sync.Pool 实现的线程安全的对象池

详情信息

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

创建一个 ObjectPool

示例代码:


func ExampleNewObjectPool() {
	var p = hub.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.Release(&m)
	fmt.Println(m)
}

查看 / 收起单元测试

func TestNewObjectPool(t *testing.T) {
	var cases = []struct {
		name        string
		generator   func() *map[string]int
		releaser    func(data *map[string]int)
		shouldPanic bool
	}{{name: "TestNewObjectPool_NilGenerator", generator: nil, releaser: func(data *map[string]int) {
	}, shouldPanic: true}, {name: "TestNewObjectPool_NilReleaser", generator: func() *map[string]int {
		return &map[string]int{}
	}, releaser: nil, shouldPanic: true}, {name: "TestNewObjectPool_NilGeneratorAndReleaser", generator: nil, releaser: nil, shouldPanic: true}, {name: "TestNewObjectPool_Normal", generator: func() *map[string]int {
		return &map[string]int{}
	}, releaser: func(data *map[string]int) {
	}, shouldPanic: false}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			defer func() {
				if err := recover(); c.shouldPanic && err == nil {
					t.Error("TestNewObjectPool should panic")
				}
			}()
			_ = hub.NewObjectPool[map[string]int](c.generator, c.releaser)
		})
	}
}


ObjectPool STRUCT

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

  • 一些高频临时生成使用的对象可以通过 ObjectPool 进行管理,例如属性计算等
type ObjectPool[T any] struct {
	p        sync.Pool
	releaser func(data T)
}

func (*ObjectPool) Get() T

获取一个对象

查看 / 收起单元测试

func TestObjectPool_Get(t *testing.T) {
	var cases = []struct {
		name      string
		generator func() *map[string]int
		releaser  func(data *map[string]int)
	}{{name: "TestObjectPool_Get_Normal", generator: func() *map[string]int {
		return &map[string]int{}
	}, releaser: func(data *map[string]int) {
		for k := range *data {
			delete(*data, k)
		}
	}}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			pool := hub.NewObjectPool[map[string]int](c.generator, c.releaser)
			if actual := pool.Get(); len(*actual) != 0 {
				t.Error("TestObjectPool_Get failed")
			}
		})
	}
}


func (*ObjectPool) Release(data T)

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

查看 / 收起单元测试

func TestObjectPool_Release(t *testing.T) {
	var cases = []struct {
		name      string
		generator func() *map[string]int
		releaser  func(data *map[string]int)
	}{{name: "TestObjectPool_Release_Normal", generator: func() *map[string]int {
		return &map[string]int{}
	}, releaser: func(data *map[string]int) {
		for k := range *data {
			delete(*data, k)
		}
	}}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			pool := hub.NewObjectPool[map[string]int](c.generator, c.releaser)
			msg := pool.Get()
			m := *msg
			m["test"] = 1
			pool.Release(msg)
			if len(m) != 0 {
				t.Error("TestObjectPool_Release failed")
			}
		})
	}
}


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/utils/collection"
	"github.com/kercylan98/minotaur/utils/hub"
)

func main() {
	var p = hub.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.Release(&m)
	fmt.Println(m)
}
Output:

map[]

func (*ObjectPool[T]) Get

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

Get 获取一个对象

func (*ObjectPool[T]) Release

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

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

Jump to

Keyboard shortcuts

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