Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
239 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
package datastore | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
type Engine string | ||
|
||
type engineBuilderFunc func(options Options) (Datastore, error) | ||
|
||
func RegisterEngine(key Engine, builder engineBuilderFunc) { | ||
if builderForEngine == nil { | ||
builderForEngine = make(map[Engine]engineBuilderFunc) | ||
} | ||
if _, ok := builderForEngine[key]; ok { | ||
panic("cannot register two datastore engines with the same name: " + key) | ||
} | ||
builderForEngine[key] = builder | ||
} | ||
|
||
var builderForEngine map[Engine]engineBuilderFunc | ||
|
||
type Options struct { | ||
kind Engine | ||
URI string | ||
GCWindow time.Duration | ||
RevisionQuantization time.Duration | ||
|
||
MaxIdleTime time.Duration | ||
MaxLifetime time.Duration | ||
MaxOpenConns int | ||
MinOpenConns int | ||
SplitQuerySize string | ||
|
||
// CRDB | ||
FollowerReadDelay time.Duration | ||
MaxRetries int | ||
OverlapKey string | ||
OverlapStrategy string | ||
|
||
// Postgres | ||
HealthCheckPeriod time.Duration | ||
GCInterval time.Duration | ||
GCMaxOperationTime time.Duration | ||
} | ||
|
||
type Option func(*Options) | ||
|
||
// NewDatastore initializes a datastore given the options | ||
func NewDatastore(kind Engine, options ...Option) (Datastore, error) { | ||
var opts Options | ||
for _, o := range options { | ||
o(&opts) | ||
} | ||
|
||
dsBuilder, ok := builderForEngine[kind] | ||
if !ok { | ||
return nil, fmt.Errorf("unknown datastore engine type: %s", kind) | ||
} | ||
log.Info().Msgf("using %s datastore engine", kind) | ||
|
||
return dsBuilder(opts) | ||
} | ||
|
||
func WithRevisionQuantization(revisionQuantization time.Duration) Option { | ||
return func(c *Options) { | ||
c.RevisionQuantization = revisionQuantization | ||
} | ||
} | ||
|
||
func WithGCWindow(gcWindow time.Duration) Option { | ||
return func(c *Options) { | ||
c.GCWindow = gcWindow | ||
} | ||
} | ||
|
||
func WithURI(uri string) Option { | ||
return func(c *Options) { | ||
c.URI = uri | ||
} | ||
} | ||
|
||
func WithMaxIdleTime(maxIdleTime time.Duration) Option { | ||
return func(c *Options) { | ||
c.MaxIdleTime = maxIdleTime | ||
} | ||
} | ||
|
||
func WithMaxLifetime(maxLifetime time.Duration) Option { | ||
return func(c *Options) { | ||
c.MaxLifetime = maxLifetime | ||
} | ||
} | ||
|
||
func WithMaxOpenConns(maxOpenConns int) Option { | ||
return func(c *Options) { | ||
c.MaxOpenConns = maxOpenConns | ||
} | ||
} | ||
|
||
func WithMinOpenConns(minOpenConns int) Option { | ||
return func(c *Options) { | ||
c.MinOpenConns = minOpenConns | ||
} | ||
} | ||
|
||
func WithSplitQuerySize(splitQuerySize string) Option { | ||
return func(c *Options) { | ||
c.SplitQuerySize = splitQuerySize | ||
} | ||
} | ||
|
||
func WithFollowerReadDelay(followerDelay time.Duration) Option { | ||
return func(c *Options) { | ||
c.FollowerReadDelay = followerDelay | ||
} | ||
} | ||
|
||
func WithMaxRetries(retries int) Option { | ||
return func(c *Options) { | ||
c.MaxRetries = retries | ||
} | ||
} | ||
|
||
func WithOverlapKey(key string) Option { | ||
return func(c *Options) { | ||
c.OverlapKey = key | ||
} | ||
} | ||
|
||
func WithOverlapStrategy(strategy string) Option { | ||
return func(c *Options) { | ||
c.OverlapStrategy = strategy | ||
} | ||
} | ||
|
||
func WithHealthCheckPeriod(interval time.Duration) Option { | ||
return func(c *Options) { | ||
c.HealthCheckPeriod = interval | ||
} | ||
} | ||
|
||
func WithGCInterval(interval time.Duration) Option { | ||
return func(c *Options) { | ||
c.GCInterval = interval | ||
} | ||
} | ||
|
||
func WithGCMaxOperationTime(interval time.Duration) Option { | ||
return func(c *Options) { | ||
c.GCMaxOperationTime = interval | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters