Skip to content

Commit

Permalink
Merge pull request #1 from ElrondfromRussia/main
Browse files Browse the repository at this point in the history
Main fix: return error (error from sqlx instead of DbConnErr)
  • Loading branch information
ElrondfromRussia authored Jan 19, 2022
2 parents 8698831 + 8875cd2 commit f80bfa1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
33 changes: 17 additions & 16 deletions dbpool.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

Expand All @@ -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

Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -120,20 +121,20 @@ 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)

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)
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -185,15 +186,15 @@ 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())
}
}

delete(c.pool, k)
}
}

// ClearAll removes all the items which key in keys.
// ClearAll - removes all items.
func (c *SafeDbMapCache) ClearAll() {
c.Lock()
defer c.Unlock()
Expand All @@ -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())
}
}

Expand Down
16 changes: 8 additions & 8 deletions dbpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
}
11 changes: 6 additions & 5 deletions getter.go
Original file line number Diff line number Diff line change
@@ -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) {

Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit f80bfa1

Please sign in to comment.