@@ -45,47 +45,32 @@ type ProviderManager struct {
4545 cleanupInterval time.Duration
4646}
4747
48- type options struct {
49- cleanupInterval time.Duration
50- cache lru.LRUCache
51- }
52-
5348// Option is a function that sets a provider manager option.
54- type Option func (* options ) error
49+ type Option func (* ProviderManager ) error
5550
56- func (c * options ) apply (opts ... Option ) error {
51+ func (pm * ProviderManager ) applyOptions (opts ... Option ) error {
5752 for i , opt := range opts {
58- if err := opt (c ); err != nil {
53+ if err := opt (pm ); err != nil {
5954 return fmt .Errorf ("provider manager option %d failed: %s" , i , err )
6055 }
6156 }
6257 return nil
6358}
6459
65- var defaults = func (o * options ) error {
66- o .cleanupInterval = defaultCleanupInterval
67- cache , err := lru .NewLRU (lruCacheSize , nil )
68- if err != nil {
69- return err
70- }
71- o .cache = cache
72- return nil
73- }
74-
7560// CleanupInterval sets the time between GC runs.
7661// Defaults to 1h.
7762func CleanupInterval (d time.Duration ) Option {
78- return func (o * options ) error {
79- o .cleanupInterval = d
63+ return func (pm * ProviderManager ) error {
64+ pm .cleanupInterval = d
8065 return nil
8166 }
8267}
8368
8469// Cache sets the LRU cache implementation.
8570// Defaults to a simple LRU cache.
8671func Cache (c lru.LRUCache ) Option {
87- return func (o * options ) error {
88- o .cache = c
72+ return func (pm * ProviderManager ) error {
73+ pm .cache = c
8974 return nil
9075 }
9176}
@@ -102,17 +87,20 @@ type getProv struct {
10287
10388// NewProviderManager constructor
10489func NewProviderManager (ctx context.Context , local peer.ID , dstore ds.Batching , opts ... Option ) (* ProviderManager , error ) {
105- var cfg options
106- if err := cfg .apply (append ([]Option {defaults }, opts ... )... ); err != nil {
107- return nil , err
108- }
10990 pm := new (ProviderManager )
11091 pm .getprovs = make (chan * getProv )
11192 pm .newprovs = make (chan * addProv )
11293 pm .dstore = autobatch .NewAutoBatching (dstore , batchBufferSize )
113- pm .cache = cfg .cache
94+ cache , err := lru .NewLRU (lruCacheSize , nil )
95+ if err != nil {
96+ return nil , err
97+ }
98+ pm .cache = cache
99+ pm .cleanupInterval = defaultCleanupInterval
100+ if err := pm .applyOptions (opts ... ); err != nil {
101+ return nil , err
102+ }
114103 pm .proc = goprocessctx .WithContext (ctx )
115- pm .cleanupInterval = cfg .cleanupInterval
116104 pm .proc .Go (pm .run )
117105 return pm , nil
118106}
0 commit comments