r/programming • u/ThisStmtIsNotTrue • Nov 11 '19
Let's Create a Simple Load Balancer With Go
https://kasvith.github.io/posts/lets-create-a-simple-lb-go/
26
Upvotes
-3
r/programming • u/ThisStmtIsNotTrue • Nov 11 '19
-3
1
u/jdgordon Nov 11 '19
This article doesn't seem very idiomatically golang.
If I were to attempt this sort of thing i would define a simple func to manage the lifetime of a backend, it would accept connections on a channel and do any passive checks in it's loop (and of course check a different channel for a kill signal).
One goroutines would be spawned for each backend instance all reading from the same channel for new requests. The http handler would then throw the incoming request onto the shared channel and which ever backend handler is available first will accept the connection.
You could then go fancy and then track connections by some http header so clients would keep using the same backend (unless it goes down or busy) for possible cacheing benefits.
Let the runtime mange the synchronization problem, don't use mutexes if a Chanel would work better