Garment

A Thread Safe Connection Pooling.

Garment retains a single connection pool for different database types used inside you application (MySQL, Redis, Etcd … etc). Please note that garment won’t be needed and not recommended if you already preserve the same connection pool across your application sub packages.

Documentation

Usage

Install the package with:

$ go get github.com/spacewalkio/garment

Here is an example:

<div class="highlight highlight-source-go position-relative" data-snippet-clipboard-copy-content="package main

import (
" errors" "fmt" "github.com spacewalkio garment" ) type Database struct { State string } func (d *Database) GetState() string { return d.State } func (d *Database) Terminate() { d.State="disconnected" } func (d *Database) Close() { d.State="disconnected" } func (d *Database) Reconnect() { d.State="connected" } func (d *Database) Ping() bool { if d.State == "connected" { return true } return false } func main() { pool := garment.NewPool() ping := func(con interface{}) error { if con.(*Database).Ping() { return nil } return errors.New("DB connection is lost") } close := func(con interface{}) error { con.(*Database).Close() return nil } reconnect := func(con interface{}) error { con.(*Database).Reconnect() return nil } pool.Set("db", &Database{State: "connected"}, ping, close, reconnect) fmt.Println(pool.Count()) 1 fmt.Println(pool.Has("db")) true fmt.Println(pool.Ping("db"))
fmt.Println(pool.Get(“db”).(*Database).GetState()) // connected
pool.Close(“db”)
fmt.Println(pool.Get(“db”).(*Database).GetState()) // disconnected
pool.Reconnect(“db”)
fmt.Println(pool.Get(“db”).(*Database).GetState()) // connected
pool.Get(“db”).(*Database).Terminate()
fmt.Println(pool.Get(“db”).(*Database).GetState()) // disconnected
pool.Remove(“db”)
fmt.Println(pool.Count()) // 0
}
“>

package main

import (
    "errors"
    "fmt"

    "github.com/spacewalkio/garment"
)

type Database struct {
    State string
}

func (d *Database) GetState() string {
    return d.State
}

func (d *Database) Terminate() {
    d.State = "disconnected"
}

func (d *Database) Close() {
    d.State = "disconnected"
}

func (d *Database) Reconnect() {
    d.State = "connected"
}

func (d *Database) Ping() bool {
    if d.State == "connected" {
        return true
    }

    return false
}

func main() {
    pool := garment.NewPool()

    ping := func(con interface{}) error {
        if con.(*Database).Ping() {
            return nil
        }

        return errors.New("DB connection is lost")
    }

    close := func(con interface{}) error {
        con.(*Database).Close()

        return nil
    }

    reconnect := func(con interface{}) error {
        con.(*Database).Reconnect()

        return nil
    }

    pool.Set("db", &Database{State: "connected"}, ping, close, reconnect)

    fmt.Println(pool.Count()) // 1

    fmt.Println(pool.Has("db"))  // true
    fmt.Println(pool.Ping("db")) // 

    fmt.Println(pool.Get("db").(*Database).GetState()) // connected

    pool.Close("db")

    fmt.Println(pool.Get("db").(*Database).GetState()) // disconnected

    pool.Reconnect("db")

    fmt.Println(pool.Get("db").(*Database).GetState()) // connected

    pool.Get("db").(*Database).Terminate()

    fmt.Println(pool.Get("db").(*Database).GetState()) // disconnected

    pool.Remove("db")

    fmt.Println(pool.Count()) // 0
}