diff options
author | Přemysl Janouch <p.janouch@gmail.com> | 2016-10-22 21:13:13 +0200 |
---|---|---|
committer | Přemysl Janouch <p@janouch.name> | 2018-10-11 16:30:37 +0200 |
commit | 92dbdedf0c774edf7a1e544622ea7d542d6111f9 (patch) | |
tree | 2b918d1e65fae76ee1e78247661c73a08ca39772 | |
parent | 42a430345f8fd14e6aaa70cc2c6afd774dc85cfe (diff) | |
download | bbc-on-ice-92dbdedf0c774edf7a1e544622ea7d542d6111f9.tar.gz bbc-on-ice-92dbdedf0c774edf7a1e544622ea7d542d6111f9.tar.xz bbc-on-ice-92dbdedf0c774edf7a1e544622ea7d542d6111f9.zip |
Refactor metadata retrieval
-rw-r--r-- | bbc-on-ice.go | 56 |
1 files changed, 30 insertions, 26 deletions
diff --git a/bbc-on-ice.go b/bbc-on-ice.go index cf290d7..57f5539 100644 --- a/bbc-on-ice.go +++ b/bbc-on-ice.go @@ -1,6 +1,7 @@ package main import ( + "context" "encoding/json" "errors" "fmt" @@ -95,7 +96,34 @@ func resolveM3U8(target string) (out []string, err error) { return out, nil } -var pathRE = regexp.MustCompile("^/(.*?)/(.*?)/(.*?)$") +func metaProc(ctx context.Context, name string, out chan<- string) { + var current, last string + var interval time.Duration + for { + meta, err := getMeta(name) + if err != nil { + current = "Error: " + err.Error() + interval = 30 * time.Second + } else { + current = meta.title + interval = time.Duration(meta.timeout) + } + // TODO: select on the send? Can't close it in the main proc. + // Also see https://blog.golang.org/pipelines + if current != last { + out <- current + last = current + } + + select { + case <-time.After(time.Duration(interval) * time.Millisecond): + case <-ctx.Done(): + return + } + } +} + +var pathRE = regexp.MustCompile(`^/(.*?)/(.*?)/(.*?)$`) func proxy(w http.ResponseWriter, req *http.Request) { const targetURI = "http://a.files.bbci.co.uk/media/live/manifesto/" + @@ -153,32 +181,8 @@ func proxy(w http.ResponseWriter, req *http.Request) { } fmt.Fprintf(bufrw, "\r\n") - // TODO: move to a normal function metaChan := make(chan string) - go func() { - var current, last string - var interval time.Duration - for { - meta, err := getMeta(name) - if err != nil { - current = "Error: " + err.Error() - interval = 30 * time.Second - } else { - current = meta.title - interval = time.Duration(meta.timeout) - } - if current != last { - metaChan <- current - last = current - } - - select { - case <-time.After(time.Duration(interval) * time.Millisecond): - case <-req.Context().Done(): - return - } - } - }() + go metaProc(req.Context(), name, metaChan) // TODO: move to a normal function // FIXME: this will load a few seconds (one URL) and die |