Implement Shutdown()

This is working for TCP, but for UDP the Read does not honor the
deadline settings.
This commit is contained in:
Miek Gieben 2014-08-19 09:25:41 +00:00
parent f5fe400c53
commit 6c631cd4ff
2 changed files with 28 additions and 4 deletions

View File

@ -305,8 +305,13 @@ func (srv *Server) ActivateAndServe() error {
// Shutdown shuts down a server. After a call to Shutdown, ListenAndServe and
// ActivateAndServe will return.
func (srv *Server) Shutdown() {
srv.stopTCP <- true
srv.stopUDP <- true
switch srv.Net {
case "tcp", "tcp4", "tcp6":
srv.stopTCP <- true
case "udp", "udp4", "udp6":
// TODO(miek): does not work for udp
// srv.stopUDP <- true
}
}
// serveTCP starts a TCP listener for the server.
@ -481,7 +486,9 @@ func (srv *Server) readTCP(conn *net.TCPConn, timeout time.Duration) ([]byte, er
}
func (srv *Server) readUDP(conn *net.UDPConn, timeout time.Duration) ([]byte, *sessionUDP, error) {
conn.SetReadDeadline(time.Now().Add(timeout))
m := make([]byte, srv.UDPSize)
// TODO(miek): deadline and timeout seem not to be honered
n, s, e := readFromSessionUDP(conn, m)
if e != nil || n == 0 {
if e != nil {

View File

@ -273,7 +273,23 @@ func TestServingLargeResponses(t *testing.T) {
}
}
func TestShutdown(t *testing.T) {
func TestShutdownTCP(t *testing.T) {
server := Server{Addr: ":8053", Net: "tcp"}
go func() {
err := server.ListenAndServe()
if err != nil {
t.Logf("failed to setup the tcp server: %s\n", err.Error())
t.Fail()
}
t.Logf("successfully stopped the tcp server")
}()
time.Sleep(4e8)
server.Shutdown()
time.Sleep(1 * time.Second)
}
// TODO(miek): does not work for udp
func TestShutdownUDP(t *testing.T) {
server := Server{Addr: ":8053", Net: "udp"}
go func() {
err := server.ListenAndServe()
@ -281,8 +297,9 @@ func TestShutdown(t *testing.T) {
t.Logf("failed to setup the udp server: %s\n", err.Error())
t.Fail()
}
t.Logf("successfully stopped the server")
t.Logf("successfully stopped the udp server")
}()
time.Sleep(4e8)
server.Shutdown()
time.Sleep(1 * time.Second)
}