The simplest answer to the question as written is to change line 46 to make the 
response channel buffered:

                        result: make(chan resp, 1),

However, you don't have to worry about leaking open channels.  For channels, 
close() is a signal, not resource management.  Open channels will be garbage 
collected when there are no references to them. For this reason, it's usually 
best to only close() a channel from the writer goroutine, not a reader.

Without knowing exactly what you're modeling, there are a few changes I would 
make to your program.  I'll make an assumption that doWrites() is intended to 
serialize all requests, but doesn't need to wait on the caller.  I find the 
current structure of sendWork() a little strange as it only reports a timeout 
on the acceptance of work, not the performance of work.  While there are 
several ways to do this, I'd simply have a single timeout across both making 
the request and getting the response.

I've shown one such approach here:  https://play.golang.org/p/chC-_cF_tH

There are other changes you can make, depending on what the rules for 
doWrites() are, such as placing a deadline for responding to avoid getting 
stuck, and/or sending a result in a fresh goroutine to prevent all blocking.  
In that case I'd definitely include a timeout to avoid leaking stuck goroutines 
if you can't trust that the result channel is buffered.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to