From fcc302dcfeb120cf3258949c2f6c7d026a160d22 Mon Sep 17 00:00:00 2001 From: Colin Henry Date: Wed, 10 Jun 2020 16:04:25 -0700 Subject: [PATCH] new db package with actor --- db/actor.go | 24 ++++++++++++++++++++++++ db/doc.go | 14 ++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 db/actor.go create mode 100644 db/doc.go diff --git a/db/actor.go b/db/actor.go new file mode 100644 index 0000000..0604597 --- /dev/null +++ b/db/actor.go @@ -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() + } + } +} diff --git a/db/doc.go b/db/doc.go new file mode 100644 index 0000000..d1d9b18 --- /dev/null +++ b/db/doc.go @@ -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) + +*/ \ No newline at end of file