gopool

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

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

Go to latest
Published: Jun 11, 2019 License: MIT Imports: 6 Imported by: 3

README

gopool

A pool library for golang

Quick Example

import (
  "github.com/lock-free/gopool"
  "time"
)

// define resource
type Res = func(int, int) int
res := func(a int, b int) int {
	return a + b
}

// create pool
getNewItem := func(onItemBoken gopool.OnItemBorken) (*gopool.Item, error) {
	return &gopool.Item{res, func() {}}, nil
}

pool := GetPool(getNewItem, 8, 3000*time.Millisecond)

// Get resource

res, err := pool.Get()

if err != nil {
  // handle
} else {
  res.(Res)(2, 3) // 5
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CleanFunction

type CleanFunction = func()

type GetNewItem

type GetNewItem = func(OnItemBorken) (*Item, error)

get new item and can know the moment when it brokes

type Item

type Item struct {
	Resouce interface{}   // keep Resouce
	Clean   CleanFunction // Clean Resouce TODO with DLB
}

type OnItemBorken

type OnItemBorken = func()

type Pool

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

define pool data structure

func GetPool

func GetPool(getNewItem GetNewItem, size int, duration time.Duration, retryDuration time.Duration) *Pool

get a pool, need to provide:

(1) getNewItem: how to get a new item
(2) size
(3) duration to get a new item

func (*Pool) Get

func (pool *Pool) Get() (interface{}, error)

func (*Pool) GetItemNum

func (pool *Pool) GetItemNum() int

func (*Pool) Shutdown

func (pool *Pool) Shutdown()

shut down a pool normally this is used for testing

Jump to

Keyboard shortcuts

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