mirror of
https://github.com/h44z/wg-portal.git
synced 2025-09-15 07:11:15 +00:00
fix duplicate creation of default peer (#437)
This commit is contained in:
@@ -3,6 +3,7 @@ package wireguard
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/h44z/wg-portal/internal/app"
|
||||
@@ -76,6 +77,8 @@ type Manager struct {
|
||||
db InterfaceAndPeerDatabaseRepo
|
||||
wg InterfaceController
|
||||
quick WgQuickController
|
||||
|
||||
userLockMap *sync.Map
|
||||
}
|
||||
|
||||
func NewWireGuardManager(
|
||||
@@ -86,11 +89,12 @@ func NewWireGuardManager(
|
||||
db InterfaceAndPeerDatabaseRepo,
|
||||
) (*Manager, error) {
|
||||
m := &Manager{
|
||||
cfg: cfg,
|
||||
bus: bus,
|
||||
wg: wg,
|
||||
db: db,
|
||||
quick: quick,
|
||||
cfg: cfg,
|
||||
bus: bus,
|
||||
wg: wg,
|
||||
db: db,
|
||||
quick: quick,
|
||||
userLockMap: &sync.Map{},
|
||||
}
|
||||
|
||||
m.connectToMessageBus()
|
||||
@@ -117,6 +121,12 @@ func (m Manager) handleUserCreationEvent(user domain.User) {
|
||||
return
|
||||
}
|
||||
|
||||
_, loaded := m.userLockMap.LoadOrStore(user.Identifier, "create")
|
||||
if loaded {
|
||||
return // another goroutine is already handling this user
|
||||
}
|
||||
defer m.userLockMap.Delete(user.Identifier)
|
||||
|
||||
slog.Debug("handling new user event", "user", user.Identifier)
|
||||
|
||||
ctx := domain.SetUserInfo(context.Background(), domain.SystemAdminContextUserInfo())
|
||||
@@ -132,6 +142,12 @@ func (m Manager) handleUserLoginEvent(userId domain.UserIdentifier) {
|
||||
return
|
||||
}
|
||||
|
||||
_, loaded := m.userLockMap.LoadOrStore(userId, "login")
|
||||
if loaded {
|
||||
return // another goroutine is already handling this user
|
||||
}
|
||||
defer m.userLockMap.Delete(userId)
|
||||
|
||||
userPeers, err := m.db.GetUserPeers(context.Background(), userId)
|
||||
if err != nil {
|
||||
slog.Error("failed to retrieve existing peers prior to default peer creation",
|
||||
|
Reference in New Issue
Block a user