pool

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2019 License: Apache-2.0 Imports: 3 Imported by: 0

README

Generic pool manager

Build Status Coverage Status License

Manage the resource pool, like connection...

Features

  • Thread safety at use
  • Graceful to create/destroy the resource/connection
  • Easy to Use
  • 100% test cover

Usage

go get -v github.com/axetroy/generic-pool
package main

import (
  "github.com/axetroy/generic-pool"
)

type faceConnection struct {
}

func (c *faceConnection) Connect() (err error) {
  return
}

func (c *faceConnection) send(data []byte) {

}

func (c *faceConnection) OnClose(func()) (err error) {
  return
}

func (c *faceConnection) Close() (err error) {
  return
}

func main() {

  p, _ := pool.New(pool.Config{
    Creator: func(p *pool.Pool, id int64) (interface{}, error) {
      // create an face connection
      faceConnection := faceConnection{}

      // connect
      if err := faceConnection.Connect(); err != nil {
        return nil, err
      }

      // when connection close by remote, we should remove it from pool
      faceConnection.OnClose(func() {
        // release the resource
        p.Release(id)
      })

      // return this
      return faceConnection, nil
    },
    Destroyer: func(p *pool.Pool, resource interface{}) (err error) {
      // parse the connection
      faceConnection := resource.(faceConnection)

      return faceConnection.Close()
    },
  }, pool.Options{Min: 5, Max: 50, Idle: 60})

  // Get the resource
  resource, err := p.Get()

  if err != nil {
    panic(err)
  }

  // parse the resource to connection
  faceConnection := resource.(faceConnection)

  defer func() {
    // faceConnection.Close()
    // You don't need to close by manual, resource pool will do this
  }()

  // send data
  faceConnection.send([]byte("Hello world"))

}

Contributing

Contributing Guid

Contributors


Axetroy

💻 🐛 🎨

License

FOSSA Status

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Creator   CreatorFunc
	Destroyer DestroyerFunc
}

type CreatorFunc

type CreatorFunc func(p *Pool, id Id) (resource interface{}, err error)

type DestroyerFunc

type DestroyerFunc func(p *Pool, resource interface{}) (err error)

type Id

type Id int64

type Options

type Options struct {
	Min  int
	Max  int
	Idle int64
}

type Pool

type Pool struct {
	Config    Config
	Options   Options
	Pool      *SafeMap
	Destroyed bool
	// contains filtered or unexported fields
}

func New

func New(c Config, o Options) (p *Pool, err error)

* Create a new pool

func (*Pool) Destroy

func (p *Pool) Destroy() (err error)

* Release the resource

func (*Pool) Get

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

* Get entity

func (*Pool) Release

func (p *Pool) Release(id Id) (err error)

* Release a resource by id

type Resource

type Resource struct {
	Idle      bool
	LastUseAt time.Time
	UseCount  int
	Resource  interface{}
	Id        Id
}

type SafeMap

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

SafeMap is a map with lock

func NewSafeMap

func NewSafeMap() *SafeMap

New return new safemap

func (*SafeMap) Check

func (m *SafeMap) Check(k interface{}) bool

Check Returns true if k is exist in the map.

func (*SafeMap) Count

func (m *SafeMap) Count() int

Count returns the number of items within the map.

func (*SafeMap) Delete

func (m *SafeMap) Delete(k interface{})

Delete the given key and value.

func (*SafeMap) Get

func (m *SafeMap) Get(k interface{}) interface{}

Get from maps return the k's value

func (*SafeMap) Items

func (m *SafeMap) Items() map[interface{}]interface{}

Items returns all items in safemap.

func (*SafeMap) Set

func (m *SafeMap) Set(k interface{}, v interface{}) bool

Set Maps the given key and value. Returns false if the key is already in the map and changes nothing.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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