SetEffectiveWindow changes the sync mode and effective window size for flow control. The windowSize is capped at the buffer size. When window shrinks: sync mode blocks new writes, async mode truncates old data to enforce limit. When window increases: blocked writers are woken up if space becomes ava
(syncMode bool, windowSize int)
| 35 | // When window shrinks: sync mode blocks new writes, async mode truncates old data to enforce limit. |
| 36 | // When window increases: blocked writers are woken up if space becomes available. |
| 37 | func (cb *CirBuf) SetEffectiveWindow(syncMode bool, windowSize int) { |
| 38 | cb.lock.Lock() |
| 39 | defer cb.lock.Unlock() |
| 40 | |
| 41 | maxSize := len(cb.buf) |
| 42 | if windowSize > maxSize { |
| 43 | windowSize = maxSize |
| 44 | } |
| 45 | |
| 46 | oldSyncMode := cb.syncMode |
| 47 | oldWindowSize := cb.windowSize |
| 48 | cb.windowSize = windowSize |
| 49 | cb.syncMode = syncMode |
| 50 | |
| 51 | // In async mode, enforce window size by truncating buffer if needed |
| 52 | if !syncMode && cb.count > windowSize { |
| 53 | excess := cb.count - windowSize |
| 54 | cb.readPos = (cb.readPos + excess) % maxSize |
| 55 | cb.count = windowSize |
| 56 | } |
| 57 | |
| 58 | // Only sync mode blocks writers, so only wake if we were in sync mode. |
| 59 | // Wake when window grows (more space available) or switching to async (no longer blocking). |
| 60 | if oldSyncMode && (windowSize > oldWindowSize || !syncMode) { |
| 61 | cb.tryWakeWriter() |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // WriteAvailable attempts to write as much data as possible without blocking. |
| 66 | // Returns the number of bytes written and a channel to wait on if buffer is full (nil if not blocking). |
no test coverage detected