From 61e3c68cc80a00827fc840af4b0989296ce344fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Janouch?= Date: Thu, 11 Oct 2018 17:23:08 +0200 Subject: Some cleanup The proxy function still keeps bothering me. --- main.go | 44 +++++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/main.go b/main.go index a1a7357..0c257cb 100644 --- a/main.go +++ b/main.go @@ -289,7 +289,19 @@ func proxy(w http.ResponseWriter, req *http.Request) { // dataProc may return less data near the end of a subfile, so we give it // a maximum count of bytes to return at once and do our own buffering. - var queuedMetaUpdate, queuedChunk []byte + var queuedMetaUpdate, queuedData []byte + writeMeta := func() error { + if !wantMeta { + return nil + } + + var meta [1 + 16*255]byte + meta[0] = byte((copy(meta[1:], queuedMetaUpdate) + 15) / 16) + queuedMetaUpdate = nil + + _, err := bufrw.Write(meta[:1+int(meta[0])*16]) + return err + } for { select { case title := <-metaChan: @@ -298,31 +310,17 @@ func proxy(w http.ResponseWriter, req *http.Request) { if !ok { return } - - space := metaint - len(queuedChunk) - if space > len(chunk) { - space = len(chunk) - } - - queuedChunk = append(queuedChunk, chunk[:space]...) - if len(queuedChunk) < metaint { - break + missing := metaint - len(queuedData) + if len(chunk) < missing { + queuedData = append(queuedData, chunk...) + continue } - if _, err := bufrw.Write(queuedChunk); err != nil { + queuedData = append(queuedData, chunk[:missing]...) + if _, err := bufrw.Write(queuedData); err != nil { return } - - queuedChunk = chunk[space:] - if wantMeta { - var meta [1 + 16*255]byte - meta[0] = byte((copy(meta[1:], queuedMetaUpdate) + 15) / 16) - queuedMetaUpdate = nil - - if _, err := bufrw.Write(meta[:1+int(meta[0])*16]); err != nil { - return - } - } - if err := bufrw.Flush(); err != nil { + queuedData = chunk[missing:] + if writeMeta() != nil || bufrw.Flush() != nil { return } } -- cgit v1.2.3