Commit 5d4283aa authored by AlexStocks's avatar AlexStocks

add server.Shutdown in (Server)stop

parent 4cf247e1
......@@ -11,6 +11,13 @@
## develop history ##
---
- 2017/02/08
> improvement
>
> 1 server shutdown in server.go:(Server) stop()
>
> 2 version: 0.7.01
- 2017/02/03
> 1 Session struct -> session struct and add Session interface
>
......@@ -23,12 +30,12 @@
- 2016/11/19
> 1 add conn.go:(*gettyWSConn) setCompressType to add zip compress feature for ws connection
>
> 2 version: 0.6.2
> 2 version: 0.6.02
- 2016/11/16
> 1 add zip/snappy compress for tcp connection
>
> 2 version: 0.6.1
> 2 version: 0.6.01
- 2016/11/02
> 1 add session.go:Session{ID(), LocalAddr(), RemoteAddr()}
......@@ -217,7 +224,7 @@
>
> 5 use err instead of nerr in session.go:(Session)handlePackage:defer:OnError
>
> 6 version: 0.2.07
> 6 version: 0.2.07
- 2016/08/25
> 1 move close done to once clause in server.go:(Server)stop
......
......@@ -10,6 +10,7 @@
package getty
import (
"context"
"crypto/tls"
"errors"
"net"
......@@ -21,18 +22,22 @@ import (
import (
"github.com/AlexStocks/goext/net"
"github.com/AlexStocks/goext/sync"
"github.com/AlexStocks/goext/time"
log "github.com/AlexStocks/log4go"
"github.com/gorilla/websocket"
)
var (
errSelfConnect = errors.New("connect self!")
errSelfConnect = errors.New("connect self!")
serverFastFailTimeout = gxtime.TimeSecondDuration(2)
)
type Server struct {
// net
addr string
listener net.Listener
lock sync.Mutex // for server
server *http.Server // for ws or wss server
sync.Once
done chan gxsync.Empty
......@@ -44,12 +49,26 @@ func NewServer() *Server {
}
func (s *Server) stop() {
var (
err error
ctx context.Context
)
select {
case <-s.done:
return
default:
s.Once.Do(func() {
close(s.done)
s.lock.Lock()
if s.server != nil {
ctx, _ = context.WithTimeout(context.Background(), serverFastFailTimeout)
if err = s.server.Shutdown(ctx); err != nil {
// 如果下面内容输出为:server shutdown ctx: context deadline exceeded,
// 则说明有未处理完的active connections。
log.Error("server shutdown ctx:%#v", err)
}
}
s.lock.Unlock()
// 把listener.Close放在这里,既能防止多次关闭调用,
// 又能及时让Server因accept返回错误而从RunEventloop退出
s.listener.Close()
......@@ -196,15 +215,20 @@ func (s *Server) RunWSEventLoop(newSession NewSessionCallback, path string) {
var (
err error
handler *wsHandler
server *http.Server
)
handler = newWSHandler(s, newSession)
handler.HandleFunc(path, handler.serveWSRequest)
err = (&http.Server{
server = &http.Server{
Addr: s.addr,
Handler: handler,
// ReadTimeout: server.HTTPTimeout,
// WriteTimeout: server.HTTPTimeout,
}).Serve(s.listener)
}
s.lock.Lock()
s.server = server
s.lock.Unlock()
err = server.Serve(s.listener)
if err != nil {
log.Error("http.Server.Serve(addr{%s}) = err{%#v}", s.addr, err)
// panic(err)
......@@ -242,6 +266,9 @@ func (s *Server) RunWSEventLoopWithTLS(newSession NewSessionCallback, path strin
// WriteTimeout: server.HTTPTimeout,
}
server.SetKeepAlivesEnabled(true)
s.lock.Lock()
s.server = server
s.lock.Unlock()
err = server.Serve(tls.NewListener(s.listener, config))
if err != nil {
log.Error("http.Server.Serve(addr{%s}) = err{%#v}", s.addr, err)
......
......@@ -10,9 +10,9 @@
package getty
const (
Version = "0.7.0"
DATE = "2017/02/03"
Version = "0.7.01"
DATE = "2017/02/08"
GETTY_MAJOR = 0
GETTY_MINOR = 7
GETTY_BUILD = 0
GETTY_BUILD = 1
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment