Skip to content

Commit

Permalink
Send heartbeats when modem connects
Browse files Browse the repository at this point in the history
  • Loading branch information
CameronRP committed Jun 2, 2023
1 parent f880084 commit 173ee9b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
20 changes: 17 additions & 3 deletions heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

api "github.com/TheCacophonyProject/go-api"
"github.com/TheCacophonyProject/modemd/connrequester"
"github.com/TheCacophonyProject/modemd/modemlistener"
"github.com/TheCacophonyProject/window"
)

Expand All @@ -32,6 +33,7 @@ type Heartbeat struct {
type Clock interface {
Sleep(d time.Duration)
Now() time.Time
After(d time.Duration) <-chan time.Time
}

type HeartBeatClock struct {
Expand All @@ -44,13 +46,21 @@ func (h *HeartBeatClock) Now() time.Time {
return time.Now()
}

func (h *HeartBeatClock) After(d time.Duration) <-chan time.Time {
return time.After(d)
}

var clock Clock = &HeartBeatClock{}

func heartBeatLoop(window *window.Window) {
hb := NewHeartbeat(window)
sendBeats(hb, window)
}
func sendBeats(hb *Heartbeat, window *window.Window) {
modemConnectSignal, err := modemlistener.GetModemConnectedSignalListener()
if err != nil {
log.Println("Failed to get modem connected signal listener")
}
initialDelay := heartBeatDelay

if !window.Active() {
Expand Down Expand Up @@ -81,7 +91,11 @@ func sendBeats(hb *Heartbeat, window *window.Window) {

}
log.Printf("Heartbeat sleeping until %v", clock.Now().Add(nextEventIn))
clock.Sleep(nextEventIn)
select {
case <-modemConnectSignal:
log.Println("Modem connected")
case <-clock.After(nextEventIn):
}
}
}

Expand Down Expand Up @@ -129,8 +143,8 @@ func sendHeartbeat(nextBeat time.Time, attempts int) error {
for {
apiClient, err = api.New()
if err != nil {
attempt +=1
if attempt < attempts{
attempt += 1
if attempt < attempts {
log.Printf("Error connecting to api %v trying again in %v", err, attemptDelay)
clock.Sleep(attemptDelay)
continue
Expand Down
18 changes: 18 additions & 0 deletions heartbeat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ func (h *TestClock) Sleep(d time.Duration) {
func (h *TestClock) Now() time.Time {
return h.now
}
func (h *TestClock) After(d time.Duration) <-chan time.Time {
// nextBeat gets updated after sleep skip first
if h.sleepCount > 0 {
if h.sleepCount == len(h.expectedSleeps)-1 {
// penultimate event is only valid for 5 minutes after sleep
assert.Equal(h.t, h.expectedSleeps[h.sleepCount].Add(5*time.Minute).Format(dateFormat), h.hb.validUntil.Format(dateFormat))

} else {
assert.Equal(h.t, h.expectedSleeps[h.sleepCount].Add(1*time.Hour).Format(dateFormat), h.hb.validUntil.Format(dateFormat))
}
}
h.now = h.now.Add(d)
assert.Equal(h.t, h.now.Format(dateFormat), h.expectedSleeps[h.sleepCount].Format(dateFormat))
h.sleepCount += 1
s := make(chan time.Time, 10)
s <- h.now
return s
}

func TestSmallWindow(t *testing.T) {
clock := &TestClock{now: time.Now(), t: t}
Expand Down

0 comments on commit 173ee9b

Please sign in to comment.