From f497ebffab5bb3f9a8ca23c6b41f5eaf30a9acec Mon Sep 17 00:00:00 2001 From: Nic Pottier Date: Thu, 24 Aug 2017 11:43:13 -0500 Subject: [PATCH] make sure db connections are always closed, set max # --- backends/rapidpro/backend.go | 2 ++ backends/rapidpro/contact.go | 1 + backends/rapidpro/log.go | 2 +- backends/rapidpro/msg.go | 2 ++ backends/rapidpro/status.go | 3 ++- backends/rapidpro/urn.go | 4 ++++ 6 files changed, 12 insertions(+), 2 deletions(-) diff --git a/backends/rapidpro/backend.go b/backends/rapidpro/backend.go index ff0d5349c..69867b116 100644 --- a/backends/rapidpro/backend.go +++ b/backends/rapidpro/backend.go @@ -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) diff --git a/backends/rapidpro/contact.go b/backends/rapidpro/contact.go index 70e113982..391e7fe01 100644 --- a/backends/rapidpro/contact.go +++ b/backends/rapidpro/contact.go @@ -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) } diff --git a/backends/rapidpro/log.go b/backends/rapidpro/log.go index 0e8de0212..d6e7a895e 100644 --- a/backends/rapidpro/log.go +++ b/backends/rapidpro/log.go @@ -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 diff --git a/backends/rapidpro/msg.go b/backends/rapidpro/msg.go index deb1fd411..9ba9da8e0 100644 --- a/backends/rapidpro/msg.go +++ b/backends/rapidpro/msg.go @@ -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 { diff --git a/backends/rapidpro/status.go b/backends/rapidpro/status.go index f6de78af1..e66d947f5 100644 --- a/backends/rapidpro/status.go +++ b/backends/rapidpro/status.go @@ -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) @@ -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() { diff --git a/backends/rapidpro/urn.go b/backends/rapidpro/urn.go index c7d5c364f..9e656a5c8 100644 --- a/backends/rapidpro/urn.go +++ b/backends/rapidpro/urn.go @@ -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) @@ -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) } @@ -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) }