From f7cf54dbf7cde826d8a8be58f18a6375ddbf6f4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Sat, 25 Nov 2023 16:25:35 +0100 Subject: [PATCH] cache fix mutex #20. --- internal/utils/cache.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/internal/utils/cache.go b/internal/utils/cache.go index 141a94b..ec4ab9d 100644 --- a/internal/utils/cache.go +++ b/internal/utils/cache.go @@ -65,20 +65,21 @@ func (c *Cache) Close() error { } func (c *Cache) ServeHTTP(w http.ResponseWriter) { - offset := 0 - index := 0 + offset, index := 0, 0 for { + // read current state c.mu.RLock() length, closed := c.length, c.closed + hasData := offset < length + var chunk []byte + if hasData { + chunk = c.chunks[index] + } c.mu.RUnlock() // if we have enough available data - if offset < length { - c.mu.RLock() - chunk := c.chunks[index] - c.mu.RUnlock() - + if hasData { i, _ := w.Write(chunk) offset += i index++