Skip to content

Instantly share code, notes, and snippets.

@mrb
Forked from rday/gist:3504674
Last active December 17, 2015 15:19
Show Gist options
  • Select an option

  • Save mrb/5630865 to your computer and use it in GitHub Desktop.

Select an option

Save mrb/5630865 to your computer and use it in GitHub Desktop.

Revisions

  1. mrb renamed this gist May 22, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @rday rday revised this gist Aug 28, 2012. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -5,14 +5,22 @@ type ConnectionPoolWrapper struct {
    conn chan interface{}
    }

    /**
    Call the init function size times. If the init function fails during any call, then
    the creation of the pool is considered a failure.
    We call the same function size times to make sure each connection shares the same
    state.
    */
    func (p *ConnectionPoolWrapper) InitPool(size int, initfn InitFunction) error {
    // Create a buffered channel allowing size senders
    p.conn = make(chan interface{}, size)
    for x := 0; x < size; x++ {
    fmt.Println("Calling init function ", x)
    conn, err := initfn()
    if err != nil {
    return err
    }

    // If the init function succeeded, add the connection to the channel
    p.conn <- conn
    }
    p.size = size
  3. @rday rday created this gist Aug 28, 2012.
    30 changes: 30 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    type InitFunction func() (interface{}, error)

    type ConnectionPoolWrapper struct {
    size int
    conn chan interface{}
    }

    func (p *ConnectionPoolWrapper) InitPool(size int, initfn InitFunction) error {
    p.conn = make(chan interface{}, size)
    for x := 0; x < size; x++ {
    fmt.Println("Calling init function ", x)
    conn, err := initfn()
    if err != nil {
    return err
    }
    p.conn <- conn
    }
    p.size = size
    return nil
    }

    func (p *ConnectionPoolWrapper) GetConnection() interface{} {
    return <-p.conn
    }

    func (p *ConnectionPoolWrapper) ReleaseConnection(conn interface{}) {
    p.conn <- conn
    }

    var dbPool = &ConnectionPoolWrapper{}