/pool

A pool to reuse something in golang, usually net connection

Primary LanguageGoMIT LicenseMIT

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