GithubHelp home page GithubHelp logo

generic-pool's Introduction

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

generic-pool's People

Contributors

axetroy avatar

Stargazers

ALYR avatar  avatar likegod avatar  avatar Pine Mizune avatar

Watchers

 avatar  avatar

generic-pool's Issues

确保多进程下,资源池的数据安全

I'm submitting a ... (check one with "x")

  • bug report => search github for a similar issue or PR before submitting
  • feature request
  • support request

Current behavior

Expected behavior

More detail

Please tell us about your environment:

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.