Skip to content

Commit

Permalink
closing row for create and add max logic
Browse files Browse the repository at this point in the history
  • Loading branch information
asabya committed Nov 26, 2020
1 parent 8c5f68c commit 1ab7b93
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
17 changes: 16 additions & 1 deletion lib/indexgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ package lib
import (
"database/sql"
pg "github.com/StreamSpace/ss-dw-indexgen/postgres"
logger "github.com/ipfs/go-log/v2"
"sync"
"time"
)

var log = logger.Logger("indexgen/lib")

type IndexGenerator struct {
lk sync.Mutex
pgUrl string
Expand Down Expand Up @@ -34,15 +38,25 @@ func (i *IndexGenerator) open() (*sql.DB, error) {
}
// If DB was not opened or its not usable, create a new connection
if i.db == nil || err != nil {
if err != nil {
log.Error("Unable to ping closing previous DB", err.Error())
i.db.Close()
}
log.Info("Opening new DB")
i.db, err = sql.Open("postgres", i.pgUrl)
if err != nil {
return nil, err
}
i.db.SetMaxOpenConns(5)
i.db.SetMaxIdleConns(5)
i.db.SetConnMaxLifetime(5*time.Minute)
} else {
log.Info("Using existing DB")
}
return i.db, nil
}

func (i *IndexGenerator) McGenrate(
func (i *IndexGenerator) McGenerate(
key string,
ip string,
customerId string) (int64, error) {
Expand Down Expand Up @@ -73,5 +87,6 @@ func (i *IndexGenerator) Close() error {
if i.db != nil {
return i.db.Close()
}
log.Debug("Closing DB")
return nil
}
3 changes: 2 additions & 1 deletion postgres/postgre.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,12 @@ func createTable(db *sql.DB, bcn int64) error {
ip varchar(45),
hash varchar(200),
timestamp timestamp default current_timestamp)`, tablename)
_, err := db.Query(query)
r, err := db.Query(query)
if err != nil {
log.Errorf("Unbale to create table %s", err.Error())
return err
}
r.Close()
return nil
}

Expand Down

0 comments on commit 1ab7b93

Please sign in to comment.