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.
Install the package with:
$ go get github.com/norwik/garment
Here is an example:
package main
import (
"errors"
"fmt"
"github.com/norwik/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")) // <nil>
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
}
For transparency into our release cycle and in striving to maintain backward compatibility, Garment is maintained under the Semantic Versioning guidelines and release process is predictable and business-friendly.
See the Releases section of our GitHub project for changelogs for each release version of Garment. It contains summaries of the most noteworthy changes made in each release.
If you have any suggestions, bug reports, or annoyances please report them to our issue tracker at https://github.com/norwik/garment/issues
If you discover a security vulnerability within Garment, please send an email to hello@clivern.com
We are an open source, community-driven project so please feel free to join us. see the contributing guidelines for more details.
© 2021, Clivern. Released under MIT License.
Garment is authored and maintained by @Clivern.