From b0a900f36f7f3d9dad625b127ea2c0eb46cce392 Mon Sep 17 00:00:00 2001 From: BroodjeAap Date: Sat, 25 Jul 2020 12:08:52 +0000 Subject: [PATCH] added exponential sleep time if there is a timeout for a stream --- stream.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/stream.go b/stream.go index 832517e..1a3d454 100755 --- a/stream.go +++ b/stream.go @@ -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) } }