I'm writing a game server and as this is my first time, I've been wondering how to send packets to the client without lagging the server.
Even if the client is lagging, packets should be sent to them. (Not sure if this is the right thing to do, but if I don't send packets to them, the client won't be in sync)
So here's my idea at first:
Each player gets 2 goroutines when they connect: one for sending and other for receiving.
// in the server main loop
select {
case player.sendChan <- somepacket:
default:
}
// this is sendChan:
p.sendChan := make(chan Packet, 100)
// in server player's sending loop (in their own goroutine)
for {
packet := <- p.sendChan:
sendPacket(packet) // this will block
}
So here the server's mainloop can send at most 100 packets to the player channel without blocking, while the sendPacket is blocking (due to lag maybe).
But the problem is if the player is blocking after 100 packets, the server will stop. That is obviously bad. And Go has no way to specify unbounded channels.
Then I thought about launching a new gorouting for each sendPacket but that seems like it would waste too much system resources and make the whole thing slow.
So the question is: What is the best way? I don't think the server should waste resources for a laggy client, but at the same time, they should be sent all packets. Is there a better way to do this? (I'm not sure how it's done in the real world so any other solutions would be fine too)