diff --git a/dbpool.go b/dbpool.go index 8238060..f887e4d 100644 --- a/dbpool.go +++ b/dbpool.go @@ -1,15 +1,16 @@ package dbpool import ( - logging "github.com/NGRsoftlab/ngr-logging" + . "github.com/NGRsoftlab/ngr-logging" "errors" - "github.com/jmoiron/sqlx" "sync" "time" + + "github.com/jmoiron/sqlx" ) -///////Safe db pool map with string in key/////////// +/////// Safe db pool map with string in key /////////// type PoolItem struct { Expiration int64 @@ -27,7 +28,7 @@ type SafeDbMapCache struct { cleanupInterval time.Duration } -// New. Initializing a new memory cache +// New - initializing a new SafeDbMapCache cache func New(defaultExpiration, cleanupInterval time.Duration) *SafeDbMapCache { items := make(map[string]PoolItem) @@ -45,7 +46,7 @@ func New(defaultExpiration, cleanupInterval time.Duration) *SafeDbMapCache { return &cache } -// Set setting a cache by key +// Set - setting *sqlx.DB value by key func (c *SafeDbMapCache) Set(key string, value *sqlx.DB, duration time.Duration) { var expiration int64 @@ -69,7 +70,7 @@ func (c *SafeDbMapCache) Set(key string, value *sqlx.DB, duration time.Duration) } } -// Get getting a cache by key +// Get - getting *sqlx.DB value by key func (c *SafeDbMapCache) Get(key string) (*sqlx.DB, bool) { // changed from RLock to Lock because of line 99 operation (updating creation time) c.Lock() @@ -106,7 +107,7 @@ func (c *SafeDbMapCache) Get(key string) (*sqlx.DB, bool) { return item.Db, true } -// Delete cache by key +// Delete - delete *sqlx.DB value by key // Return false if key not found func (c *SafeDbMapCache) Delete(key string) error { c.Lock() @@ -120,7 +121,7 @@ func (c *SafeDbMapCache) Delete(key string) error { err := connector.Db.Close() if err != nil { - logging.Logger.Warning("db connection close error: ", err) + Logger.Warningf("db connection close error: %s", err.Error()) } delete(c.pool, key) @@ -128,12 +129,12 @@ func (c *SafeDbMapCache) Delete(key string) error { return nil } -// StartGC start Garbage Collection +// StartGC - start Garbage Collection func (c *SafeDbMapCache) StartGC() { go c.GC() } -// GC Garbage Collection +// GC - Garbage Collection cycle func (c *SafeDbMapCache) GC() { for { <-time.After(c.cleanupInterval) @@ -148,7 +149,7 @@ func (c *SafeDbMapCache) GC() { } } -// expiredKeys returns key list which are expired. +// GetItems - returns item list. func (c *SafeDbMapCache) GetItems() (items []string) { c.RLock() defer c.RUnlock() @@ -160,7 +161,7 @@ func (c *SafeDbMapCache) GetItems() (items []string) { return } -// expiredKeys returns key list which are expired. +// ExpiredKeys - returns list of expired keys. func (c *SafeDbMapCache) ExpiredKeys() (keys []string) { c.RLock() defer c.RUnlock() @@ -174,7 +175,7 @@ func (c *SafeDbMapCache) ExpiredKeys() (keys []string) { return } -// clearItems removes all the items which key in keys. +// clearItems - removes all the items with key in keys. func (c *SafeDbMapCache) clearItems(keys []string) { c.Lock() defer c.Unlock() @@ -185,7 +186,7 @@ func (c *SafeDbMapCache) clearItems(keys []string) { if ok { err := connector.Db.Close() if err != nil { - logging.Logger.Warning("db connection close error: ", err) + Logger.Warningf("db connection close error: %s", err.Error()) } } @@ -193,7 +194,7 @@ func (c *SafeDbMapCache) clearItems(keys []string) { } } -// ClearAll removes all the items which key in keys. +// ClearAll - removes all items. func (c *SafeDbMapCache) ClearAll() { c.Lock() defer c.Unlock() @@ -204,7 +205,7 @@ func (c *SafeDbMapCache) ClearAll() { if ok { err := connector.Db.Close() if err != nil { - logging.Logger.Warning("db connection close error: ", err) + Logger.Warningf("db connection close error: %s", err.Error()) } } diff --git a/dbpool_test.go b/dbpool_test.go index dbf3a6a..850ca9d 100644 --- a/dbpool_test.go +++ b/dbpool_test.go @@ -4,7 +4,7 @@ import ( _ "github.com/lib/pq" _ "github.com/mailru/go-clickhouse" - logging "github.com/NGRsoftlab/ngr-logging" + . "github.com/NGRsoftlab/ngr-logging" "context" "fmt" @@ -89,7 +89,7 @@ func TestDbPoolCache(t *testing.T) { db, err := sqlx.ConnectContext(Ctx, driver, connStr) if err != nil { - logging.Logger.Fatal(err) + Logger.Fatal(err) } LocalCache.Set(connStr, db, 10*time.Second) @@ -98,33 +98,33 @@ func TestDbPoolCache(t *testing.T) { cachedRes, ok := LocalCache.Get(connStr) if ok { - logging.Logger.Debug("cached db is here: ", cachedRes) + Logger.Debug("cached db is here: ", cachedRes) // use cachedRes var name string err = cachedRes.GetContext(Ctx, &name, "SELECT name FROM test WHERE id=1") if err != nil { - logging.Logger.Fatal(err) + Logger.Fatal(err) } } else { - logging.Logger.Debug("no res: ", connStr) + Logger.Debug("no res: ", connStr) } time.Sleep(5 * time.Second) cachedRes, ok = LocalCache.Get(connStr) if ok { - logging.Logger.Debug("cached db is here: ", cachedRes) + Logger.Debug("cached db is here: ", cachedRes) // use cachedRes var name string err = cachedRes.GetContext(Ctx, &name, "SELECT name FROM test WHERE id=1") if err != nil { - logging.Logger.Fatal(err) + Logger.Fatal(err) } } else { - logging.Logger.Debug("no res: ", connStr) + Logger.Debug("no res: ", connStr) } } diff --git a/getter.go b/getter.go index 13a0b3a..337e79f 100644 --- a/getter.go +++ b/getter.go @@ -1,14 +1,15 @@ package dbpool import ( - errorCustom "github.com/NGRsoftlab/error-lib" - "context" + "errors" + "time" + "github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx/reflectx" - "time" ) +// GetConnectionByParams - get *sqlx.DB from cache (if exists) or create new and put into cache func GetConnectionByParams(Ctx context.Context, connCache *SafeDbMapCache, duration time.Duration, driver, connString string) (*sqlx.DB, error) { @@ -26,7 +27,7 @@ func GetConnectionByParams(Ctx context.Context, connCache *SafeDbMapCache, //create conn db, err := sqlx.ConnectContext(Ctx, driver, connString) if err != nil { - return nil, errorCustom.GlobalErrors.ErrBadDbConn() + return nil, err } //db.SetMaxIdleConns(10) @@ -38,7 +39,7 @@ func GetConnectionByParams(Ctx context.Context, connCache *SafeDbMapCache, conn, ok = connCache.Get(connString) if !ok && conn == nil { - return nil, errorCustom.GlobalErrors.ErrBadDbConn() + return nil, errors.New("no conn in connCache") } return conn, nil