concurrency for the expiry time

This commit is contained in:
Erik Brakkee 2024-08-15 21:25:30 +02:00
parent fd18a63360
commit 56ad9fbf03
4 changed files with 22 additions and 6 deletions

View File

@ -92,8 +92,8 @@ func main() {
OS: "linux", OS: "linux",
Shell: "/bin/bash", Shell: "/bin/bash",
}, },
ExpiryTime: time.Now().In(japan).Add(10 * time.Minute),
} }
agent.SetExpiryTime(time.Now().In(japan).Add(10 * time.Minute))
state.Agents.Put(agent.Guid, &agent) state.Agents.Put(agent.Guid, &agent)
client := models.Client{ client := models.Client{
Guid: models.ClientGuid(strconv.Itoa(rand.Int())), Guid: models.ClientGuid(strconv.Itoa(rand.Int())),

View File

@ -3,9 +3,16 @@ package models
import ( import (
"git.wamblee.org/converge/pkg/comms" "git.wamblee.org/converge/pkg/comms"
"git.wamblee.org/converge/pkg/support/collections" "git.wamblee.org/converge/pkg/support/collections"
"sync/atomic"
"time" "time"
) )
// Concurrency design:
// 1. State is immutable
// 2. The MatchMakers uses copy-on-write and never modifies a state directly.
// 3. the matchmaker modifies the expiry time of the agent. This is dealt with using the
// sync/atomic package by storing the expiry time as an int64 using time.Time.UnixNano()
type RendezVousId string type RendezVousId string
type AgentGuid string type AgentGuid string
type ClientGuid string type ClientGuid string
@ -22,7 +29,16 @@ type Agent struct {
// TODO add remote address. // TODO add remote address.
EnvironmentInfo comms.EnvironmentInfo EnvironmentInfo comms.EnvironmentInfo
ExpiryTime time.Time expiryTime int64
}
func (agent *Agent) SetExpiryTime(t time.Time) {
atomic.StoreInt64(&agent.expiryTime, t.UnixNano())
}
func (agent *Agent) GetExpiryTime() time.Time {
t := atomic.LoadInt64(&agent.expiryTime)
return time.Unix(0, t)
} }
type Client struct { type Client struct {

View File

@ -56,7 +56,7 @@ func (converge *MatchMaker) Register(publicId models.RendezVousId, conn io.ReadW
converge.admin.SetSessionType(models.ClientId(session.ClientId), models.SessionType(session.SessionType)) converge.admin.SetSessionType(models.ClientId(session.ClientId), models.SessionType(session.SessionType))
}, },
func(expiry comms.ExpiryTimeUpdate) { func(expiry comms.ExpiryTimeUpdate) {
agent.ExpiryTime = expiry.ExpiryTime agent.SetExpiryTime(expiry.ExpiryTime)
converge.logStatus() converge.logStatus()
}) })
}() }()
@ -137,7 +137,7 @@ func logStatusImpl(admin *models.State, notifier Notifier) {
for agent := range admin.Agents.RangeValues() { for agent := range admin.Agents.RangeValues() {
lines = append(lines, fmt.Sprintf(format, agent.PublicId, lines = append(lines, fmt.Sprintf(format, agent.PublicId,
agent.StartTime.Format(time.DateTime), agent.StartTime.Format(time.DateTime),
agent.ExpiryTime.Format(time.DateTime), agent.GetExpiryTime().Format(time.DateTime),
agent.EnvironmentInfo.Username, agent.EnvironmentInfo.Username,
agent.EnvironmentInfo.Hostname, agent.EnvironmentInfo.Hostname,
agent.EnvironmentInfo.OS)) agent.EnvironmentInfo.OS))

View File

@ -39,7 +39,7 @@ templ State(state *models.State, location *time.Location) {
<tr> <tr>
<th>rendez-vous id</th> <th>rendez-vous id</th>
<th>start time</th> <th>start time</th>
<th>expiry time</th> <th> time</th>
<th>username</th> <th>username</th>
<th>host</th> <th>host</th>
<th>os</th> <th>os</th>
@ -50,7 +50,7 @@ templ State(state *models.State, location *time.Location) {
<tr> <tr>
<td>{string(agent.PublicId)}</td> <td>{string(agent.PublicId)}</td>
<td>{agent.StartTime.In(location).Format(time.DateTime)}</td> <td>{agent.StartTime.In(location).Format(time.DateTime)}</td>
<td>{agent.ExpiryTime.In(location).Format(time.DateTime)}</td> <td>{agent.GetExpiryTime().In(location).Format(time.DateTime)}</td>
<td>{agent.EnvironmentInfo.Username}</td> <td>{agent.EnvironmentInfo.Username}</td>
<td>{agent.EnvironmentInfo.Hostname}</td> <td>{agent.EnvironmentInfo.Hostname}</td>
<td>{agent.EnvironmentInfo.OS}</td> <td>{agent.EnvironmentInfo.OS}</td>