Help us shape the future of Go! Take the 2026 Go Developer Survey

pool

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

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

Go to latest
Published: Aug 7, 2016 License: MIT Imports: 2 Imported by: 1

README

pool

A pool to reuse something, usually connection ....

Getting Started

It's simple to use

package main

import (
    "errors"
    "fmt"
    "github.com/quorzz/pool"
    "github.com/quorzz/redis-protocol"
    "strings"
)

var create = func() (interface{}, error) {
    return protocol.NewReader(strings.NewReader("$5\r\nconfig\r\n")), nil
}

var checkItem = func(item interface{}) error {

    if nil == item {
        return errors.New("can not be a nil")
    }
    if _, isOk := item.(*protocol.Reader); !isOk {
        return errors.New("not a reader")
    }

    return nil
}

func main() {

    p := pool.NewPool(create, 30)
    p.CheckOnGet = checkItem

    item, err := p.Get()
    if err != nil {
        fmt.Println(err)
    }

    reader, _ := item.(*protocol.Reader)

    reader.ReadMessage()

    p.Put(reader)

    fmt.Println(p.Len()) // 1

    p.Clear()

    fmt.Println(p.Len()) // 0
}

License

The MIT License (MIT) Copyright (c) 2016 quorzz

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CreateFunc

type CreateFunc func() (interface{}, error)

type Pool

type Pool struct {
	MaxIdle int
	Checker func(item interface{}) error
	// contains filtered or unexported fields
}

func NewPool

func NewPool(createFunc CreateFunc, maxIdel int) *Pool

func (*Pool) Clear

func (p *Pool) Clear()

func (*Pool) Get

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

func (*Pool) Len

func (p *Pool) Len() int

func (*Pool) Put

func (p *Pool) Put(item interface{})

Jump to

Keyboard shortcuts

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