correctly disconnect from upstream on client disconnect

--HG--
branch : nmdc-ircfrontend
This commit is contained in:
. 2016-05-07 12:59:28 +12:00
parent cc04626518
commit cc460e2fe9

View File

@ -23,6 +23,7 @@ import (
"fmt" "fmt"
"io" "io"
"libnmdc" "libnmdc"
"log"
"net" "net"
"strings" "strings"
"time" "time"
@ -70,11 +71,14 @@ func (s *Server) RunWorker() {
} }
buf := make([]byte, CLIENT_READ_BUFFSIZE) buf := make([]byte, CLIENT_READ_BUFFSIZE)
//s.clientConn.SetReadDeadline(time.Now().Add(5 * time.Second))
ln, err := s.clientConn.Read(buf) ln, err := s.clientConn.Read(buf)
if err != nil { if err != nil {
if err == io.EOF { if err == io.EOF {
break // abandon thread break // abandon thread
} }
//log.Println(err.Error())
continue continue
} }
@ -100,17 +104,16 @@ func (s *Server) RunWorker() {
} }
} }
// Cleanup log.Println("Broken loop.")
s.DisconnectClient() // if not already done
// Cleanup upstream // Cleanup upstream
if s.clientConn != nil && s.clientRegistered { if s.clientRegistered {
s.upstreamCloser <- struct{}{} s.upstreamCloser <- struct{}{} // always safe to do this /once/
} }
// Clean up ourselves // Clean up ourselves
s.DisconnectClient() // if not already done
s.clientRegistered = false s.clientRegistered = false
} }
func (s *Server) upstreamWorker() { func (s *Server) upstreamWorker() {
@ -120,6 +123,7 @@ func (s *Server) upstreamWorker() {
select { select {
case <-s.upstreamCloser: case <-s.upstreamCloser:
// Abandon the upstream connection // Abandon the upstream connection
log.Println("Abandoning upstream connection...")
s.upstream.Disconnect() s.upstream.Disconnect()
return return
@ -151,7 +155,8 @@ func (s *Server) upstreamWorker() {
} }
case libnmdc.EVENT_SYSTEM_MESSAGE_FROM_CONN, libnmdc.EVENT_SYSTEM_MESSAGE_FROM_HUB: case libnmdc.EVENT_SYSTEM_MESSAGE_FROM_CONN, libnmdc.EVENT_SYSTEM_MESSAGE_FROM_HUB:
s.sendClientGlobalMessage(hubEvent.Message) s.reply(rplMsg, BLESSED_CHANNEL, BLESSED_CHANNEL, hubEvent.Message) // experimental
// s.sendClientGlobalMessage(hubEvent.Message)
} }
} }