added exponential sleep time if there is a timeout for a stream
This commit is contained in:
parent
ddf6ffb4c8
commit
b0a900f36f
1 changed files with 12 additions and 2 deletions
14
stream.go
14
stream.go
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"image/color"
|
"image/color"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
@ -22,6 +23,7 @@ type Stream struct {
|
||||||
Base64 string `json:"base64"`
|
Base64 string `json:"base64"`
|
||||||
Interval int `json:"interval"`
|
Interval int `json:"interval"`
|
||||||
WatchAreas []WatchArea `json:"watchAreas"`
|
WatchAreas []WatchArea `json:"watchAreas"`
|
||||||
|
Timeouts float64 `json:"-"`
|
||||||
FileLock sync.Mutex `json:"-"`
|
FileLock sync.Mutex `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -35,6 +37,7 @@ func NewStream(Name string, URL string) *Stream {
|
||||||
Base64: base64,
|
Base64: base64,
|
||||||
Interval: 5000,
|
Interval: 5000,
|
||||||
WatchAreas: make([]WatchArea, 0),
|
WatchAreas: make([]WatchArea, 0),
|
||||||
|
Timeouts: 0,
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
stream.WatchAreas = append(stream.WatchAreas,
|
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
|
// StreamFromJSON takes a filepath to a JSON file, and returns the unmarshalled Stream object
|
||||||
func StreamFromJSON(path string) *Stream {
|
func StreamFromJSON(path string) *Stream {
|
||||||
streamJSONFile, _ := ioutil.ReadFile(path)
|
streamJSONFile, _ := ioutil.ReadFile(path)
|
||||||
stream := Stream{}
|
stream := Stream{
|
||||||
|
Timeouts: 0,
|
||||||
|
}
|
||||||
json.Unmarshal([]byte(streamJSONFile), &stream)
|
json.Unmarshal([]byte(streamJSONFile), &stream)
|
||||||
return &stream
|
return &stream
|
||||||
}
|
}
|
||||||
|
|
@ -73,7 +78,10 @@ func (s *Stream) Update() {
|
||||||
resp, err := http.Get(s.URL)
|
resp, err := http.Get(s.URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
s.Timeouts++
|
||||||
return
|
return
|
||||||
|
} else {
|
||||||
|
s.Timeouts = 0
|
||||||
}
|
}
|
||||||
img, err := ioutil.ReadAll(resp.Body)
|
img, err := ioutil.ReadAll(resp.Body)
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
@ -132,7 +140,9 @@ func (s *Stream) CheckWatchAreas(area image.Rectangle, contours [][]image.Point)
|
||||||
func (s *Stream) UpdateInterval() {
|
func (s *Stream) UpdateInterval() {
|
||||||
for {
|
for {
|
||||||
go s.Update()
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue