storj/pkg/discovery/config.go
aligeti 5e1b02ca8b
Statdb master db v3 848 (#830)
* intial changes to migrate statdb to masterdb framework

* statdb refactor compiles

* added TestCreateDoesNotExist testcase

* Initial port of statdb to masterdb framework working

* refactored statdb proto def to pkg/statdb

* removed statdb/proto folder

* moved pb.Node to storj.NodeID

* CreateEntryIfNotExistsRequest moved pd.Node to storj.NodeID

* moved the fields from pb.Node to statdb.UpdateRequest

ported TestUpdateExists, TestUpdateUptimeExists, TestUpdateAuditSuccessExists TestUpdateBatchExists
2018-12-14 15:17:30 -05:00

78 lines
1.7 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package discovery
import (
"context"
"time"
"github.com/zeebo/errs"
"go.uber.org/zap"
monkit "gopkg.in/spacemonkeygo/monkit.v2"
"storj.io/storj/pkg/kademlia"
"storj.io/storj/pkg/overlay"
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/provider"
"storj.io/storj/pkg/statdb"
)
var (
mon = monkit.Package()
// Error represents an overlay error
Error = errs.Class("discovery error")
)
// CtxKey used for assigning a key to Discovery server
type CtxKey int
const (
ctxKeyDiscovery CtxKey = iota
)
// Config loads on the configuration values from run flags
type Config struct {
RefreshInterval time.Duration `help:"the interval at which the cache refreshes itself in seconds" default:"1s"`
}
// Run runs the Discovery boot up and initialization
func (c Config) Run(ctx context.Context, server *provider.Provider) (err error) {
defer mon.Task()(&ctx)(&err)
srv := NewServer(zap.L())
pb.RegisterDiscoveryServer(server.GRPC(), srv)
ol := overlay.LoadFromContext(ctx)
kad := kademlia.LoadFromContext(ctx)
stat, ok := ctx.Value("masterdb").(interface {
StatDB() statdb.DB
})
if !ok {
return Error.New("unable to get master db instance")
}
discovery := NewDiscovery(ol, kad, stat.StatDB())
zap.L().Debug("Starting discovery")
ticker := time.NewTicker(c.RefreshInterval)
defer ticker.Stop()
go func() {
for {
select {
case <-ticker.C:
zap.L().Debug("Kicking off refresh")
err := discovery.Refresh(ctx)
if err != nil {
zap.L().Error("Error with cache refresh: ", zap.Error(err))
}
case <-ctx.Done():
return
}
}
}()
return server.Run(context.WithValue(ctx, ctxKeyDiscovery, discovery))
}