added exponential sleep time if there is a timeout for a stream

This commit is contained in:
BroodjeAap 2020-07-25 12:08:52 +00:00
parent ddf6ffb4c8
commit b0a900f36f

View file

@ -6,6 +6,7 @@ import (
"image/color"
"io/ioutil"
"log"
"math"
"net/http"
"os"
"path/filepath"
@ -22,6 +23,7 @@ type Stream struct {
Base64 string `json:"base64"`
Interval int `json:"interval"`
WatchAreas []WatchArea `json:"watchAreas"`
Timeouts float64 `json:"-"`
FileLock sync.Mutex `json:"-"`
}
@ -35,6 +37,7 @@ func NewStream(Name string, URL string) *Stream {
Base64: base64,
Interval: 5000,
WatchAreas: make([]WatchArea, 0),
Timeouts: 0,
}
/*
stream.WatchAreas = append(stream.WatchAreas,
@ -62,7 +65,9 @@ func NewStream(Name string, URL string) *Stream {
// StreamFromJSON takes a filepath to a JSON file, and returns the unmarshalled Stream object
func StreamFromJSON(path string) *Stream {
streamJSONFile, _ := ioutil.ReadFile(path)
stream := Stream{}
stream := Stream{
Timeouts: 0,
}
json.Unmarshal([]byte(streamJSONFile), &stream)
return &stream
}
@ -73,7 +78,10 @@ func (s *Stream) Update() {
resp, err := http.Get(s.URL)
if err != nil {
log.Println(err)
s.Timeouts++
return
} else {
s.Timeouts = 0
}
img, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
@ -132,7 +140,9 @@ func (s *Stream) CheckWatchAreas(area image.Rectangle, contours [][]image.Point)
func (s *Stream) UpdateInterval() {
for {
go s.Update()
time.Sleep(time.Duration(s.Interval) * time.Millisecond)
expTimeout := math.Pow(2, s.Timeouts)
maxExpTimeout := (int)(math.Min(12, expTimeout))
time.Sleep(time.Duration(s.Interval*maxExpTimeout) * time.Millisecond)
}
}