Skip to content

Commit

Permalink
reduce the dbblocker to use the callback blocker
Browse files Browse the repository at this point in the history
  • Loading branch information
dan9186 committed Apr 11, 2021
1 parent d25b31e commit f44b1a9
Showing 1 changed file with 4 additions and 31 deletions.
35 changes: 4 additions & 31 deletions dbblocker/dbblocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,11 @@ package dbblocker
import (
"database/sql"
"time"
)

// Blocker represents a SQL database connection to monitor for connectivity
// and block until a connection is established.
type Blocker struct {
db *sql.DB
}
"github.com/gomicro/blockit/cbblocker"
)

// New takes a SQL database object and returns a newly instantiated Blocker
func New(db *sql.DB) *Blocker {
return &Blocker{db: db}
}

// Blockit meets the blocker interface. It returns a read only channel that will
// receive true when the database is connected.
func (d *Blocker) Blockit() <-chan bool {
connected := make(chan bool)

go func() {
for {
err := d.db.Ping()
if err != nil {
<-time.After(1 * time.Second)
continue
}

connected <- true
close(connected)

break
}
}()

return connected
func New(db *sql.DB) *cbblocker.Blocker {
return cbblocker.New(db.Ping, 1*time.Second)
}

0 comments on commit f44b1a9

Please sign in to comment.