mutex_profile.go 1.06 KB
Newer Older
1 2 3 4 5 6 7 8
package corehttp

import (
	"net"
	"net/http"
	"runtime"
	"strconv"

tavit ohanian's avatar
tavit ohanian committed
9
	core "gitlab.dms3.io/dms3/go-dms3/core"
10 11 12 13 14
)

// MutexFractionOption allows to set runtime.SetMutexProfileFraction via HTTP
// using POST request with parameter 'fraction'.
func MutexFractionOption(path string) ServeOption {
tavit ohanian's avatar
tavit ohanian committed
15
	return func(_ *core.Dms3Node, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
16 17
		mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
			if r.Method != http.MethodPost {
18
				http.Error(w, "only POST allowed", http.StatusMethodNotAllowed)
19 20 21
				return
			}
			if err := r.ParseForm(); err != nil {
22
				http.Error(w, err.Error(), http.StatusBadRequest)
23 24 25 26 27
				return
			}

			asfr := r.Form.Get("fraction")
			if len(asfr) == 0 {
28
				http.Error(w, "parameter 'fraction' must be set", http.StatusBadRequest)
29 30 31 32 33
				return
			}

			fr, err := strconv.Atoi(asfr)
			if err != nil {
34
				http.Error(w, err.Error(), http.StatusBadRequest)
35 36 37 38 39 40 41 42 43
				return
			}
			log.Infof("Setting MutexProfileFraction to %d", fr)
			runtime.SetMutexProfileFraction(fr)
		})

		return mux, nil
	}
}