databroker server backend config (#1127)

* config,docs: add databroker storage backend configuration

* cache: allow configuring which backend storage to use

Currently supported types are "memory", "redis".
This commit is contained in:
Cuong Manh Le 2020-07-23 10:42:43 +07:00 committed by GitHub
parent c9182f757e
commit 1640151bc1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 99 additions and 6 deletions

View file

@ -8,17 +8,22 @@ var (
DefaultDeletePermanentlyAfter = time.Hour
// DefaultBTreeDegree is the default number of items to store in each node of the BTree.
DefaultBTreeDegree = 8
// DefaultStorageType is the default storage type that Server use
DefaultStorageType = "memory"
)
type serverConfig struct {
deletePermanentlyAfter time.Duration
btreeDegree int
deletePermanentlyAfter time.Duration
btreeDegree int
storageType string
storageConnectionString string
}
func newServerConfig(options ...ServerOption) *serverConfig {
cfg := new(serverConfig)
WithDeletePermanentlyAfter(DefaultDeletePermanentlyAfter)(cfg)
WithBTreeDegree(DefaultBTreeDegree)(cfg)
WithStorageType(DefaultStorageType)(cfg)
for _, option := range options {
option(cfg)
}
@ -43,3 +48,17 @@ func WithDeletePermanentlyAfter(dur time.Duration) ServerOption {
cfg.deletePermanentlyAfter = dur
}
}
// WithStorageType sets the storage type.
func WithStorageType(typ string) ServerOption {
return func(cfg *serverConfig) {
cfg.storageType = typ
}
}
// WithStorageConnectionString sets the DSN for storage.
func WithStorageConnectionString(connStr string) ServerOption {
return func(cfg *serverConfig) {
cfg.storageConnectionString = connStr
}
}

View file

@ -21,6 +21,7 @@ import (
"github.com/pomerium/pomerium/pkg/grpc/databroker"
"github.com/pomerium/pomerium/pkg/storage"
"github.com/pomerium/pomerium/pkg/storage/inmemory"
"github.com/pomerium/pomerium/pkg/storage/redis"
)
const (
@ -278,10 +279,26 @@ func (srv *Server) getDB(recordType string) storage.Backend {
srv.mu.Lock()
db = srv.byType[recordType]
if db == nil {
db = inmemory.NewDB(recordType, srv.cfg.btreeDegree)
db = srv.newDB(recordType)
srv.byType[recordType] = db
}
srv.mu.Unlock()
}
return db
}
func (srv *Server) newDB(recordType string) storage.Backend {
switch srv.cfg.storageType {
case inmemory.Name:
return inmemory.NewDB(recordType, srv.cfg.btreeDegree)
case redis.Name:
db, err := redis.New(srv.cfg.storageConnectionString, recordType, int64(srv.cfg.deletePermanentlyAfter.Seconds()))
if err != nil {
srv.log.Error().Err(err).Msg("failed to create new redis storage")
return nil
}
return db
default:
return nil
}
}