mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-30 22:26:42 +00:00
load or store for account
This commit is contained in:
@@ -1,8 +1,6 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
|
|
||||||
"github.com/netbirdio/netbird/management/server/types"
|
"github.com/netbirdio/netbird/management/server/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -29,11 +27,10 @@ func (am *DefaultAccountManager) getAccountFromHolderOrInit(accountID string) *t
|
|||||||
if a != nil {
|
if a != nil {
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
account, err := am.requestBuffer.GetAccountWithBackpressure(context.Background(), accountID)
|
account, err := am.holder.LoadOrStoreFunc(accountID, am.requestBuffer.GetAccountWithBackpressure)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
am.holder.AddAccount(account)
|
|
||||||
return account
|
return account
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ func (am *DefaultAccountManager) getPeerNetworkMapExp(
|
|||||||
customZone nbdns.CustomZone,
|
customZone nbdns.CustomZone,
|
||||||
metrics *telemetry.AccountManagerMetrics,
|
metrics *telemetry.AccountManagerMetrics,
|
||||||
) *types.NetworkMap {
|
) *types.NetworkMap {
|
||||||
account := am.getAccountFromHolder(accountId)
|
account := am.getAccountFromHolderOrInit(accountId)
|
||||||
if account == nil {
|
if account == nil {
|
||||||
log.WithContext(ctx).Warnf("account %s not found in holder when getting peer network map", accountId)
|
log.WithContext(ctx).Warnf("account %s not found in holder when getting peer network map", accountId)
|
||||||
return &types.NetworkMap{
|
return &types.NetworkMap{
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
import "sync"
|
import (
|
||||||
|
"context"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
type Holder struct {
|
type Holder struct {
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
@@ -24,3 +27,17 @@ func (h *Holder) AddAccount(account *Account) {
|
|||||||
defer h.mu.Unlock()
|
defer h.mu.Unlock()
|
||||||
h.accounts[account.Id] = account
|
h.accounts[account.Id] = account
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *Holder) LoadOrStoreFunc(id string, accGetter func(context.Context, string) (*Account, error)) (*Account, error) {
|
||||||
|
h.mu.Lock()
|
||||||
|
defer h.mu.Unlock()
|
||||||
|
if acc, ok := h.accounts[id]; ok {
|
||||||
|
return acc, nil
|
||||||
|
}
|
||||||
|
account, err := accGetter(context.Background(), id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
h.accounts[id] = account
|
||||||
|
return account, nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user