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

add context #56

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 2 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
18 changes: 12 additions & 6 deletions endless.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package endless

import (
"context"
"crypto/tls"
"errors"
"fmt"
Expand All @@ -15,7 +16,6 @@ import (
"sync"
"syscall"
"time"

// "github.com/fvbock/uds-go/introspect"
)

Expand Down Expand Up @@ -80,13 +80,15 @@ type endlessServer struct {
state uint8
lock *sync.RWMutex
BeforeBegin func(add string)
Ctx context.Context
Cancel context.CancelFunc
}

/*
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(ctx context.Context, addr string, handler http.Handler) (srv *endlessServer) {
runningServerReg.Lock()
defer runningServerReg.Unlock()

Expand Down Expand Up @@ -132,6 +134,7 @@ func NewServer(addr string, handler http.Handler) (srv *endlessServer) {
srv.Server.WriteTimeout = DefaultWriteTimeOut
srv.Server.MaxHeaderBytes = DefaultMaxHeaderBytes
srv.Server.Handler = handler
srv.Ctx, srv.Cancel = context.WithCancel(ctx)

srv.BeforeBegin = func(addr string) {
log.Println(syscall.Getpid(), addr)
Expand All @@ -148,8 +151,8 @@ ListenAndServe listens on the TCP network address addr and then calls Serve
with handler to handle requests on incoming connections. Handler is typically
nil, in which case the DefaultServeMux is used.
*/
func ListenAndServe(addr string, handler http.Handler) error {
server := NewServer(addr, handler)
func ListenAndServe(ctx context.Context, addr string, handler http.Handler) error {
server := NewServer(ctx, addr, handler)
return server.ListenAndServe()
}

Expand All @@ -160,8 +163,8 @@ private key for the server must be provided. If the certificate is signed by a
certificate authority, the certFile should be the concatenation of the server's
certificate followed by the CA's certificate.
*/
func ListenAndServeTLS(addr string, certFile string, keyFile string, handler http.Handler) error {
server := NewServer(addr, handler)
func ListenAndServeTLS(ctx context.Context, addr string, certFile string, keyFile string, handler http.Handler) error {
server := NewServer(ctx, addr, handler)
return server.ListenAndServeTLS(certFile, keyFile)
}

Expand Down Expand Up @@ -192,7 +195,9 @@ down the server.
func (srv *endlessServer) Serve() (err error) {
defer log.Println(syscall.Getpid(), "Serve() returning...")
srv.setState(STATE_RUNNING)
srv.Server.BaseContext = func(list net.Listener) context.Context { return srv.Ctx }
err = srv.Server.Serve(srv.EndlessListener)

log.Println(syscall.Getpid(), "Waiting for connections to finish...")
srv.wg.Wait()
srv.setState(STATE_TERMINATE)
Expand Down Expand Up @@ -376,6 +381,7 @@ func (srv *endlessServer) shutdown() {
if DefaultHammerTime >= 0 {
go srv.hammerTime(DefaultHammerTime)
}
srv.Cancel()
// disable keep-alives on existing connections
srv.SetKeepAlivesEnabled(false)
err := srv.EndlessListener.Close()
Expand Down