multithreading - Why is my goroutine starving? -
overview
i writing program server launched in 1 goroutine. server has few different goroutines:
- the main goroutine: handles initialization, launches second goroutine listens new connections (2), , goes infinite loop act on data received connections (i.e., clients).
- the listen goroutine: goroutine goes infinite loop, listening new connections. if new connection accepted, goroutine launched listens messages connection until closed.
the server seems work well. can add many new connections, , can send data on these new connections after accepted server.
my client simple. there 2 goroutines well:
- the main goroutine: handles intialization, registers client server, launches second goroutine read data server (2), , goes infinite loop act on data received server.
- the second goroutine: goroutine tries read data server until connection closed.
starvation
i'm having huge issue goroutine starvation on client. specifically, second goroutine of client starves. here's source of goroutine that's starving:
func receiver() { { msg, err := bufio.newreader(conn).readstring(byte(protocol.endofmessage)) if err != nil { fmt.printf("disconnected server %v.\n", conn.remoteaddr()) return } if len(msg) < 2 { continue } receivertohandler <- msg[1 : len(msg)-1] } }
i message being sent server client. sure message being sent ends in protocol.endofmessage
. method of getting data server correct since use same code register client, instead of running in infinite loop allow prespecified number of attempts.
for reason, client not receive data.
to not misunderstanding nature of goroutines, if replace above code this:
func receiver() { { fmt.println("in receiver goroutine!") } }
the goroutine works expected: functions before, "in receiver goroutine!" printed. routine executing correctly in case.
my handler
func handlemessage(debug bool) { select { case msg := <-receivertohandler: update(msg, debug) default: /* if debug { * fmt.printf("nothing received!\n") * } */ } }
right now, update(msg, debug)
calls fmt.println(msg)
.
is there can cleanly solve problem? feel giving away priority / forcing scheduler run hacky solution problem.
Comments
Post a Comment