update nodeconf service
This commit is contained in:
parent
3809b20d50
commit
6e1c5aab91
@ -12,6 +12,7 @@ const (
|
|||||||
|
|
||||||
type ConfigGetter interface {
|
type ConfigGetter interface {
|
||||||
GetNodes() []NodeConfig
|
GetNodes() []NodeConfig
|
||||||
|
GetNodesConfId() string
|
||||||
}
|
}
|
||||||
|
|
||||||
type NodeConfig struct {
|
type NodeConfig struct {
|
||||||
|
|||||||
@ -9,6 +9,8 @@ import (
|
|||||||
"github.com/anytypeio/any-sync/util/keys/asymmetric/signingkey"
|
"github.com/anytypeio/any-sync/util/keys/asymmetric/signingkey"
|
||||||
"github.com/anytypeio/go-chash"
|
"github.com/anytypeio/go-chash"
|
||||||
"github.com/libp2p/go-libp2p/core/peer"
|
"github.com/libp2p/go-libp2p/core/peer"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
const CName = "common.nodeconf"
|
const CName = "common.nodeconf"
|
||||||
@ -26,13 +28,14 @@ func New() Service {
|
|||||||
|
|
||||||
type Service interface {
|
type Service interface {
|
||||||
GetLast() Configuration
|
GetLast() Configuration
|
||||||
GetById(id string) Configuration
|
SetLastConfig(id string, nodes []NodeConfig) (err error)
|
||||||
app.Component
|
app.Component
|
||||||
}
|
}
|
||||||
|
|
||||||
type service struct {
|
type service struct {
|
||||||
accountId string
|
accountId string
|
||||||
last Configuration
|
last Configuration
|
||||||
|
mu sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
type Node struct {
|
type Node struct {
|
||||||
@ -51,11 +54,30 @@ func (n *Node) Capacity() float64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) Init(a *app.App) (err error) {
|
func (s *service) Init(a *app.App) (err error) {
|
||||||
nodesConf := a.MustComponent("config").(ConfigGetter).GetNodes()
|
nodesConf := a.MustComponent("config").(ConfigGetter)
|
||||||
s.accountId = a.MustComponent(commonaccount.CName).(commonaccount.Service).Account().PeerId
|
s.accountId = a.MustComponent(commonaccount.CName).(commonaccount.Service).Account().PeerId
|
||||||
|
return s.SetLastConfig(nodesConf.GetNodesConfId(), nodesConf.GetNodes())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *service) Name() (name string) {
|
||||||
|
return CName
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *service) GetLast() Configuration {
|
||||||
|
s.mu.RLock()
|
||||||
|
defer s.mu.RUnlock()
|
||||||
|
return s.last
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *service) SetLastConfig(id string, nodesConf []NodeConfig) (err error) {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
if s.last != nil && s.last.Id() == id {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
fileConfig := &configuration{
|
fileConfig := &configuration{
|
||||||
id: "config",
|
id: id,
|
||||||
accountId: s.accountId,
|
accountId: s.accountId,
|
||||||
}
|
}
|
||||||
if fileConfig.chash, err = chash.New(chash.Config{
|
if fileConfig.chash, err = chash.New(chash.Config{
|
||||||
@ -89,23 +111,15 @@ func (s *service) Init(a *app.App) (err error) {
|
|||||||
if err = fileConfig.chash.AddMembers(members...); err != nil {
|
if err = fileConfig.chash.AddMembers(members...); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
var beforeId = ""
|
||||||
|
if s.last != nil {
|
||||||
|
beforeId = s.last.Id()
|
||||||
|
}
|
||||||
|
log.Info("configuration changed", zap.String("before", beforeId), zap.String("after", fileConfig.Id()))
|
||||||
s.last = fileConfig
|
s.last = fileConfig
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) Name() (name string) {
|
|
||||||
return CName
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *service) GetLast() Configuration {
|
|
||||||
return s.last
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *service) GetById(id string) Configuration {
|
|
||||||
//TODO implement me
|
|
||||||
panic("implement me")
|
|
||||||
}
|
|
||||||
|
|
||||||
func nodeFromConfigNode(n NodeConfig) (*Node, error) {
|
func nodeFromConfigNode(n NodeConfig) (*Node, error) {
|
||||||
p, err := peer.Decode(n.PeerId)
|
p, err := peer.Decode(n.PeerId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user