mhttp: refactor to use Components
This commit is contained in:
parent
d0ba23ac7b
commit
e741e79acf
@ -10,7 +10,7 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/mediocregopher/mediocre-go-lib/mctx"
|
"github.com/mediocregopher/mediocre-go-lib/mcmp"
|
||||||
"github.com/mediocregopher/mediocre-go-lib/merr"
|
"github.com/mediocregopher/mediocre-go-lib/merr"
|
||||||
"github.com/mediocregopher/mediocre-go-lib/mlog"
|
"github.com/mediocregopher/mediocre-go-lib/mlog"
|
||||||
"github.com/mediocregopher/mediocre-go-lib/mnet"
|
"github.com/mediocregopher/mediocre-go-lib/mnet"
|
||||||
@ -20,49 +20,51 @@ import (
|
|||||||
// Server is returned by WithListeningServer and simply wraps an *http.Server.
|
// Server is returned by WithListeningServer and simply wraps an *http.Server.
|
||||||
type Server struct {
|
type Server struct {
|
||||||
*http.Server
|
*http.Server
|
||||||
ctx context.Context
|
cmp *mcmp.Component
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithListeningServer returns a *Server which will be initialized and have
|
// InstListeningServer returns a *Server which will be initialized and have
|
||||||
// ListenAndServe called on it (asynchronously) when the start event is
|
// ListenAndServe called on it (asynchronously) when the Init event is triggered
|
||||||
// triggered on the returned Context (see mrun.Start). The Server will have
|
// on the Component. The Server will have Shutdown called on it when the
|
||||||
// Shutdown called on it when the stop event is triggered on the returned
|
// Shutdown event is triggered on the Component.
|
||||||
// Context (see mrun.Stop).
|
|
||||||
//
|
//
|
||||||
// This function automatically handles setting up configuration parameters via
|
// This function automatically handles setting up configuration parameters via
|
||||||
// mcfg. The default listen address is ":0".
|
// mcfg. The default listen address is ":0".
|
||||||
func WithListeningServer(ctx context.Context, h http.Handler) (context.Context, *Server) {
|
func InstListeningServer(cmp *mcmp.Component, h http.Handler) *Server {
|
||||||
srv := &Server{
|
srv := &Server{
|
||||||
Server: &http.Server{Handler: h},
|
Server: &http.Server{Handler: h},
|
||||||
ctx: mctx.NewChild(ctx, "http"),
|
cmp: cmp.Child("http"),
|
||||||
}
|
}
|
||||||
|
|
||||||
var listener *mnet.Listener
|
listener := mnet.InstListener(srv.cmp,
|
||||||
srv.ctx, listener = mnet.WithListener(srv.ctx)
|
// http.Server.Shutdown will handle this
|
||||||
listener.NoCloseOnStop = true // http.Server.Shutdown will do this
|
mnet.ListenerCloseOnShutdown(false),
|
||||||
|
)
|
||||||
|
|
||||||
srv.ctx = mrun.WithStartHook(srv.ctx, func(context.Context) error {
|
threadCtx := context.Background()
|
||||||
|
mrun.InitHook(srv.cmp, func(ctx context.Context) error {
|
||||||
srv.Addr = listener.Addr().String()
|
srv.Addr = listener.Addr().String()
|
||||||
srv.ctx = mrun.WithThreads(srv.ctx, 1, func() error {
|
threadCtx = mrun.WithThreads(threadCtx, 1, func() error {
|
||||||
mlog.Info("serving requests", srv.ctx)
|
mlog.From(srv.cmp).Info("serving requests", ctx)
|
||||||
if err := srv.Serve(listener); !merr.Equal(err, http.ErrServerClosed) {
|
if err := srv.Serve(listener); !merr.Equal(err, http.ErrServerClosed) {
|
||||||
mlog.Error("error serving listener", srv.ctx, merr.Context(err))
|
mlog.From(srv.cmp).Error("error serving listener", ctx, merr.Context(err))
|
||||||
return err
|
return merr.Wrap(err, srv.cmp.Context(), ctx)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
srv.ctx = mrun.WithStopHook(srv.ctx, func(innerCtx context.Context) error {
|
mrun.ShutdownHook(srv.cmp, func(ctx context.Context) error {
|
||||||
mlog.Info("shutting down server", srv.ctx)
|
mlog.From(srv.cmp).Info("shutting down server", ctx)
|
||||||
if err := srv.Shutdown(innerCtx); err != nil {
|
if err := srv.Shutdown(ctx); err != nil {
|
||||||
return err
|
return merr.Wrap(err, srv.cmp.Context(), ctx)
|
||||||
}
|
}
|
||||||
return mrun.Wait(srv.ctx, innerCtx.Done())
|
err := mrun.Wait(threadCtx, ctx.Done())
|
||||||
|
return merr.Wrap(err, srv.cmp.Context(), ctx)
|
||||||
})
|
})
|
||||||
|
|
||||||
return mctx.WithChild(ctx, srv.ctx), srv
|
return srv
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddXForwardedFor populates the X-Forwarded-For header on the Request to
|
// AddXForwardedFor populates the X-Forwarded-For header on the Request to
|
||||||
|
@ -13,13 +13,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestMListenAndServe(t *T) {
|
func TestMListenAndServe(t *T) {
|
||||||
ctx := mtest.Context()
|
cmp := mtest.Component()
|
||||||
|
|
||||||
ctx, srv := WithListeningServer(ctx, http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
srv := InstListeningServer(cmp, http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||||
io.Copy(rw, r.Body)
|
io.Copy(rw, r.Body)
|
||||||
}))
|
}))
|
||||||
|
|
||||||
mtest.Run(ctx, t, func() {
|
mtest.Run(cmp, t, func() {
|
||||||
body := bytes.NewBufferString("HELLO")
|
body := bytes.NewBufferString("HELLO")
|
||||||
resp, err := http.Post("http://"+srv.Addr, "text/plain", body)
|
resp, err := http.Post("http://"+srv.Addr, "text/plain", body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user