Skip to content

Commit

Permalink
Merge pull request #1424 from guozhao-coder/nsqd-freememory-api
Browse files Browse the repository at this point in the history
nsqd: add debug http endpoint to release memory to OS
  • Loading branch information
ploxiln authored Sep 23, 2022
2 parents a1889c4 + f3c72cc commit 393f7b9
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions nsqd/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os"
"reflect"
"runtime"
"runtime/debug"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -85,6 +86,7 @@ func newHTTPServer(nsqd *NSQD, tlsEnabled bool, tlsRequired bool) *httpServer {
router.Handler("GET", "/debug/pprof/goroutine", pprof.Handler("goroutine"))
router.Handler("GET", "/debug/pprof/block", pprof.Handler("block"))
router.Handle("PUT", "/debug/setblockrate", http_api.Decorate(setBlockRateHandler, log, http_api.PlainText))
router.Handle("POST", "/debug/freememory", http_api.Decorate(freeMemory, log, http_api.PlainText))
router.Handler("GET", "/debug/pprof/threadcreate", pprof.Handler("threadcreate"))

return s
Expand All @@ -99,6 +101,11 @@ func setBlockRateHandler(w http.ResponseWriter, req *http.Request, ps httprouter
return nil, nil
}

func freeMemory(w http.ResponseWriter, req *http.Request, ps httprouter.Params) (interface{}, error) {
debug.FreeOSMemory()
return nil, nil
}

func (s *httpServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if !s.tlsEnabled && s.tlsRequired {
resp := fmt.Sprintf(`{"message": "TLS_REQUIRED", "https_port": %d}`,
Expand Down

0 comments on commit 393f7b9

Please sign in to comment.