new db package with actor

This commit is contained in:
Colin Henry 2020-06-10 16:04:25 -07:00
parent 3cedd8564c
commit fcc302dcfe
2 changed files with 38 additions and 0 deletions

24
db/actor.go Normal file
View File

@ -0,0 +1,24 @@
package db
import (
"context"
"database/sql"
)
type Func func(db *sql.DB)
type Actor struct {
DB *sql.DB
ActionChan chan Func
}
func (a *Actor) Run(ctx context.Context) error {
for {
select {
case f := <-a.ActionChan:
f(a.DB)
case <-ctx.Done():
return ctx.Err()
}
}
}

14
db/doc.go Normal file
View File

@ -0,0 +1,14 @@
package db
/*
Example usage:
ctx, _ := context.WithCancel(context.Background())
dba = &db.Actor{
DB: s.db,
ActionChan: make(chan db.Func),
}
go dba.Run(ctx)
*/