From f3c72cc2a131728fa86e3573fe8655c83bc9205d Mon Sep 17 00:00:00 2001 From: guozhao Date: Thu, 15 Sep 2022 18:10:29 +0800 Subject: [PATCH] nsqd: add memory release function In some cases where the message volume is large, the memory occupied by the program cannot be returned to the operating system immediately. You can call this method to enforce execution. However, using this method may cause the system to pause for a period of time, so use it with caution. --- nsqd/http.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/nsqd/http.go b/nsqd/http.go index 309332336..7fa2a10e5 100644 --- a/nsqd/http.go +++ b/nsqd/http.go @@ -13,6 +13,7 @@ import ( "os" "reflect" "runtime" + "runtime/debug" "strconv" "strings" "time" @@ -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 @@ -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}`,