multithreading - Why is my goroutine starving? -


overview

i writing program server launched in 1 goroutine. server has few different goroutines:

  1. the main goroutine: handles initialization, launches second goroutine listens new connections (2), , goes infinite loop act on data received connections (i.e., clients).
  2. 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:

  1. the main goroutine: handles intialization, registers client server, launches second goroutine read data server (2), , goes infinite loop act on data received server.
  2. 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

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -