Skip to content

Commit

Permalink
Merge pull request #256 from pace/255-couchdb-dedi-client-healthcheck
Browse files Browse the repository at this point in the history
Use secondary client for couchdb healthchecks #255
  • Loading branch information
Ferlonas committed Mar 18, 2021
2 parents 7b3dc59 + a3827fd commit 378d8c0
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions backend/couchdb/db.go
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/caarlos0/env"
"github.com/go-kivik/couchdb/v3"
kivik "github.com/go-kivik/kivik/v3"

"github.com/pace/bricks/http/transport"
"github.com/pace/bricks/maintenance/health/servicehealthcheck"
"github.com/pace/bricks/maintenance/log"
Expand All @@ -25,31 +26,46 @@ func Database(name string) (*kivik.DB, error) {
if err != nil {
return nil, err
}
client, err := Client(cfg)
// Primary client+db
_, db, err := clientAndDB(ctx, name, cfg)
if err != nil {
return nil, err
}

// use default db
if name == "" {
name = cfg.Database
}

db := client.DB(ctx, name, nil)
if db.Err() != nil {
return nil, db.Err()
// Secondary (healthcheck) client+db
healthCheckClient, healthCheckDB, err := clientAndDB(ctx, name, cfg)
if err != nil {
return nil, err
}

servicehealthcheck.RegisterHealthCheck("couchdb("+name+")", &HealthCheck{
Name: name,
Client: client,
DB: db,
Client: healthCheckClient,
DB: healthCheckDB,
Config: cfg,
})

return db, nil
}

func clientAndDB(ctx context.Context, dbName string, cfg *Config) (*kivik.Client, *kivik.DB, error) {
client, err := Client(cfg)
if err != nil {
return nil, nil, err
}

// use default db
if dbName == "" {
dbName = cfg.Database
}

db := client.DB(ctx, dbName, nil)
if db.Err() != nil {
return nil, nil, db.Err()
}
return client, db, err
}

func Client(cfg *Config) (*kivik.Client, error) {
ctx := log.WithContext(context.Background())

Expand Down

0 comments on commit 378d8c0

Please sign in to comment.