added some code to return statistics
This commit is contained in:
		| @ -3,6 +3,7 @@ package hodu | |||||||
| import "encoding/json" | import "encoding/json" | ||||||
| import "net/http" | import "net/http" | ||||||
| import "net/url" | import "net/url" | ||||||
|  | import "runtime" | ||||||
| import "strconv" | import "strconv" | ||||||
|  |  | ||||||
| /* | /* | ||||||
| @ -65,6 +66,11 @@ type json_out_client_peer struct { | |||||||
| 	ServerPeerAddr string `json:"server-peer-addr"` | 	ServerPeerAddr string `json:"server-peer-addr"` | ||||||
| 	ServerLocalAddr string `json:"server-local-addr"` | 	ServerLocalAddr string `json:"server-local-addr"` | ||||||
| } | } | ||||||
|  |  | ||||||
|  | type json_out_client_stats struct { | ||||||
|  | 	NumCPUs int `json:"num-cpus"` | ||||||
|  | 	NumGoroutines int `json:"num-goroutines"` | ||||||
|  | } | ||||||
| // ------------------------------------ | // ------------------------------------ | ||||||
|  |  | ||||||
| type client_ctl_client_conns struct { | type client_ctl_client_conns struct { | ||||||
| @ -91,6 +97,9 @@ type client_ctl_client_conns_id_routes_id_peers_id struct { | |||||||
| 	c *Client | 	c *Client | ||||||
| } | } | ||||||
|  |  | ||||||
|  | type client_ctl_stats struct { | ||||||
|  | 	c *Client | ||||||
|  | } | ||||||
| // ------------------------------------ | // ------------------------------------ | ||||||
|  |  | ||||||
| func (ctl *client_ctl_client_conns) ServeHTTP(w http.ResponseWriter, req *http.Request) { | func (ctl *client_ctl_client_conns) ServeHTTP(w http.ResponseWriter, req *http.Request) { | ||||||
| @ -622,3 +631,40 @@ oops: | |||||||
| 	c.log.Write("", LOG_ERROR, "[%s] %s %s - %s", req.RemoteAddr, req.Method, req.URL.String(), err.Error()) | 	c.log.Write("", LOG_ERROR, "[%s] %s %s - %s", req.RemoteAddr, req.Method, req.URL.String(), err.Error()) | ||||||
| 	return | 	return | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // ------------------------------------ | ||||||
|  |  | ||||||
|  | func (ctl *client_ctl_stats) ServeHTTP(w http.ResponseWriter, req *http.Request) { | ||||||
|  | 	var c *Client | ||||||
|  | 	var status_code int | ||||||
|  | 	var err error | ||||||
|  | 	var je *json.Encoder | ||||||
|  |  | ||||||
|  | 	defer func() { | ||||||
|  | 		var err interface{} = recover() | ||||||
|  | 		if err != nil { dump_call_frame_and_exit(ctl.c.log, req, err) } | ||||||
|  | 	}() | ||||||
|  |  | ||||||
|  | 	c = ctl.c | ||||||
|  | 	je = json.NewEncoder(w) | ||||||
|  |  | ||||||
|  | 	switch req.Method { | ||||||
|  | 		case http.MethodGet: | ||||||
|  | 			var stats json_out_client_stats | ||||||
|  | 			stats.NumCPUs = runtime.NumCPU() | ||||||
|  | 			stats.NumGoroutines = runtime.NumGoroutine() | ||||||
|  | 			status_code = http.StatusOK; w.WriteHeader(status_code) | ||||||
|  | 			if err = je.Encode(stats); err != nil { goto oops } | ||||||
|  |  | ||||||
|  | 		default: | ||||||
|  | 			status_code = http.StatusBadRequest; w.WriteHeader(status_code) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | //done: | ||||||
|  | 	c.log.Write("", LOG_DEBUG, "[%s] %s %s %d", req.RemoteAddr, req.Method, req.URL.String(), status_code) // TODO: time taken | ||||||
|  | 	return | ||||||
|  |  | ||||||
|  | oops: | ||||||
|  | 	c.log.Write("", LOG_ERROR, "[%s] %s %s - %s", req.RemoteAddr, req.Method, req.URL.String(), err.Error()) | ||||||
|  | 	return | ||||||
|  | } | ||||||
|  | |||||||
| @ -1005,6 +1005,7 @@ func NewClient(ctx context.Context, ctl_addrs []string, logger Logger, ctltlscfg | |||||||
| 	c.ctl_mux.Handle(c.ctl_prefix + "/client-conns/{conn_id}/routes/{route_id}", &client_ctl_client_conns_id_routes_id{c: &c}) | 	c.ctl_mux.Handle(c.ctl_prefix + "/client-conns/{conn_id}/routes/{route_id}", &client_ctl_client_conns_id_routes_id{c: &c}) | ||||||
| 	c.ctl_mux.Handle(c.ctl_prefix + "/client-conns/{conn_id}/routes/{route_id}/peers", &client_ctl_client_conns_id_routes_id_peers{c: &c}) | 	c.ctl_mux.Handle(c.ctl_prefix + "/client-conns/{conn_id}/routes/{route_id}/peers", &client_ctl_client_conns_id_routes_id_peers{c: &c}) | ||||||
| 	c.ctl_mux.Handle(c.ctl_prefix + "/client-conns/{conn_id}/routes/{route_id}/peers/{peer_id}", &client_ctl_client_conns_id_routes_id_peers_id{c: &c}) | 	c.ctl_mux.Handle(c.ctl_prefix + "/client-conns/{conn_id}/routes/{route_id}/peers/{peer_id}", &client_ctl_client_conns_id_routes_id_peers_id{c: &c}) | ||||||
|  | 	c.ctl_mux.Handle(c.ctl_prefix + "/stats", &client_ctl_stats{c: &c}) | ||||||
|  |  | ||||||
| 	c.ctl_addr = make([]string, len(ctl_addrs)) | 	c.ctl_addr = make([]string, len(ctl_addrs)) | ||||||
| 	c.ctl = make([]*http.Server, len(ctl_addrs)) | 	c.ctl = make([]*http.Server, len(ctl_addrs)) | ||||||
|  | |||||||
| @ -20,7 +20,7 @@ type json_out_server_route struct { | |||||||
| 	ServerPeerProto ROUTE_PROTO `json:"server-peer-proto"` | 	ServerPeerProto ROUTE_PROTO `json:"server-peer-proto"` | ||||||
| } | } | ||||||
|  |  | ||||||
| type json_out_stats struct { | type json_out_server_stats struct { | ||||||
| 	NumCPUs int `json:"num-cpus"` | 	NumCPUs int `json:"num-cpus"` | ||||||
| 	NumGoroutines int `json:"num-goroutines"` | 	NumGoroutines int `json:"num-goroutines"` | ||||||
| } | } | ||||||
| @ -43,7 +43,7 @@ type server_ctl_server_conns_id_routes_id struct { | |||||||
| 	s *Server | 	s *Server | ||||||
| } | } | ||||||
|  |  | ||||||
| type server_ctl_server_stats struct { | type server_ctl_stats struct { | ||||||
| 	s *Server | 	s *Server | ||||||
| } | } | ||||||
|  |  | ||||||
| @ -215,7 +215,7 @@ func (ctl *server_ctl_server_conns_id_routes_id) ServeHTTP(w http.ResponseWriter | |||||||
|  |  | ||||||
| // ------------------------------------ | // ------------------------------------ | ||||||
|  |  | ||||||
| func (ctl *server_ctl_server_stats) ServeHTTP(w http.ResponseWriter, req *http.Request) { | func (ctl *server_ctl_stats) ServeHTTP(w http.ResponseWriter, req *http.Request) { | ||||||
| 	var s *Server | 	var s *Server | ||||||
| 	var status_code int | 	var status_code int | ||||||
| 	var err error | 	var err error | ||||||
| @ -231,7 +231,7 @@ func (ctl *server_ctl_server_stats) ServeHTTP(w http.ResponseWriter, req *http.R | |||||||
|  |  | ||||||
| 	switch req.Method { | 	switch req.Method { | ||||||
| 		case http.MethodGet: | 		case http.MethodGet: | ||||||
| 			var stats json_out_stats | 			var stats json_out_server_stats | ||||||
| 			stats.NumCPUs = runtime.NumCPU() | 			stats.NumCPUs = runtime.NumCPU() | ||||||
| 			stats.NumGoroutines = runtime.NumGoroutine() | 			stats.NumGoroutines = runtime.NumGoroutine() | ||||||
| 			status_code = http.StatusOK; w.WriteHeader(status_code) | 			status_code = http.StatusOK; w.WriteHeader(status_code) | ||||||
|  | |||||||
| @ -881,7 +881,7 @@ func NewServer(ctx context.Context, ctl_addrs []string, rpc_addrs []string, logg | |||||||
| 	s.ctl_mux.Handle(s.ctl_prefix + "/server-conns/{conn_id}", &server_ctl_server_conns_id{s: &s}) | 	s.ctl_mux.Handle(s.ctl_prefix + "/server-conns/{conn_id}", &server_ctl_server_conns_id{s: &s}) | ||||||
| 	s.ctl_mux.Handle(s.ctl_prefix + "/server-conns/{conn_id}/routes", &server_ctl_server_conns_id_routes{s: &s}) | 	s.ctl_mux.Handle(s.ctl_prefix + "/server-conns/{conn_id}/routes", &server_ctl_server_conns_id_routes{s: &s}) | ||||||
| 	s.ctl_mux.Handle(s.ctl_prefix + "/server-conns/{conn_id}/routes/{route_id}", &server_ctl_server_conns_id_routes_id{s: &s}) | 	s.ctl_mux.Handle(s.ctl_prefix + "/server-conns/{conn_id}/routes/{route_id}", &server_ctl_server_conns_id_routes_id{s: &s}) | ||||||
| 	s.ctl_mux.Handle(s.ctl_prefix + "/stats", &server_ctl_server_stats{s: &s}) | 	s.ctl_mux.Handle(s.ctl_prefix + "/stats", &server_ctl_stats{s: &s}) | ||||||
|  |  | ||||||
| 	s.ctl_addr = make([]string, len(ctl_addrs)) | 	s.ctl_addr = make([]string, len(ctl_addrs)) | ||||||
| 	s.ctl = make([]*http.Server, len(ctl_addrs)) | 	s.ctl = make([]*http.Server, len(ctl_addrs)) | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user