On Friday, 15 April 2022 at 06:47:09 UTC+1 yan.z...@gmail.com wrote: > When one server finds something wrong, it sends out a request to another > server for help. > Then it makes a log and then a ticker to recheck the situation again and > again and in the meantime, to receive a response from that server that > indicates everything is fine. > > So here is a gap between sending help and getting ready to receive the > "ok" response. It is a tiny gap but it is not possible that the other > server sends the "ok" message back through TCP connection just in this gap. > Yes my program handles the TCP message as always, in this case sends back > through the channel to the goroutine that has not prepared the select for > ticker channel and ok-receiving channel. Uh-oh, deadlock, crash! What is > your suggestion? buffered channel? >
TCP sockets and Go channels are two completely different things. But in either case, if a receiver becomes ready before the sender, it's fine. The receiver just waits for data to arrive. And if the receiver is not ready first, it's also fine. The sender will send up to the buffer size (in the case of a buffered channel), and after that it will pause until the receiver is ready. (In the case of TCP you have quite a lot of buffering in network sockets; typically it could send a megabyte or more before it pauses). A pause is not a "deadlock". A pause is just waiting until it's OK to proceed. In this case you already admit that opportunity that the sending happens > when the receiver is not ready. > And there is no problem at all. It really doesn't matter if the sender becomes ready first, or the receiver becomes ready first. Here are two concrete examples, using an unbuffered channel: https://go.dev/play/p/r5eptx4dE7T https://go.dev/play/p/sTB5lio6Rhx Aside: in those examples I have used a sync.WaitGroup to ensure that the main program doesn't terminate until both the goroutines have completed. -- 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. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/9a0a275e-54cf-40da-b0ac-8ce724fc3d49n%40googlegroups.com.