Skip to content
Permalink
Browse files

fix connection timeout handling, add handling for drivers that connec…

…t but never load
  • Loading branch information...
cj123 committed Jul 1, 2019
1 parent 89a50ca commit 782bef1696304c250b7e587c59ef6dd10dc5c496
Showing with 368 additions and 114 deletions.
  1. +14 −4 race_control.go
  2. +2 −1 race_control_driver.go
  3. +352 −109 race_control_test.go
@@ -296,9 +296,14 @@ func (rc *RaceControl) disconnectDriver(driver *RaceControlDriver) error {
return rc.OnClientDisconnect(carInfo)
}

// driverLastSeenMaxDuration is how long to wait before considering a driver 'timed out'. A timed out driver
// is force-disconnected.
var driverLastSeenMaxDuration = time.Second * 5
var (
// driverLastSeenMaxDuration is how long to wait before considering a driver 'timed out'.
// A timed out driver is force-disconnected.
driverLastSeenMaxDuration = time.Second * 5
// driverConnectionTimeout is how long to wait for a connected driver to load. if they exceed this time, they are
// considered to be disconnected and are removed from ConnectedDrivers.
driverConnectionTimeout = time.Minute
)

// OnSessionUpdate is called every sessionRequestInterval.
func (rc *RaceControl) OnSessionUpdate(sessionInfo udp.SessionInfo) error {
@@ -308,7 +313,11 @@ func (rc *RaceControl) OnSessionUpdate(sessionInfo udp.SessionInfo) error {
var driversToDisconnect []*RaceControlDriver

_ = rc.ConnectedDrivers.Each(func(driverGUID udp.DriverGUID, driver *RaceControlDriver) error {
if time.Now().Sub(driver.LastSeen) > driverLastSeenMaxDuration {
if driver.LoadedTime.IsZero() && time.Now().Sub(driver.ConnectedTime) > driverConnectionTimeout {
// driver connected but never loaded
driversToDisconnect = append(driversToDisconnect, driver)
} else if !driver.LoadedTime.IsZero() && time.Now().Sub(driver.LastSeen) > driverLastSeenMaxDuration {
// driver disconnected after loading
driversToDisconnect = append(driversToDisconnect, driver)
}

@@ -362,6 +371,7 @@ func (rc *RaceControl) OnClientConnect(client udp.SessionCarInfo) error {
driver.Cars[driver.CarInfo.CarModel] = &RaceControlCarLapInfo{}
}

driver.ConnectedTime = time.Now()
driver.CurrentCar().LastLapCompletedTime = time.Now()

rc.ConnectedDrivers.Add(driver.CarInfo.DriverGUID, driver)
@@ -26,7 +26,8 @@ type RaceControlDriver struct {
CarInfo udp.SessionCarInfo `json:"CarInfo"`
TotalNumLaps int `json:"TotalNumLaps"`

LoadedTime time.Time `json:"LoadedTime" ts:"date"`
ConnectedTime time.Time `json:"ConnectedTime" ts:"date"`
LoadedTime time.Time `json:"LoadedTime" ts:"date"`

Position int `json:"Position"`
Split string `json:"Split"`

0 comments on commit 782bef1

Please sign in to comment.
You can’t perform that action at this time.