2019-09-19 20:56:34 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package contact
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"gopkg.in/spacemonkeygo/monkit.v2"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/pb"
|
|
|
|
"storj.io/common/sync2"
|
2019-09-19 20:56:34 +01:00
|
|
|
"storj.io/storj/satellite/overlay"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Error is the default error class for contact package
|
|
|
|
var Error = errs.Class("contact")
|
|
|
|
|
|
|
|
var mon = monkit.Package()
|
|
|
|
|
|
|
|
// Config contains configurable values for contact service
|
|
|
|
type Config struct {
|
|
|
|
ExternalAddress string `user:"true" help:"the public address of the node, useful for nodes behind NAT" default:""`
|
|
|
|
|
|
|
|
// Chore config values
|
2019-11-04 21:20:31 +00:00
|
|
|
Interval time.Duration `help:"how frequently the node contact chore should run" releaseDefault:"1h" devDefault:"30s"`
|
2019-09-19 20:56:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Service is the contact service between storage nodes and satellites
|
|
|
|
type Service struct {
|
|
|
|
log *zap.Logger
|
|
|
|
|
2019-09-27 16:47:57 +01:00
|
|
|
mu sync.Mutex
|
|
|
|
self *overlay.NodeDossier
|
2019-10-26 18:16:25 +01:00
|
|
|
|
2019-10-28 14:04:31 +00:00
|
|
|
initialized sync2.Fence
|
2019-09-19 20:56:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewService creates a new contact service
|
|
|
|
func NewService(log *zap.Logger, self *overlay.NodeDossier) *Service {
|
|
|
|
return &Service{
|
2019-10-28 14:04:31 +00:00
|
|
|
log: log,
|
|
|
|
self: self,
|
2019-09-19 20:56:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Local returns the storagenode node-dossier
|
|
|
|
func (service *Service) Local() overlay.NodeDossier {
|
2019-09-27 16:47:57 +01:00
|
|
|
service.mu.Lock()
|
|
|
|
defer service.mu.Unlock()
|
2019-09-19 20:56:34 +01:00
|
|
|
return *service.self
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateSelf updates the local node with the capacity
|
|
|
|
func (service *Service) UpdateSelf(capacity *pb.NodeCapacity) {
|
2019-09-27 16:47:57 +01:00
|
|
|
service.mu.Lock()
|
|
|
|
defer service.mu.Unlock()
|
2019-09-19 20:56:34 +01:00
|
|
|
if capacity != nil {
|
|
|
|
service.self.Capacity = *capacity
|
|
|
|
}
|
2019-10-26 18:16:25 +01:00
|
|
|
|
2019-10-28 14:04:31 +00:00
|
|
|
service.initialized.Release()
|
2019-09-19 20:56:34 +01:00
|
|
|
}
|