mirror of
https://github.com/juanfont/headscale.git
synced 2024-12-24 16:58:05 +00:00
Always load machine object from DB before save/modify
We are currently holding Machine objects in memory for a long time, while waiting for stream/longpoll, this might make us end up with stale objects, that we just call save on, potentially overwriting stuff in the database. A typical scenario would be someone changing something from the CLI, e.g. enabling routes, which in turn is overwritten again by the stale object in the longpolling function. The code has been left with TODO's and a discussion is available in #93.
This commit is contained in:
parent
a054e2514a
commit
0aeeaac361
56
poll.go
56
poll.go
@ -234,6 +234,18 @@ func (h *Headscale) PollNetMapStream(
|
|||||||
Str("channel", "pollData").
|
Str("channel", "pollData").
|
||||||
Int("bytes", len(data)).
|
Int("bytes", len(data)).
|
||||||
Msg("Data from pollData channel written successfully")
|
Msg("Data from pollData channel written successfully")
|
||||||
|
// TODO: Abstract away all the database calls, this can cause race conditions
|
||||||
|
// when an outdated machine object is kept alive, e.g. db is update from
|
||||||
|
// command line, but then overwritten.
|
||||||
|
err = h.UpdateMachine(&m)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().
|
||||||
|
Str("handler", "PollNetMapStream").
|
||||||
|
Str("machine", m.Name).
|
||||||
|
Str("channel", "pollData").
|
||||||
|
Err(err).
|
||||||
|
Msg("Cannot update machine from database")
|
||||||
|
}
|
||||||
now := time.Now().UTC()
|
now := time.Now().UTC()
|
||||||
m.LastSeen = &now
|
m.LastSeen = &now
|
||||||
m.LastSuccessfulUpdate = &now
|
m.LastSuccessfulUpdate = &now
|
||||||
@ -268,6 +280,18 @@ func (h *Headscale) PollNetMapStream(
|
|||||||
Str("channel", "keepAlive").
|
Str("channel", "keepAlive").
|
||||||
Int("bytes", len(data)).
|
Int("bytes", len(data)).
|
||||||
Msg("Keep alive sent successfully")
|
Msg("Keep alive sent successfully")
|
||||||
|
// TODO: Abstract away all the database calls, this can cause race conditions
|
||||||
|
// when an outdated machine object is kept alive, e.g. db is update from
|
||||||
|
// command line, but then overwritten.
|
||||||
|
err = h.UpdateMachine(&m)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().
|
||||||
|
Str("handler", "PollNetMapStream").
|
||||||
|
Str("machine", m.Name).
|
||||||
|
Str("channel", "keepAlive").
|
||||||
|
Err(err).
|
||||||
|
Msg("Cannot update machine from database")
|
||||||
|
}
|
||||||
now := time.Now().UTC()
|
now := time.Now().UTC()
|
||||||
m.LastSeen = &now
|
m.LastSeen = &now
|
||||||
h.db.Save(&m)
|
h.db.Save(&m)
|
||||||
@ -316,10 +340,22 @@ func (h *Headscale) PollNetMapStream(
|
|||||||
Str("channel", "update").
|
Str("channel", "update").
|
||||||
Msg("Updated Map has been sent")
|
Msg("Updated Map has been sent")
|
||||||
|
|
||||||
// Keep track of the last successful update,
|
// Keep track of the last successful update,
|
||||||
// we sometimes end in a state were the update
|
// we sometimes end in a state were the update
|
||||||
// is not picked up by a client and we use this
|
// is not picked up by a client and we use this
|
||||||
// to determine if we should "force" an update.
|
// to determine if we should "force" an update.
|
||||||
|
// TODO: Abstract away all the database calls, this can cause race conditions
|
||||||
|
// when an outdated machine object is kept alive, e.g. db is update from
|
||||||
|
// command line, but then overwritten.
|
||||||
|
err = h.UpdateMachine(&m)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().
|
||||||
|
Str("handler", "PollNetMapStream").
|
||||||
|
Str("machine", m.Name).
|
||||||
|
Str("channel", "update").
|
||||||
|
Err(err).
|
||||||
|
Msg("Cannot update machine from database")
|
||||||
|
}
|
||||||
now := time.Now().UTC()
|
now := time.Now().UTC()
|
||||||
m.LastSuccessfulUpdate = &now
|
m.LastSuccessfulUpdate = &now
|
||||||
h.db.Save(&m)
|
h.db.Save(&m)
|
||||||
@ -338,6 +374,18 @@ func (h *Headscale) PollNetMapStream(
|
|||||||
Str("handler", "PollNetMapStream").
|
Str("handler", "PollNetMapStream").
|
||||||
Str("machine", m.Name).
|
Str("machine", m.Name).
|
||||||
Msg("The client has closed the connection")
|
Msg("The client has closed the connection")
|
||||||
|
// TODO: Abstract away all the database calls, this can cause race conditions
|
||||||
|
// when an outdated machine object is kept alive, e.g. db is update from
|
||||||
|
// command line, but then overwritten.
|
||||||
|
err := h.UpdateMachine(&m)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().
|
||||||
|
Str("handler", "PollNetMapStream").
|
||||||
|
Str("machine", m.Name).
|
||||||
|
Str("channel", "Done").
|
||||||
|
Err(err).
|
||||||
|
Msg("Cannot update machine from database")
|
||||||
|
}
|
||||||
now := time.Now().UTC()
|
now := time.Now().UTC()
|
||||||
m.LastSeen = &now
|
m.LastSeen = &now
|
||||||
h.db.Save(&m)
|
h.db.Save(&m)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user