Skip to content

Commit

Permalink
cache fix mutex m1k1o#20.
Browse files Browse the repository at this point in the history
  • Loading branch information
m1k1o committed Nov 25, 2023
1 parent 0f166b1 commit f7cf54d
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions internal/utils/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand Down

0 comments on commit f7cf54d

Please sign in to comment.