Skip to content

Commit

Permalink
fix: race condition; sync.map
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelemusiani committed Apr 3, 2024
1 parent fc8af63 commit 0f02ce2
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 86 deletions.
48 changes: 26 additions & 22 deletions cmd/macpassd/registration/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,17 @@ func Remove(r Registration) {
// expired.
func GetOldEntries() (oldEntries []Registration) {
// Get from map
for _, reg := range currentMap.v {
if time.Now().Sub(reg.End) >= 0 {
oldEntries = append(oldEntries, reg)
currentMap.v.Range(func(key, value any) bool {
val, ok := value.(Registration)
if !ok {
slog.With("value", value, "ok", ok).
Error("Type assertion failed. Could not converto aval to type Registration")
log.Fatal("Could not continue")
return false
}
}
oldEntries = append(oldEntries, val)
return true
})

// Get from db ?
return
Expand All @@ -112,36 +118,38 @@ func AddIpToMac(ip net.IP, mac net.HardwareAddr) {

func GetAllEntries() (entries []Registration) {
// Get from map
for _, reg := range currentMap.v {
entries = append(entries, reg)
}
currentMap.v.Range(func(key, value any) bool {
val, ok := value.(Registration)
if !ok {
slog.With("value", value, "ok", ok).
Error("Type assertion failed. Could not converto aval to type Registration")
log.Fatal("Could not continue")
return false
}
entries = append(entries, val)
return true
})

// Get from db
return
}

func UpdateLastPing(e Registration) {
//update on map
currentMap.mu.Lock()
e.LastPing = time.Now()
currentMap.v[e.Mac] = e
currentMap.mu.Unlock()
currentMap.v.Store(e.Mac, e)
}

func SetHostDown(e Registration) {
//update on map
currentMap.mu.Lock()
e.IsDown = true
currentMap.v[e.Mac] = e
currentMap.mu.Unlock()
currentMap.v.Store(e.Mac, e)
}

func SetHostUp(e Registration) {
//update on map
currentMap.mu.Lock()
e.IsDown = false
currentMap.v[e.Mac] = e
currentMap.mu.Unlock()
currentMap.v.Store(e.Mac, e)
}

// This function removes the ip form the Ips field of a registration and save
Expand All @@ -155,10 +163,8 @@ func RemoveIP(e Registration, ip net.IP) {
}
}

currentMap.mu.Lock()
e.Ips = newIps
currentMap.v[e.Mac] = e
currentMap.mu.Unlock()
currentMap.v.Store(e.Mac, e)
}

// This function removes the ip form the Ips field of a registration, move the
Expand All @@ -175,11 +181,9 @@ func SetOldIP(e Registration, ip net.IP) {
}
}

currentMap.mu.Lock()
e.Ips = newIps
e.OldIps = oldIps
currentMap.v[e.Mac] = e
currentMap.mu.Unlock()
currentMap.v.Store(e.Mac, e)
}

func GetOldStateFromDB() []Registration {
Expand Down
30 changes: 15 additions & 15 deletions cmd/macpassd/registration/safemap.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package registration

import (
"log"
"log/slog"
"net"
"sync"
Expand All @@ -9,42 +10,41 @@ import (
// A safe map is a map that have a Mutex condition in order to support
// concurrency
type safeMap struct {
mu sync.Mutex
v map[string]Registration
v sync.Map
}

func newSafeMap() *safeMap {
return &safeMap{v: make(map[string]Registration)}
return &safeMap{}
}

func (m *safeMap) add(r Registration) {
m.mu.Lock()
m.v[r.Mac] = r
m.mu.Unlock()
m.v.Store(r.Mac, r)
}

func (m *safeMap) remove(mac string) {
m.mu.Lock()
delete(m.v, mac)
m.mu.Unlock()
m.v.Delete(mac)
}

// This function adds the ip to the registration associated with the mac address
// in the m map. If the ip is in the OldIPs files nothing is done
func (m *safeMap) addIp(mac string, ip net.IP) {
_, present := m.v[mac]
if present {
m.mu.Lock()
val := m.v[mac]
aval, present := m.v.Load(mac)
val, ok := aval.(Registration)

if !ok {
slog.With("ok", ok, "aval", aval).
Error("Type assertion failed. Could not converto aval to type Registration")
log.Fatal("Could not continue")
}

if present {
if !isIpPrenset(val.Ips, ip) && !isIpPrenset(val.OldIps, ip) {
val.Ips = append(val.Ips, ip) //Need to check for duplicates
slog.With("registration", val.String()).
Info("The registration is been updated on the map")
}

m.v[mac] = val
m.mu.Unlock()
m.v.Store(mac, val)
}
}

Expand Down
49 changes: 0 additions & 49 deletions cmd/macpassd/registration/safemap_test.go

This file was deleted.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/jcmturner/gokrb5/v8 v8.4.4
github.com/mattn/go-sqlite3 v1.14.17
github.com/vishvananda/netlink v1.1.0
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
golang.org/x/term v0.13.0
gotest.tools/v3 v3.5.1
internal/comunication v1.0.0
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8=
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down

0 comments on commit 0f02ce2

Please sign in to comment.