From d819de6f77adb1dc347527c68d140db5e213f23b Mon Sep 17 00:00:00 2001 From: xuxiangyang Date: Wed, 18 May 2016 14:21:24 +0800 Subject: [PATCH] public EndlessServer, so we could wrap it --- endless.go | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/endless.go b/endless.go index 45f156e..24f226d 100644 --- a/endless.go +++ b/endless.go @@ -32,7 +32,7 @@ const ( var ( runningServerReg sync.RWMutex - runningServers map[string]*endlessServer + runningServers map[string]*EndlessServer runningServersOrder []string socketPtrOffsetMap map[string]uint runningServersForked bool @@ -53,7 +53,7 @@ func init() { flag.StringVar(&socketOrder, "socketorder", "", "previous initialization order - used when more than one listener was started") runningServerReg = sync.RWMutex{} - runningServers = make(map[string]*endlessServer) + runningServers = make(map[string]*EndlessServer) runningServersOrder = []string{} socketPtrOffsetMap = make(map[string]uint) @@ -73,7 +73,7 @@ func init() { } } -type endlessServer struct { +type EndlessServer struct { http.Server EndlessListener net.Listener SignalHooks map[int]map[os.Signal][]func() @@ -87,10 +87,10 @@ type endlessServer struct { } /* -NewServer returns an intialized endlessServer Object. Calling Serve on it will +NewServer returns an intialized EndlessServer Object. Calling Serve on it will actually "start" the server. */ -func NewServer(addr string, handler http.Handler) (srv *endlessServer) { +func NewServer(addr string, handler http.Handler) (srv *EndlessServer) { runningServerReg.Lock() defer runningServerReg.Unlock() if !flag.Parsed() { @@ -104,7 +104,7 @@ func NewServer(addr string, handler http.Handler) (srv *endlessServer) { socketPtrOffsetMap[addr] = uint(len(runningServersOrder)) } - srv = &endlessServer{ + srv = &EndlessServer{ wg: sync.WaitGroup{}, sigChan: make(chan os.Signal), isChild: isChild, @@ -168,14 +168,14 @@ func ListenAndServeTLS(addr string, certFile string, keyFile string, handler htt return server.ListenAndServeTLS(certFile, keyFile) } -func (srv *endlessServer) getState() uint8 { +func (srv *EndlessServer) getState() uint8 { srv.lock.RLock() defer srv.lock.RUnlock() return srv.state } -func (srv *endlessServer) setState(st uint8) { +func (srv *EndlessServer) setState(st uint8) { srv.lock.Lock() defer srv.lock.Unlock() @@ -192,7 +192,7 @@ In addition to the stl Serve behaviour each connection is added to a sync.Waitgroup so that all outstanding connections can be served before shutting down the server. */ -func (srv *endlessServer) Serve() (err error) { +func (srv *EndlessServer) Serve() (err error) { defer log.Println(syscall.Getpid(), "Serve() returning...") srv.setState(STATE_RUNNING) err = srv.Server.Serve(srv.EndlessListener) @@ -207,7 +207,7 @@ ListenAndServe listens on the TCP network address srv.Addr and then calls Serve to handle requests on incoming connections. If srv.Addr is blank, ":http" is used. */ -func (srv *endlessServer) ListenAndServe() (err error) { +func (srv *EndlessServer) ListenAndServe() (err error) { addr := srv.Addr if addr == "" { addr = ":http" @@ -243,7 +243,7 @@ CA's certificate. If srv.Addr is blank, ":https" is used. */ -func (srv *endlessServer) ListenAndServeTLS(certFile, keyFile string) (err error) { +func (srv *EndlessServer) ListenAndServeTLS(certFile, keyFile string) (err error) { addr := srv.Addr if addr == "" { addr = ":https" @@ -286,7 +286,7 @@ func (srv *endlessServer) ListenAndServeTLS(certFile, keyFile string) (err error getListener either opens a new socket to listen on, or takes the acceptor socket it got passed when restarted. */ -func (srv *endlessServer) getListener(laddr string) (l net.Listener, err error) { +func (srv *EndlessServer) getListener(laddr string) (l net.Listener, err error) { if srv.isChild { var ptrOffset uint = 0 runningServerReg.RLock() @@ -316,7 +316,7 @@ func (srv *endlessServer) getListener(laddr string) (l net.Listener, err error) handleSignals listens for os Signals and calls any hooked in function that the user had registered with the signal. */ -func (srv *endlessServer) handleSignals() { +func (srv *EndlessServer) handleSignals() { var sig os.Signal signal.Notify( @@ -355,7 +355,7 @@ func (srv *endlessServer) handleSignals() { } } -func (srv *endlessServer) signalHooks(ppFlag int, sig os.Signal) { +func (srv *EndlessServer) signalHooks(ppFlag int, sig os.Signal) { if _, notSet := srv.SignalHooks[ppFlag][sig]; !notSet { return } @@ -370,7 +370,7 @@ shutdown closes the listener so that no new connections are accepted. it also starts a goroutine that will hammer (stop all running requests) the server after DefaultHammerTime. */ -func (srv *endlessServer) shutdown() { +func (srv *EndlessServer) shutdown() { if srv.getState() != STATE_RUNNING { return } @@ -398,7 +398,7 @@ srv.Serve() will not return until all connections are served. this will unblock the srv.wg.Wait() in Serve() thus causing ListenAndServe(TLS) to return. */ -func (srv *endlessServer) hammerTime(d time.Duration) { +func (srv *EndlessServer) hammerTime(d time.Duration) { defer func() { // we are calling srv.wg.Done() until it panics which means we called // Done() when the counter was already at 0 and we're done. @@ -421,7 +421,7 @@ func (srv *endlessServer) hammerTime(d time.Duration) { } } -func (srv *endlessServer) fork() (err error) { +func (srv *EndlessServer) fork() (err error) { runningServerReg.Lock() defer runningServerReg.Unlock() @@ -486,7 +486,7 @@ func (srv *endlessServer) fork() (err error) { type endlessListener struct { net.Listener stopped bool - server *endlessServer + server *EndlessServer } func (el *endlessListener) Accept() (c net.Conn, err error) { @@ -507,7 +507,7 @@ func (el *endlessListener) Accept() (c net.Conn, err error) { return } -func newEndlessListener(l net.Listener, srv *endlessServer) (el *endlessListener) { +func newEndlessListener(l net.Listener, srv *EndlessServer) (el *endlessListener) { el = &endlessListener{ Listener: l, server: srv, @@ -534,7 +534,7 @@ func (el *endlessListener) File() *os.File { type endlessConn struct { net.Conn - server *endlessServer + server *EndlessServer } func (w endlessConn) Close() error { @@ -550,7 +550,7 @@ RegisterSignalHook registers a function to be run PRE_SIGNAL or POST_SIGNAL for a given signal. PRE or POST in this case means before or after the signal related code endless itself runs */ -func (srv *endlessServer) RegisterSignalHook(prePost int, sig os.Signal, f func()) (err error) { +func (srv *EndlessServer) RegisterSignalHook(prePost int, sig os.Signal, f func()) (err error) { if prePost != PRE_SIGNAL && prePost != POST_SIGNAL { err = fmt.Errorf("Cannot use %v for prePost arg. Must be endless.PRE_SIGNAL or endless.POST_SIGNAL.") return