Skip to content

Commit

Permalink
Local cache to reuse db connection (#40)
Browse files Browse the repository at this point in the history
migrate library not closing the db connection db show those connections
are in 'idle' state. When the count reaches the limit database is not
accessible.

so cache the db connection in a map with resouce name as key.
  • Loading branch information
jkraj authored Jul 10, 2018
1 parent 96e0976 commit cc24f83
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions atlas-db-controller/databaseSchemaResource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/golang-migrate/migrate"
"github.com/golang-migrate/migrate/database"
"github.com/golang/glog"
atlas "github.com/infobloxopen/atlas-db/pkg/apis/db/v1alpha1"
"k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -12,6 +13,8 @@ import (
"k8s.io/client-go/tools/cache"
)

var dbDriverMap = make(map[string]database.Driver)

func (c *Controller) syncSchema(key string) error {
glog.Infof("Schema key: %v", key)

Expand Down Expand Up @@ -116,15 +119,29 @@ func (c *Controller) syncSchema(key string) error {
}
}

mgrt, err := migrate.New(gitURL, dsn)
// migrate package is not closing the dbconnnection so using a local cache reuse dbconnection.
// TODO when same resource with different dsn arrives; need to unset local cache during schema resource deletion.
dbDriver, ok := dbDriverMap[schema.Namespace+schema.Name]
if !ok {
dbDriver, err = database.Open(dsn)
if err != nil {
schemaStatusMsg = fmt.Sprintf("failed to open dbconnection: %s", err)
c.updateDatabaseSchemaStatus(key, schema, StateError, schemaStatusMsg)
err = fmt.Errorf(schemaStatusMsg)
runtime.HandleError(err)
return err
}
dbDriverMap[schema.Namespace+schema.Name] = dbDriver
}

mgrt, err := migrate.NewWithDatabaseInstance(gitURL, dbName, dbDriver)
if err != nil {
schemaStatusMsg = fmt.Sprintf("failed to initialize migrate engine: %s", err)
c.updateDatabaseSchemaStatus(key, schema, StateError, schemaStatusMsg)
err = fmt.Errorf(schemaStatusMsg)
runtime.HandleError(err)
return err
}
defer mgrt.Close()

ver, dirt, err := mgrt.Version()
if err != nil {
Expand Down

0 comments on commit cc24f83

Please sign in to comment.