Skip to content

Commit

Permalink
Performance hotfix
Browse files Browse the repository at this point in the history
Rather than having a single timer for polling the individual readers,
have each them having their own ticker which is not exposed.
  • Loading branch information
mhemeryck committed Nov 23, 2018
1 parent 230956f commit 0171f6d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 19 deletions.
5 changes: 4 additions & 1 deletion digital_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ func (d *DigitalInputReader) Update(events chan *DigitalInputReader) (err error)
}

// Poll continuously updates the instance
func (d *DigitalInputReader) Poll(events chan *DigitalInputReader, ticker *time.Ticker) {
func (d *DigitalInputReader) Poll(events chan *DigitalInputReader, interval int) {
ticker := time.NewTicker(time.Duration(interval) * time.Millisecond)
defer ticker.Stop()

count := 0
for {
select {
Expand Down
15 changes: 2 additions & 13 deletions digital_input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"
"path/filepath"
"testing"
"time"
)

// setup for creating a temporary digital input
Expand Down Expand Up @@ -158,11 +157,6 @@ func TestPoll(t *testing.T) {
events := make(chan *DigitalInputReader)
defer close(events)

// Ticker
pollingInterval := 500 * time.Millisecond
ticker := time.NewTicker(pollingInterval)
defer ticker.Stop()

// Setup for triggering an event
digitalInput.Value = false
f.Seek(0, 0)
Expand All @@ -172,7 +166,7 @@ func TestPoll(t *testing.T) {
}

// Poll
go digitalInput.Poll(events, ticker)
go digitalInput.Poll(events, 500)

// Block on events
d := <-events
Expand Down Expand Up @@ -202,14 +196,9 @@ func TestPollError(t *testing.T) {
events := make(chan *DigitalInputReader)
defer close(events)

// Ticker
pollingInterval := 500 * time.Millisecond
ticker := time.NewTicker(pollingInterval)
defer ticker.Stop()

f.Close()
// Poll
go digitalInput.Poll(events, ticker)
go digitalInput.Poll(events, 500)

d := <-events

Expand Down
6 changes: 1 addition & 5 deletions unipitt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package unipitt

import (
"log"
"time"

"github.com/cenkalti/backoff"
mqtt "github.com/eclipse/paho.mqtt.golang"
Expand Down Expand Up @@ -60,12 +59,10 @@ func (h *Handler) Poll(done chan bool, interval int, payload string) (err error)
events := make(chan *DigitalInputReader)
defer close(events)

ticker := time.NewTicker(time.Duration(interval) * time.Millisecond)

// Start polling
log.Printf("Initiate polling for %d readers\n", len(h.readers))
for k := range h.readers {
go h.readers[k].Poll(events, ticker)
go h.readers[k].Poll(events, interval)
}

// Publish on a trigger
Expand All @@ -82,7 +79,6 @@ func (h *Handler) Poll(done chan bool, interval int, payload string) (err error)
}
case <-done:
log.Println("Handler done polling, coming back ...")
ticker.Stop()
return
}
}
Expand Down

0 comments on commit 0171f6d

Please sign in to comment.