Skip to content

Commit

Permalink
main: register net/http/pprof on reloads too
Browse files Browse the repository at this point in the history
We didn't need to in the past as we layered the routers, meaning that we
never fully replaced the main router. Now that we do, we must set it up.
Otherwise, its routes will be 404s after any reload.

Do it in loadAPIEndpoints, since it is run in the same scenarios and we
want to put the pprof stuff in the control router anyway, since it's
Tyk's endpoint and not the user's.

Add a test too, ensuring that it survives reloads and that it won't be
on the non-control router.

Also a small file closing fix.
  • Loading branch information
mvdan authored and buger committed Sep 29, 2017
1 parent 1deacf4 commit be97a94
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
16 changes: 16 additions & 0 deletions gateway_test.go
Expand Up @@ -935,6 +935,22 @@ func TestControlListener(t *testing.T) {
testHttp(t, tests, true)
}

func TestHttpPprof(t *testing.T) {
old := httpProfile
defer func() { httpProfile = old }()

testHttp(t, []tykHttpTest{
{method: "GET", path: "/debug/pprof/", code: 404},
{method: "GET", path: "/debug/pprof/", code: 404, controlRequest: true},
}, true)
httpProfile = true
doReload()
testHttp(t, []tykHttpTest{
{method: "GET", path: "/debug/pprof/", code: 404},
{method: "GET", path: "/debug/pprof/", code: 200, controlRequest: true},
}, true)
}

func TestManagementNodeRedisEvents(t *testing.T) {
defer func() {
config.Global.ManagementNode = false
Expand Down
27 changes: 14 additions & 13 deletions main.go
Expand Up @@ -284,16 +284,22 @@ func loadAPIEndpoints(muxer *mux.Router) {
if config.Global.ControlAPIHostname != "" {
hostname = config.Global.ControlAPIHostname
}
r := mux.NewRouter()
muxer.PathPrefix("/tyk/").Handler(http.StripPrefix("/tyk",
checkIsAPIOwner(InstrumentationMW(r)),
))
var subr *mux.Router
if hostname != "" {
r = r.Host(hostname).Subrouter()
subr = muxer.Host(hostname).Subrouter()
log.WithFields(logrus.Fields{
"prefix": "main",
}).Info("Control API hostname set: ", hostname)
} else {
subr = muxer.NewRoute().Subrouter()
}
if httpProfile {
subr.HandleFunc("/debug/pprof/{_:.*}", pprof_http.Index)
}
r := mux.NewRouter()
subr.PathPrefix("/tyk/").Handler(http.StripPrefix("/tyk",
checkIsAPIOwner(InstrumentationMW(r)),
))
log.WithFields(logrus.Fields{
"prefix": "main",
}).Info("Initialising Tyk REST API Endpoints")
Expand Down Expand Up @@ -1010,7 +1016,10 @@ func main() {
time.Sleep(time.Second)
}

var httpProfile = false

func start(arguments map[string]interface{}) {
httpProfile = arguments["--httpprofile"] == true
if arguments["--memprofile"] == true {
log.WithFields(logrus.Fields{
"prefix": "main",
Expand All @@ -1032,13 +1041,6 @@ func start(arguments map[string]interface{}) {
pprof.StartCPUProfile(cpuProfFile)
defer pprof.StopCPUProfile()
}
if arguments["--httpprofile"] == true {
log.WithFields(logrus.Fields{
"prefix": "main",
}).Debug("Adding pprof endpoints")

mainRouter.HandleFunc("/debug/pprof/{_:.*}", pprof_http.Index)
}

// Set up a default org manager so we can traverse non-live paths
if !config.Global.SupressDefaultOrgStore {
Expand All @@ -1050,7 +1052,6 @@ func start(arguments map[string]interface{}) {
//DefaultQuotaStore.Init(getGlobalStorageHandler(CloudHandler, "orgkey.", false))
DefaultQuotaStore.Init(getGlobalStorageHandler("orgkey.", false))
}

if config.Global.ControlAPIPort == 0 {
loadAPIEndpoints(mainRouter)
}
Expand Down
2 changes: 1 addition & 1 deletion mw_js_plugin.go
Expand Up @@ -295,13 +295,13 @@ func (j *JSVM) Init() {
}).Error("Could not open TykJS: ", err)
return
}
defer f.Close()
if _, err := vm.Run(f); err != nil {
log.WithFields(logrus.Fields{
"prefix": "jsvm",
}).Error("Could not load TykJS: ", err)
return
}
f.Close()

j.VM = vm

Expand Down

0 comments on commit be97a94

Please sign in to comment.