mdb: add GCE type, refactor PubSub configuration a bit

This commit is contained in:
Brian Picciano 2018-07-18 23:14:18 +00:00
parent 6c17eaa62f
commit 91f56cb40c
3 changed files with 63 additions and 50 deletions

View File

@ -1,3 +1,54 @@
// Package mdb contains a number of database wrappers for databases I commonly // Package mdb contains a number of database wrappers for databases I commonly
// use // use
package mdb package mdb
import (
"context"
"github.com/mediocregopher/mediocre-go-lib/mcfg"
"github.com/mediocregopher/mediocre-go-lib/mlog"
"google.golang.org/api/option"
)
// DefaultGCEProject can be set before any of the Cfg* functions are called for
// GCE services, and will be used as the default value for the "project"
// configuration parameter for each service.
var DefaultGCEProject string
// GCE wraps configuration parameters commonly used for interacting with GCE
// services.
type GCE struct {
Project string
CredFile string
}
// CfgGCE configures and returns a GCE instance which will be usable once Run is
// called on the passed in Cfg instance.
func CfgGCE(cfg *mcfg.Cfg) *GCE {
proj := cfg.ParamString("project", DefaultGCEProject, "name of GCE project")
credFile := cfg.ParamString("cred-file", "", "path to GCE credientials json file, if any")
var gce GCE
cfg.Start.Then(func(ctx context.Context) error {
gce.Project = *proj
gce.CredFile = *credFile
return nil
})
return &gce
}
// ClientOptions generates and returns the ClientOption instances which can be
// passed into most GCE client drivers.
func (gce *GCE) ClientOptions() []option.ClientOption {
var opts []option.ClientOption
if gce.CredFile != "" {
opts = append(opts, option.WithCredentialsFile(gce.CredFile))
}
return opts
}
// KV implements the mlog.KVer interface
func (gce *GCE) KV() mlog.KV {
return mlog.KV{
"project": gce.Project,
}
}

View File

@ -11,7 +11,6 @@ import (
"github.com/mediocregopher/mediocre-go-lib/mcfg" "github.com/mediocregopher/mediocre-go-lib/mcfg"
"github.com/mediocregopher/mediocre-go-lib/mlog" "github.com/mediocregopher/mediocre-go-lib/mlog"
oldctx "golang.org/x/net/context" oldctx "golang.org/x/net/context"
"google.golang.org/api/option"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
) )
@ -30,68 +29,34 @@ type Message = pubsub.Message
// PubSub holds onto the info needed to communicate with PubSub and wraps a // PubSub holds onto the info needed to communicate with PubSub and wraps a
// *pubsub.Client // *pubsub.Client
type PubSub struct { type PubSub struct {
proj, credFile string
*pubsub.Client *pubsub.Client
gce *GCE
log *mlog.Logger log *mlog.Logger
} }
// NewPubSub initializes and returns PubSub instance for the given projectID and
// using the credentials file (if given)
func NewPubSub(ctx context.Context, projectID, credFile string) (*PubSub, error) {
ps := &PubSub{
proj: projectID,
credFile: credFile,
}
ps.log = mlog.DefaultLogger.WithKV(ps.KV())
var err error
ps.Client, err = pubsub.NewClient(ctx, ps.proj, ps.clientOpts()...)
return ps, mlog.ErrWithKV(err, ps)
}
// CfgPubSub configures and returns a PubSub instance which will be usable once // CfgPubSub configures and returns a PubSub instance which will be usable once
// Run is called on the passed in Cfg instance // Run is called on the passed in Cfg instance
// func CfgPubSub(cfg *mcfg.Cfg) *PubSub {
// defaultProject is optional and can be given to indcate the default gce
// project id for the configuration parameter which is created.
func CfgPubSub(cfg *mcfg.Cfg, defaultProject string) *PubSub {
cfg = cfg.Child("pubsub") cfg = cfg.Child("pubsub")
proj := cfg.ParamString("project", defaultProject, "name of the project in gce")
credFile := cfg.ParamString("cred-file", "", "path to pubsub credientials json file, if any")
var ps PubSub var ps PubSub
ps.gce = CfgGCE(cfg)
cfg.Start.Then(func(ctx context.Context) error { cfg.Start.Then(func(ctx context.Context) error {
ps.proj = *proj ps.log = m.Log(cfg, ps.KV())
ps.credFile = *credFile ps.log.Info("connecting to pubsub")
log := m.Log(cfg, ps.KV()) var err error
log.Info("connecting to pubsub") ps.Client, err = pubsub.NewClient(ctx, ps.gce.Project, ps.gce.ClientOptions()...)
psInner, err := NewPubSub(ctx, *proj, *credFile)
if err != nil { if err != nil {
return mlog.ErrWithKV(err, &ps) return mlog.ErrWithKV(err, &ps)
} }
ps = *psInner
ps.log = log
return nil return nil
}) })
return &ps return &ps
} }
func (ps *PubSub) clientOpts() []option.ClientOption {
var opts []option.ClientOption
//opts := []option.ClientOption{
// option.WithGRPCConnectionPool(ps.NumConns),
//}
if ps.credFile != "" {
opts = append(opts, option.WithCredentialsFile(ps.credFile))
}
return opts
}
// KV implements the mlog.KVer interface // KV implements the mlog.KVer interface
func (ps *PubSub) KV() mlog.KV { func (ps *PubSub) KV() mlog.KV {
return mlog.KV{ return ps.gce.KV()
"psProj": ps.proj,
}
} }
// Topic provides methods around a particular topic in PubSub // Topic provides methods around a particular topic in PubSub
@ -130,9 +95,7 @@ func (ps *PubSub) Topic(ctx context.Context, name string, create bool) (*Topic,
// KV implements the mlog.KVer interface // KV implements the mlog.KVer interface
func (t *Topic) KV() mlog.KV { func (t *Topic) KV() mlog.KV {
kv := t.ps.KV() return t.ps.KV().Set("topicName", t.name)
kv["topicName"] = t.name
return kv
} }
// Publish publishes a message with the given data as its body to the Topic // Publish publishes a message with the given data as its body to the Topic
@ -186,9 +149,7 @@ func (t *Topic) Subscription(ctx context.Context, name string, create bool) (*Su
// KV implements the mlog.KVer interface // KV implements the mlog.KVer interface
func (s *Subscription) KV() mlog.KV { func (s *Subscription) KV() mlog.KV {
kv := s.topic.KV() return s.topic.KV().Set("subName", s.name)
kv["subName"] = s.name
return kv
} }
// ConsumerFunc is a function which messages being consumed will be passed. The // ConsumerFunc is a function which messages being consumed will be passed. The

View File

@ -14,8 +14,9 @@ import (
var testPS *PubSub var testPS *PubSub
func init() { func init() {
DefaultGCEProject = "test"
cfg := mcfg.New() cfg := mcfg.New()
testPS = CfgPubSub(cfg, "test") testPS = CfgPubSub(cfg)
cfg.StartTestRun() cfg.StartTestRun()
} }