Commit 1d696d1e authored by Alan Shaw's avatar Alan Shaw

refactor: implement feedback from review

parent e01dc8e3
...@@ -351,6 +351,10 @@ func DisableValues() Option { ...@@ -351,6 +351,10 @@ func DisableValues() Option {
} }
// ProvidersOptions are options passed directly to the provider manager. // ProvidersOptions are options passed directly to the provider manager.
//
// The provider manager adds and gets provider records from the datastore, cahing
// them in between. These options are passed to the provider manager allowing
// customisation of things like the GC interval and cache implementation.
func ProvidersOptions(opts []providers.Option) Option { func ProvidersOptions(opts []providers.Option) Option {
return func(c *config) error { return func(c *config) error {
c.providersOptions = opts c.providersOptions = opts
......
...@@ -45,38 +45,23 @@ type ProviderManager struct { ...@@ -45,38 +45,23 @@ type ProviderManager struct {
cleanupInterval time.Duration cleanupInterval time.Duration
} }
type options struct {
cleanupInterval time.Duration
cache lru.LRUCache
}
// Option is a function that sets a provider manager option. // Option is a function that sets a provider manager option.
type Option func(*options) error type Option func(*ProviderManager) error
func (c *options) apply(opts ...Option) error { func (pm *ProviderManager) applyOptions(opts ...Option) error {
for i, opt := range opts { for i, opt := range opts {
if err := opt(c); err != nil { if err := opt(pm); err != nil {
return fmt.Errorf("provider manager option %d failed: %s", i, err) return fmt.Errorf("provider manager option %d failed: %s", i, err)
} }
} }
return nil return nil
} }
var defaults = func(o *options) error {
o.cleanupInterval = defaultCleanupInterval
cache, err := lru.NewLRU(lruCacheSize, nil)
if err != nil {
return err
}
o.cache = cache
return nil
}
// CleanupInterval sets the time between GC runs. // CleanupInterval sets the time between GC runs.
// Defaults to 1h. // Defaults to 1h.
func CleanupInterval(d time.Duration) Option { func CleanupInterval(d time.Duration) Option {
return func(o *options) error { return func(pm *ProviderManager) error {
o.cleanupInterval = d pm.cleanupInterval = d
return nil return nil
} }
} }
...@@ -84,8 +69,8 @@ func CleanupInterval(d time.Duration) Option { ...@@ -84,8 +69,8 @@ func CleanupInterval(d time.Duration) Option {
// Cache sets the LRU cache implementation. // Cache sets the LRU cache implementation.
// Defaults to a simple LRU cache. // Defaults to a simple LRU cache.
func Cache(c lru.LRUCache) Option { func Cache(c lru.LRUCache) Option {
return func(o *options) error { return func(pm *ProviderManager) error {
o.cache = c pm.cache = c
return nil return nil
} }
} }
...@@ -102,17 +87,20 @@ type getProv struct { ...@@ -102,17 +87,20 @@ type getProv struct {
// NewProviderManager constructor // NewProviderManager constructor
func NewProviderManager(ctx context.Context, local peer.ID, dstore ds.Batching, opts ...Option) (*ProviderManager, error) { func NewProviderManager(ctx context.Context, local peer.ID, dstore ds.Batching, opts ...Option) (*ProviderManager, error) {
var cfg options
if err := cfg.apply(append([]Option{defaults}, opts...)...); err != nil {
return nil, err
}
pm := new(ProviderManager) pm := new(ProviderManager)
pm.getprovs = make(chan *getProv) pm.getprovs = make(chan *getProv)
pm.newprovs = make(chan *addProv) pm.newprovs = make(chan *addProv)
pm.dstore = autobatch.NewAutoBatching(dstore, batchBufferSize) pm.dstore = autobatch.NewAutoBatching(dstore, batchBufferSize)
pm.cache = cfg.cache cache, err := lru.NewLRU(lruCacheSize, nil)
if err != nil {
return nil, err
}
pm.cache = cache
pm.cleanupInterval = defaultCleanupInterval
if err := pm.applyOptions(opts...); err != nil {
return nil, err
}
pm.proc = goprocessctx.WithContext(ctx) pm.proc = goprocessctx.WithContext(ctx)
pm.cleanupInterval = cfg.cleanupInterval
pm.proc.Go(pm.run) pm.proc.Go(pm.run)
return pm, nil return pm, nil
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment