Core Idea:
The primary goal of this pattern is to allow the http.HandlerFunc
to always have access to the latest available set of []Company
data without blocking, even while a potentially long-running crawlCompanies
function is fetching new data in the background. It provides a form of non-blocking data exchange between a background worker and a request handler.
How it Works:
- Two Buffered Channels (
ch1
,ch2
):- Two channels (
ch1
,ch2
) are created, each capable of holding exactly one[]Company
slice (make(chan []Company, 1)
). The buffer size of 1 is key. - They are initialized by sending
nil
into each (ch1 <- nil
,ch2 <- nil
). This means both channels start "full" with a placeholder value.
- Two channels (