Enable TCP keep-alive on all connections
This commit is contained in:
parent
d484e6e374
commit
1141698a92
17
server.go
17
server.go
@ -5,10 +5,25 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"gopkg.in/irc.v3"
|
"gopkg.in/irc.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TODO: make configurable
|
||||||
|
var keepAlivePeriod = time.Minute
|
||||||
|
|
||||||
|
func setKeepAlive(c net.Conn) error {
|
||||||
|
tcpConn, ok := c.(*net.TCPConn)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("cannot enable keep-alive on a non-TCP connection")
|
||||||
|
}
|
||||||
|
if err := tcpConn.SetKeepAlive(true); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return tcpConn.SetKeepAlivePeriod(keepAlivePeriod)
|
||||||
|
}
|
||||||
|
|
||||||
type Logger interface {
|
type Logger interface {
|
||||||
Print(v ...interface{})
|
Print(v ...interface{})
|
||||||
Printf(format string, v ...interface{})
|
Printf(format string, v ...interface{})
|
||||||
@ -177,6 +192,8 @@ func (s *Server) Serve(ln net.Listener) error {
|
|||||||
return fmt.Errorf("failed to accept connection: %v", err)
|
return fmt.Errorf("failed to accept connection: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setKeepAlive(netConn)
|
||||||
|
|
||||||
dc := newDownstreamConn(s, netConn)
|
dc := newDownstreamConn(s, netConn)
|
||||||
go func() {
|
go func() {
|
||||||
s.lock.Lock()
|
s.lock.Lock()
|
||||||
|
@ -56,6 +56,8 @@ func connectToUpstream(u *user, upstream *Upstream) (*upstreamConn, error) {
|
|||||||
return nil, fmt.Errorf("failed to dial %q: %v", upstream.Addr, err)
|
return nil, fmt.Errorf("failed to dial %q: %v", upstream.Addr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setKeepAlive(netConn)
|
||||||
|
|
||||||
msgs := make(chan *irc.Message, 64)
|
msgs := make(chan *irc.Message, 64)
|
||||||
uc := &upstreamConn{
|
uc := &upstreamConn{
|
||||||
upstream: upstream,
|
upstream: upstream,
|
||||||
|
Loading…
Reference in New Issue
Block a user