garment

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 6, 2021 License: MIT Imports: 2 Imported by: 0

README

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:

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")) // <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
}

Versioning

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.

Bug tracker

If you have any suggestions, bug reports, or annoyances please report them to our issue tracker at https://github.com/spacewalkio/garment/issues

Security Issues

If you discover a security vulnerability within Garment, please send an email to hello@clivern.com

Contributing

We are an open source, community-driven project so please feel free to join us. see the contributing guidelines for more details.

License

© 2021, SpaceWalk. Released under MIT License.

Garment is authored and maintained by @SpaceWalk.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CloseCallback

type CloseCallback func(interface{}) error

CloseCallback type

type Connection

type Connection struct {
	Value     interface{}
	Ping      PingCallback
	Close     CloseCallback
	Reconnect ReconnectCallback
}

Connection type

type PingCallback

type PingCallback func(interface{}) error

PingCallback type

type Pool

type Pool struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Pool type

func NewPool

func NewPool() *Pool

NewPool creates a new instance of Pool

func (*Pool) Close

func (c *Pool) Close(key string) error

Close close the connection and removes the key

func (*Pool) Count

func (c *Pool) Count() int

Count counts the number of managed connections

func (*Pool) Get

func (c *Pool) Get(key string) interface{}

Get gets a connection with a key

func (*Pool) Has

func (c *Pool) Has(key string) bool

Has checks if connection exists

func (*Pool) Ping

func (c *Pool) Ping(key string) error

Ping checks the connection status

func (*Pool) Reconnect

func (c *Pool) Reconnect(key string) error

Reconnect reconnects again

func (*Pool) Remove

func (c *Pool) Remove(key string)

Remove removes a connection

func (*Pool) Set

func (c *Pool) Set(key string, value interface{}, ping PingCallback, close CloseCallback, reconnect ReconnectCallback)

Set sets a connection with a key

type ReconnectCallback

type ReconnectCallback func(interface{}) error

ReconnectCallback type

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL