Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

public EndlessServer, so we could wrap it #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions endless.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -73,7 +73,7 @@ func init() {
}
}

type endlessServer struct {
type EndlessServer struct {
http.Server
EndlessListener net.Listener
SignalHooks map[int]map[os.Signal][]func()
Expand All @@ -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() {
Expand All @@ -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,
Expand Down Expand Up @@ -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()

Expand All @@ -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)
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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.
Expand All @@ -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()

Expand Down Expand Up @@ -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) {
Expand All @@ -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,
Expand All @@ -534,7 +534,7 @@ func (el *endlessListener) File() *os.File {

type endlessConn struct {
net.Conn
server *endlessServer
server *EndlessServer
}

func (w endlessConn) Close() error {
Expand All @@ -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
Expand Down