Skip to content

Commit

Permalink
removing healthchecks from appengine server
Browse files Browse the repository at this point in the history
  • Loading branch information
jprobinson committed Mar 5, 2016
1 parent 0dfea02 commit 057fa25
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 122 deletions.
54 changes: 4 additions & 50 deletions appengineserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,15 @@ package appengineserver

import (
"net/http"
"os"
"os/signal"
"syscall"

"github.com/NYTimes/gizmo/config"
"github.com/NYTimes/gizmo/healthcheck"
"github.com/NYTimes/gizmo/web"
)

// Server is the basic interface that defines what to expect from any server.
type Server interface {
ServeHTTP(http.ResponseWriter, *http.Request)
Register(Service) error
Start() error
Stop() error
}

var (
Expand All @@ -33,15 +27,6 @@ var (
// Init will set up our name, logging, healthchecks and parse flags. If DefaultServer isn't set,
// this func will set it to a `SimpleServer` listening on `Config.Server.HTTPPort`.
func Init(scfg *config.Server, service Service) {
InitVM(scfg)
err := server.Register(service)
if err != nil {
panic("unable to register service: " + err.Error())
}
http.Handle("/", server)
}

func InitVM(scfg *config.Server) {
// if no config given, attempt to pull one from
// the environment.
if scfg == nil {
Expand All @@ -61,42 +46,11 @@ func InitVM(scfg *config.Server) {
}

server = NewServer(scfg)
}

// Register will add a new Service to the DefaultServer.
func Register(svc Service) error {
return server.Register(svc)
}

// Run will start the DefaultServer and set it up to Stop()
// on a kill signal.
func Run() error {
if err := server.Start(); err != nil {
return err
}

// parse address for host, port
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT)
return Stop()
}

// Stop will stop the default server.
func Stop() error {
return server.Stop()
}

// NewHealthCheckHandler will inspect the config to generate
// the appropriate HealthCheckHandler.
func NewHealthCheckHandler(cfg *config.Server) healthcheck.Handler {
switch cfg.HealthCheckType {
case "simple":
return healthcheck.NewSimple(cfg.HealthCheckPath)
case "esx":
return healthcheck.NewESX()
default:
return healthcheck.NewSimple("/_ah/health")
err := server.Register(service)
if err != nil {
panic("unable to register service: " + err.Error())
}
http.Handle("/", server)
}

// NewServer will inspect the config and generate
Expand Down
74 changes: 2 additions & 72 deletions appengineserver/simple_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ package appengineserver

import (
"errors"
"fmt"
"net"
"net/http"

"github.com/NYTimes/gizmo/config"
"github.com/NYTimes/gizmo/healthcheck"
"github.com/gorilla/mux"
)

Expand All @@ -21,9 +18,6 @@ type SimpleServer struct {

// mux for routing
mux *mux.Router

// monitor
monitor *healthcheck.ActivityMonitor
}

// NewSimpleServer will init the mux, exit channel and
Expand All @@ -38,77 +32,13 @@ func NewSimpleServer(cfg *config.Server) *SimpleServer {
mx.NotFoundHandler = cfg.NotFoundHandler
}
return &SimpleServer{
mux: mx,
monitor: healthcheck.NewActivityMonitor(),
cfg: cfg,
}
}

// Start will start the SimpleServer at it's configured address.
// If they are configured, this will start emitting metrics to Graphite,
// register profiling, health checks and access logging.
func (s *SimpleServer) Start() error {
// register health check
hch := healthcheck.NewHandler(s.cfg)
err := hch.Start(s.monitor)
if err != nil {
panic("unable to start health check handler")
}
s.cfg.HealthCheckPath = hch.Path()
s.mux.Handle(hch.Path(), hch)

// create server
srv := http.Server{
Handler: s,
MaxHeaderBytes: maxHeaderBytes,
mux: mx,
cfg: cfg,
}

l, err := net.Listen("tcp", fmt.Sprintf(":%d", s.cfg.HTTPPort))
if err != nil {
return err
}

l = net.Listener(TCPKeepAliveListener{l.(*net.TCPListener)})

go func() {
if err := srv.Serve(l); err != nil {
panic("encountered an error while serving listener: " + err.Error())
}
}()

// join the LB
go func() {
exit := <-s.exit

// let the health check clean up if it needs to
if err := hch.Stop(); err != nil {
panic("health check Stop returned with error: " + err.Error())
}

// stop the listener
exit <- l.Close()
}()

return nil
}

// Stop initiates the shutdown process and returns when
// the server completes.
func (s *SimpleServer) Stop() error {
ch := make(chan error)
s.exit <- ch
return <-ch
}

// ServeHTTP is SimpleServer's hook for metrics and safely executing each request.
func (s *SimpleServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {

// only count healthcheck requests
if r.URL.Path != s.cfg.HealthCheckPath {
s.monitor.CountRequest()
defer s.monitor.UncountRequest()
}

// hand the request off to gorilla
s.mux.ServeHTTP(w, r)
}
Expand Down

0 comments on commit 057fa25

Please sign in to comment.