Reconnect after waiting too long for request

This commit is contained in:
Bilal
2020-05-13 18:23:56 +02:00
parent e9b6aec79d
commit a709ac2dc5
3 changed files with 32 additions and 1 deletions

View File

@@ -45,7 +45,35 @@ func startSingleConnection(connectionId int) {
}
for {
encodedRequestMessage, err := bufio.NewReader(conn).ReadString('\n')
timeout := make(chan bool, 1)
go func() {
time.Sleep(time.Duration(c.ClientConfig.WaitingTimeout) * time.Second)
timeout <- true
}()
requestChann := make(chan bool, 1)
var err error
var encodedRequestMessage string
go func() {
encodedRequestMessage, err = bufio.NewReader(conn).ReadString('\n')
requestChann <- true
}()
timeoutConnection := false
select {
case <-requestChann:
timeoutConnection = false
case <-timeout:
timeoutConnection = true
}
if timeoutConnection {
log.Printf("(%d) Server not sending requests for too long, closing connection\n", connectionId)
_ = conn.Close()
break
}
if err != nil {
log.Printf("(%d) Error receiving request from load balancer server : %s\n", connectionId, err)
_ = conn.Close()