Skip to content

Commit

Permalink
make sure db connections are always closed, set max #
Browse files Browse the repository at this point in the history
  • Loading branch information
nicpottier committed Aug 24, 2017
1 parent 3183f92 commit f497ebf
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 2 deletions.
2 changes: 2 additions & 0 deletions backends/rapidpro/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ func (b *backend) Start() error {
log.Info("db ok")
}
b.db = db
b.db.SetMaxIdleConns(4)
b.db.SetMaxOpenConns(16)

// parse and test our redis config
redisURL, err := url.Parse(b.config.Redis)
Expand Down
1 change: 1 addition & 0 deletions backends/rapidpro/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func insertContact(db *sqlx.DB, contact *DBContact) error {
if err != nil {
return err
}
defer rows.Close()
if rows.Next() {
err = rows.Scan(&contact.ID)
}
Expand Down
2 changes: 1 addition & 1 deletion backends/rapidpro/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func writeChannelLog(b *backend, log *courier.ChannelLog) error {
description = fmt.Sprintf("Error: %s", log.Error)
}

_, err := b.db.Exec(insertLogSQL, dbChan.ID(), log.MsgID, description, log.Error != "", log.Method, log.URL,
_, err := b.db.Exec(insertLogSQL, dbChan.ID(), log.MsgID, description[:255], log.Error != "", log.Method, log.URL,
log.Request, log.Response, log.StatusCode, log.CreatedOn, log.Elapsed/time.Millisecond)

return err
Expand Down
2 changes: 2 additions & 0 deletions backends/rapidpro/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ func writeMsgToDB(b *backend, m *DBMsg) error {
if err != nil {
return err
}
defer rows.Close()

rows.Next()
err = rows.Scan(&m.ID_)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion backends/rapidpro/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func writeMsgStatusToDB(b *backend, status *DBMsgStatus) error {
var rows *sqlx.Rows
var err error

if status.ID() != courier.NilMsgID {
if status.ID() != courier.NilMsgID {
rows, err = b.db.NamedQuery(updateMsgID, status)
} else if status.ExternalID() != "" {
rows, err = b.db.NamedQuery(updateMsgExternalID, status)
Expand All @@ -108,6 +108,7 @@ func writeMsgStatusToDB(b *backend, status *DBMsgStatus) error {
if err != nil {
return err
}
defer rows.Close()

// scan and read the id of the msg that was updated
if rows.Next() {
Expand Down
4 changes: 4 additions & 0 deletions backends/rapidpro/urn.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func contactURNsForContact(db *sqlx.DB, contactID ContactID) ([]*DBContactURN, e
if err != nil {
return nil, err
}
defer rows.Close()

// read our URNs out
urns := make([]*DBContactURN, 0, 3)
Expand Down Expand Up @@ -163,6 +164,7 @@ func insertContactURN(db *sqlx.DB, urn *DBContactURN) error {
return err
}
defer rows.Close()

if rows.Next() {
err = rows.Scan(&urn.ID)
}
Expand All @@ -181,6 +183,8 @@ func updateContactURN(db *sqlx.DB, urn *DBContactURN) error {
if err != nil {
return err
}
defer rows.Close()

if rows.Next() {
rows.Scan(&urn.ID)
}
Expand Down

0 comments on commit f497ebf

Please sign in to comment.