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
Show file tree
Hide file tree
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
36 changes: 32 additions & 4 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 @@ -143,16 +146,38 @@ func NewServer(addr string, handler http.Handler) (srv *endlessServer) {
return
}

/*
ListenAndServeCtx 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 ListenAndServeCtx(ctx context.Context, addr string, handler http.Handler) error {
server := NewServer(ctx, addr, handler)
return server.ListenAndServe()
}

/*
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)
server := NewServer(context.Background(), addr, handler)
return server.ListenAndServe()
}

/*
ListenAndServeTLSCtx acts identically to ListenAndServe, except that it expects
HTTPS connections. Additionally, files containing a certificate and matching
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 ListenAndServeTLSCtx(ctx context.Context, addr string, certFile string, keyFile string, handler http.Handler) error {
server := NewServer(ctx, addr, handler)
return server.ListenAndServeTLS(certFile, keyFile)
}

/*
ListenAndServeTLS acts identically to ListenAndServe, except that it expects
HTTPS connections. Additionally, files containing a certificate and matching
Expand All @@ -161,7 +186,7 @@ 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)
server := NewServer(context.Background(), addr, handler)
return server.ListenAndServeTLS(certFile, keyFile)
}

Expand Down Expand Up @@ -192,7 +217,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 +403,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
2 changes: 1 addition & 1 deletion examples/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"os"
"syscall"

"github.com/fvbock/endless"
"github.com/gorilla/mux"
"github.com/voyager3m/endless/v2"
)

func handler(w http.ResponseWriter, r *http.Request) {
Expand Down
7 changes: 4 additions & 3 deletions examples/multi_port.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package main

import (
"context"
"log"
"net/http"
"os"
"sync"
"time"

"github.com/fvbock/endless"
"github.com/gorilla/mux"
"github.com/voyager3m/endless"
)

func handler(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -41,15 +42,15 @@ func main() {
w.Add(2)
go func() {
time.Sleep(time.Second)
err := endless.ListenAndServe("localhost:4242", mux1)
err := endless.ListenAndServe(context.Background(), "localhost:4242", mux1)
if err != nil {
log.Println(err)
}
log.Println("Server on 4242 stopped")
w.Done()
}()
go func() {
err := endless.ListenAndServe("localhost:4243", mux2)
err := endless.ListenAndServe(context.Background(), "localhost:4243", mux2)
if err != nil {
log.Println(err)
}
Expand Down
5 changes: 3 additions & 2 deletions examples/simple.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package main

import (
"context"
"log"
"net/http"
"os"

"github.com/fvbock/endless"
"github.com/gorilla/mux"
"github.com/voyager3m/endless"
)

func handler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -18,7 +19,7 @@ func main() {
mux1.HandleFunc("/hello", handler).
Methods("GET")

err := endless.ListenAndServe("localhost:4242", mux1)
err := endless.ListenAndServe(context.Background(), "localhost:4242", mux1)
if err != nil {
log.Println(err)
}
Expand Down
5 changes: 3 additions & 2 deletions examples/testserver.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package main

import (
"context"
"log"
"math/rand"
"net/http"
"os"
"time"

"github.com/fvbock/endless"
"github.com/gorilla/mux"
"github.com/voyager3m/endless"
)

func handler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -22,7 +23,7 @@ func main() {
mux.HandleFunc("/foo", handler).
Methods("GET")

err := endless.ListenAndServe("localhost:4242", mux)
err := endless.ListenAndServe(context.Background(), "localhost:4242", mux)
if err != nil {
log.Println(err)
}
Expand Down
5 changes: 3 additions & 2 deletions examples/tls.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package main

import (
"context"
"log"
"net/http"
"os"

"github.com/fvbock/endless"
"github.com/gorilla/mux"
"github.com/voyager3m/endless"
)

func handler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -19,7 +20,7 @@ func main() {
mux1.HandleFunc("/hello", handler).
Methods("GET")

err := endless.ListenAndServeTLS("localhost:4242", "cert.pem", "key.pem", mux1)
err := endless.ListenAndServeTLS(context.Background(), "localhost:4242", "cert.pem", "key.pem", mux1)
if err != nil {
log.Println(err)
}
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/voyager3m/endless/v2

go 1.15