fix(services): use buffered channel for history flush to prevent blocking
- commit
- c2c40ba238b77f17c70da54f47614ef6bbab9da5
- author
- bnema · · 2025-10-23 22:06 UTC
- committer
- bnema · · 2025-10-24 17:33 UTC
- parent
- 50e023b
- signature
- unsigned
1 file · +4 -4
M internal/services/browser.go +4 -4
| @@ -49,7 +49,7 @@ type BrowserService struct { | |||
| 49 | 49 | guiBundle string | |
| 50 | 50 | zoomCache sync.Map // In-memory cache: key = domain/URL, value = float64 zoom level | |
| 51 | 51 | historyQueue chan historyUpdate // Queue for batched history writes | |
| 52 | - | historyFlushDone chan struct{} // Signal when history flush is complete | |
| 52 | + | historyFlushDone chan bool // Signal when history flush is complete (buffered) | |
| 53 | 53 | } | |
| 54 | 54 | ||
| 55 | 55 | // ServiceName returns the service name for frontend binding | |
| @@ -88,7 +88,7 @@ func NewBrowserService(cfg *config.Config, queries db.DatabaseQuerier) *BrowserS | |||
| 88 | 88 | initialURL: "", | |
| 89 | 89 | guiBundle: "", | |
| 90 | 90 | historyQueue: make(chan historyUpdate, 100), // Buffer 100 history updates | |
| 91 | - | historyFlushDone: make(chan struct{}), | |
| 91 | + | historyFlushDone: make(chan bool, 1), // Buffered to prevent blocking | |
| 92 | 92 | } | |
| 93 | 93 | ||
| 94 | 94 | // Start background batch processor for history writes | |
| @@ -428,7 +428,7 @@ func (s *BrowserService) processHistoryQueue() { | |||
| 428 | 428 | if !ok { | |
| 429 | 429 | // Channel closed, flush remaining and exit | |
| 430 | 430 | flush() | |
| 431 | - | close(s.historyFlushDone) | |
| 431 | + | s.historyFlushDone <- true // Send signal (non-blocking due to buffer) | |
| 432 | 432 | return | |
| 433 | 433 | } | |
| 434 | 434 | batch = append(batch, update) | |
| @@ -446,7 +446,7 @@ func (s *BrowserService) processHistoryQueue() { | |||
| 446 | 446 | // Call this during graceful shutdown. | |
| 447 | 447 | func (s *BrowserService) FlushHistoryQueue() { | |
| 448 | 448 | close(s.historyQueue) | |
| 449 | - | <-s.historyFlushDone // Wait for flush to complete | |
| 449 | + | <-s.historyFlushDone // Wait for completion signal | |
| 450 | 450 | log.Printf("History queue flushed and closed") | |
| 451 | 451 | } | |
| 452 | 452 | ||
| @@ -49,7 +49,7 @@ type BrowserService struct { | |||||
| 49 | guiBundle string | 49 | guiBundle string | ||
| 50 | zoomCache sync.Map // In-memory cache: key = domain/URL, value = float64 zoom level | 50 | zoomCache sync.Map // In-memory cache: key = domain/URL, value = float64 zoom level | ||
| 51 | historyQueue chan historyUpdate // Queue for batched history writes | 51 | historyQueue chan historyUpdate // Queue for batched history writes | ||
| 52 | - | historyFlushDone chan struct{} // Signal when history flush is complete | 52 | + | historyFlushDone chan bool // Signal when history flush is complete (buffered) |
| 53 | } | 53 | } | ||
| 54 | 54 | ||||
| 55 | // ServiceName returns the service name for frontend binding | 55 | // ServiceName returns the service name for frontend binding | ||
| @@ -88,7 +88,7 @@ func NewBrowserService(cfg *config.Config, queries db.DatabaseQuerier) *BrowserS | |||||
| 88 | initialURL: "", | 88 | initialURL: "", | ||
| 89 | guiBundle: "", | 89 | guiBundle: "", | ||
| 90 | historyQueue: make(chan historyUpdate, 100), // Buffer 100 history updates | 90 | historyQueue: make(chan historyUpdate, 100), // Buffer 100 history updates | ||
| 91 | - | historyFlushDone: make(chan struct{}), | 91 | + | historyFlushDone: make(chan bool, 1), // Buffered to prevent blocking |
| 92 | } | 92 | } | ||
| 93 | 93 | ||||
| 94 | // Start background batch processor for history writes | 94 | // Start background batch processor for history writes | ||
| @@ -428,7 +428,7 @@ func (s *BrowserService) processHistoryQueue() { | |||||
| 428 | if !ok { | 428 | if !ok { | ||
| 429 | // Channel closed, flush remaining and exit | 429 | // Channel closed, flush remaining and exit | ||
| 430 | flush() | 430 | flush() | ||
| 431 | - | close(s.historyFlushDone) | 431 | + | s.historyFlushDone <- true // Send signal (non-blocking due to buffer) |
| 432 | return | 432 | return | ||
| 433 | } | 433 | } | ||
| 434 | batch = append(batch, update) | 434 | batch = append(batch, update) | ||
| @@ -446,7 +446,7 @@ func (s *BrowserService) processHistoryQueue() { | |||||
| 446 | // Call this during graceful shutdown. | 446 | // Call this during graceful shutdown. | ||
| 447 | func (s *BrowserService) FlushHistoryQueue() { | 447 | func (s *BrowserService) FlushHistoryQueue() { | ||
| 448 | close(s.historyQueue) | 448 | close(s.historyQueue) | ||
| 449 | - | <-s.historyFlushDone // Wait for flush to complete | 449 | + | <-s.historyFlushDone // Wait for completion signal |
| 450 | log.Printf("History queue flushed and closed") | 450 | log.Printf("History queue flushed and closed") | ||
| 451 | } | 451 | } | ||
| 452 | 452 | ||||