From 2ee946480b86cc404013ab2a9cdf14ff5d60842d Mon Sep 17 00:00:00 2001 From: MuZhou233 Date: Wed, 7 Aug 2024 03:36:07 +0100 Subject: [PATCH] fix: update sync_map.go --- .../model/modelsupervisor/modelsupervisor.go | 20 +++--- app/sephirah/internal/supervisor/feature.go | 20 +++--- app/sephirah/internal/supervisor/summary.go | 32 ++++----- .../internal/supervisor/supervisor.go | 13 ++-- internal/lib/libtype/sync_map.go | 68 +++++++------------ 5 files changed, 67 insertions(+), 86 deletions(-) diff --git a/app/sephirah/internal/model/modelsupervisor/modelsupervisor.go b/app/sephirah/internal/model/modelsupervisor/modelsupervisor.go index dffbdf1..febf79c 100644 --- a/app/sephirah/internal/model/modelsupervisor/modelsupervisor.go +++ b/app/sephirah/internal/model/modelsupervisor/modelsupervisor.go @@ -119,19 +119,19 @@ type PorterGroup struct { } type ServerFeatureSummaryMap struct { - AccountPlatforms *libtype.SyncMap[[]string] - AppInfoSources *libtype.SyncMap[[]string] - FeedSources *libtype.SyncMap[[]string] - NotifyDestinations *libtype.SyncMap[[]string] - FeedItemActions *libtype.SyncMap[[]string] + AccountPlatforms *libtype.SyncMap[string, []string] + AppInfoSources *libtype.SyncMap[string, []string] + FeedSources *libtype.SyncMap[string, []string] + NotifyDestinations *libtype.SyncMap[string, []string] + FeedItemActions *libtype.SyncMap[string, []string] } func NewServerFeatureSummaryMap() *ServerFeatureSummaryMap { return &ServerFeatureSummaryMap{ - AccountPlatforms: libtype.NewSyncMap[[]string](), - AppInfoSources: libtype.NewSyncMap[[]string](), - FeedSources: libtype.NewSyncMap[[]string](), - NotifyDestinations: libtype.NewSyncMap[[]string](), - FeedItemActions: libtype.NewSyncMap[[]string](), + AccountPlatforms: libtype.NewSyncMap[string, []string](), + AppInfoSources: libtype.NewSyncMap[string, []string](), + FeedSources: libtype.NewSyncMap[string, []string](), + NotifyDestinations: libtype.NewSyncMap[string, []string](), + FeedItemActions: libtype.NewSyncMap[string, []string](), } } diff --git a/app/sephirah/internal/supervisor/feature.go b/app/sephirah/internal/supervisor/feature.go index a0c1261..77d366e 100644 --- a/app/sephirah/internal/supervisor/feature.go +++ b/app/sephirah/internal/supervisor/feature.go @@ -21,8 +21,8 @@ func (s *Supervisor) HasAccountPlatform(platform string) bool { func (s *Supervisor) WithAccountPlatform(ctx context.Context, platform string) context.Context { s.featureSummaryRWMu.RLock() defer s.featureSummaryRWMu.RUnlock() - if platforms := s.featureSummaryMap.AccountPlatforms.Load(platform); platforms != nil { - return client.WithPorterAddress(ctx, *platforms) + if platforms, ok := s.featureSummaryMap.AccountPlatforms.Load(platform); ok { + return client.WithPorterAddress(ctx, platforms) } return client.WithPorterFastFail(ctx) } @@ -41,8 +41,8 @@ func (s *Supervisor) HasAppInfoSource(source string) bool { func (s *Supervisor) WithAppInfoSource(ctx context.Context, source string) context.Context { s.featureSummaryRWMu.RLock() defer s.featureSummaryRWMu.RUnlock() - if sources := s.featureSummaryMap.AppInfoSources.Load(source); sources != nil { - return client.WithPorterAddress(ctx, *sources) + if sources, ok := s.featureSummaryMap.AppInfoSources.Load(source); ok { + return client.WithPorterAddress(ctx, sources) } return client.WithPorterFastFail(ctx) } @@ -64,8 +64,8 @@ func (s *Supervisor) HasFeedSource(source *modelsupervisor.FeatureRequest) bool func (s *Supervisor) WithFeedSource(ctx context.Context, source *modelsupervisor.FeatureRequest) context.Context { s.featureSummaryRWMu.RLock() defer s.featureSummaryRWMu.RUnlock() - if sources := s.featureSummaryMap.FeedSources.Load(source.ID); sources != nil { - return client.WithPorterAddress(ctx, *sources) + if sources, ok := s.featureSummaryMap.FeedSources.Load(source.ID); ok { + return client.WithPorterAddress(ctx, sources) } return client.WithPorterFastFail(ctx) } @@ -87,8 +87,8 @@ func (s *Supervisor) HasNotifyDestination(destination *modelsupervisor.FeatureRe func (s *Supervisor) WithNotifyDestination(ctx context.Context, destination *modelsupervisor.FeatureRequest) context.Context { s.featureSummaryRWMu.RLock() defer s.featureSummaryRWMu.RUnlock() - if destinations := s.featureSummaryMap.NotifyDestinations.Load(destination.ID); destinations != nil { - return client.WithPorterAddress(ctx, *destinations) + if destinations, ok := s.featureSummaryMap.NotifyDestinations.Load(destination.ID); ok { + return client.WithPorterAddress(ctx, destinations) } return client.WithPorterFastFail(ctx) } @@ -107,8 +107,8 @@ func (s *Supervisor) HasFeedItemAction(request *modelsupervisor.FeatureRequest) func (s *Supervisor) WithFeedItemAction(ctx context.Context, request *modelsupervisor.FeatureRequest) context.Context { s.featureSummaryRWMu.RLock() defer s.featureSummaryRWMu.RUnlock() - if actions := s.featureSummaryMap.FeedItemActions.Load(request.ID); actions != nil { - return client.WithPorterAddress(ctx, *actions) + if actions, ok := s.featureSummaryMap.FeedItemActions.Load(request.ID); ok { + return client.WithPorterAddress(ctx, actions) } return client.WithPorterFastFail(ctx) } diff --git a/app/sephirah/internal/supervisor/summary.go b/app/sephirah/internal/supervisor/summary.go index 1e965a0..1c27b30 100644 --- a/app/sephirah/internal/supervisor/summary.go +++ b/app/sephirah/internal/supervisor/summary.go @@ -51,12 +51,12 @@ func summarize( //nolint:gocognit // how? continue } for _, account := range ins.FeatureSummary.AccountPlatforms { - a := resMap.AccountPlatforms.Load(ins.Address) + a, _ := resMap.AccountPlatforms.Load(ins.Address) if a == nil { - a = &[]string{} + a = []string{} } - *a = append(*a, account.ID) - resMap.AccountPlatforms.Store(ins.Address, *a) + a = append(a, account.ID) + resMap.AccountPlatforms.Store(ins.Address, a) if supportedAccountPlatforms[account.ID] { continue } @@ -64,12 +64,12 @@ func summarize( //nolint:gocognit // how? supportedAccountPlatforms[account.ID] = true } for _, appSource := range ins.FeatureSummary.AppInfoSources { - a := resMap.AppInfoSources.Load(ins.Address) + a, _ := resMap.AppInfoSources.Load(ins.Address) if a == nil { - a = &[]string{} + a = []string{} } - *a = append(*a, appSource.ID) - resMap.AppInfoSources.Store(ins.Address, *a) + a = append(a, appSource.ID) + resMap.AppInfoSources.Store(ins.Address, a) if supportedAppSources[appSource.ID] { continue } @@ -77,12 +77,12 @@ func summarize( //nolint:gocognit // how? supportedAppSources[appSource.ID] = true } for _, feedSource := range ins.FeatureSummary.FeedSources { - a := resMap.FeedSources.Load(ins.Address) + a, _ := resMap.FeedSources.Load(ins.Address) if a == nil { - a = &[]string{} + a = []string{} } - *a = append(*a, feedSource.ID) - resMap.FeedSources.Store(ins.Address, *a) + a = append(a, feedSource.ID) + resMap.FeedSources.Store(ins.Address, a) if supportedFeedSources[feedSource.ID] { continue } @@ -90,12 +90,12 @@ func summarize( //nolint:gocognit // how? supportedFeedSources[feedSource.ID] = true } for _, notifyDestination := range ins.FeatureSummary.NotifyDestinations { - a := resMap.NotifyDestinations.Load(ins.Address) + a, _ := resMap.NotifyDestinations.Load(ins.Address) if a == nil { - a = &[]string{} + a = []string{} } - *a = append(*a, notifyDestination.ID) - resMap.NotifyDestinations.Store(ins.Address, *a) + a = append(a, notifyDestination.ID) + resMap.NotifyDestinations.Store(ins.Address, a) if supportedNotifyDestinations[notifyDestination.ID] { continue } diff --git a/app/sephirah/internal/supervisor/supervisor.go b/app/sephirah/internal/supervisor/supervisor.go index 6253cf6..db56e2d 100644 --- a/app/sephirah/internal/supervisor/supervisor.go +++ b/app/sephirah/internal/supervisor/supervisor.go @@ -41,7 +41,7 @@ type Supervisor struct { refreshMu sync.Mutex trustedAddresses []string - instanceController *libtype.SyncMap[modelsupervisor.PorterInstanceController] + instanceController *libtype.SyncMap[string, modelsupervisor.PorterInstanceController] instanceCache *libcache.Map[string, modelsupervisor.PorterInstance] featureSummary *modelsupervisor.ServerFeatureSummary @@ -63,7 +63,7 @@ func NewSupervisor( UUID: int64(uuid.New().ID()), porter: porter, auth: auth, - instanceController: libtype.NewSyncMap[modelsupervisor.PorterInstanceController](), + instanceController: libtype.NewSyncMap[string, modelsupervisor.PorterInstanceController](), instanceCache: instanceCache, refreshMu: sync.Mutex{}, featureSummary: new(modelsupervisor.ServerFeatureSummary), @@ -82,7 +82,10 @@ func (s *Supervisor) GetInstanceController( ctx context.Context, address string, ) *modelsupervisor.PorterInstanceController { - return s.instanceController.Load(address) + if c, ok := s.instanceController.Load(address); ok { + return &c + } + return nil } func (s *Supervisor) RefreshAliveInstances( //nolint:gocognit,funlen // TODO @@ -114,7 +117,7 @@ func (s *Supervisor) RefreshAliveInstances( //nolint:gocognit,funlen // TODO // Discover new instances and Refresh disconnected instances for _, address := range discoveredAddresses { - if ic := s.instanceController.Load(address); ic != nil && + if ic, ok := s.instanceController.Load(address); ok && ic.ConnectionStatus != modelsupervisor.PorterConnectionStatusDisconnected { continue } @@ -131,7 +134,7 @@ func (s *Supervisor) RefreshAliveInstances( //nolint:gocognit,funlen // TODO return } - if ic := s.instanceController.Load(address); ic == nil || + if ic, ok := s.instanceController.Load(address); ok || (ic.GlobalName != ins.GlobalName || ic.BinarySummary.BuildVersion != ins.BinarySummary.Version) { newInstancesMu.Lock() newInstances = append(newInstances, ins) diff --git a/internal/lib/libtype/sync_map.go b/internal/lib/libtype/sync_map.go index c9b2641..85db04a 100644 --- a/internal/lib/libtype/sync_map.go +++ b/internal/lib/libtype/sync_map.go @@ -2,82 +2,60 @@ package libtype import "sync" -type SyncMap[T any] struct { +type SyncMap[K comparable, V any] struct { m sync.Map } -func NewSyncMap[T any]() *SyncMap[T] { - return &SyncMap[T]{ +func NewSyncMap[K comparable, V any]() *SyncMap[K, V] { + return &SyncMap[K, V]{ m: sync.Map{}, } } -func (m *SyncMap[T]) Load(key string) *T { +func (m *SyncMap[K, V]) Load(key K) (V, bool) { + var value V v, ok := m.m.Load(key) if !ok { - return nil + return value, ok } - value, ok := v.(T) - if !ok { - return nil - } - return &value + return v.(V), ok } -func (m *SyncMap[T]) LoadOrStore(key string, value T) *T { - v, loaded := m.m.LoadOrStore(key, value) - if !loaded { - return nil - } - actual, ok := v.(T) - if !ok { - return nil - } - return &actual +func (m *SyncMap[K, V]) LoadOrStore(key K, value V) (V, bool) { + a, loaded := m.m.LoadOrStore(key, value) + return a.(V), loaded } -func (m *SyncMap[T]) LoadAndDelete(key string) *T { +func (m *SyncMap[K, V]) LoadAndDelete(key K) (V, bool) { + var value V v, loaded := m.m.LoadAndDelete(key) if !loaded { - return nil + return value, loaded } - value, ok := v.(T) - if !ok { - return nil - } - return &value + return v.(V), loaded } -func (m *SyncMap[T]) Store(key string, value T) { +func (m *SyncMap[K, V]) Store(key K, value V) { m.m.Store(key, value) } -func (m *SyncMap[T]) Range(f func(key string, value T) bool) { - m.m.Range(func(key, value interface{}) bool { - return f(key.(string), value.(T)) - }) +func (m *SyncMap[K, V]) Range(f func(key K, value V) bool) { + m.m.Range(func(key, value any) bool { return f(key.(K), value.(V)) }) } -func (m *SyncMap[T]) Swap(key string, value T) *T { - v, loaded := m.m.Swap(key, value) - if !loaded { - return nil - } - previous, ok := v.(T) - if !ok { - return nil - } - return &previous +func (m *SyncMap[K, V]) Swap(key K, value V) (V, bool) { + p, loaded := m.m.Swap(key, value) + return p.(V), loaded } -func (m *SyncMap[T]) CompareAndSwap(key string, oldValue, newValue T) bool { +func (m *SyncMap[K, V]) CompareAndSwap(key K, oldValue, newValue V) bool { return m.m.CompareAndSwap(key, oldValue, newValue) } -func (m *SyncMap[T]) Delete(key string) { +func (m *SyncMap[K, V]) Delete(key K) { m.m.Delete(key) } -func (m *SyncMap[T]) CompareAndDelete(key string, value T) bool { +func (m *SyncMap[K, V]) CompareAndDelete(key string, value V) bool { return m.m.CompareAndDelete(key, value) }