added the routes parameter to the server-conns endpoint

This commit is contained in:
hyung-hwan 2025-03-18 23:37:46 +09:00
parent 3714138656
commit 5fa6cd466b
3 changed files with 62 additions and 34 deletions

View File

@ -1924,6 +1924,7 @@ func (c *Client) FixServices() {
func (c *Client) WaitForTermination() { func (c *Client) WaitForTermination() {
c.wg.Wait() c.wg.Wait()
c.log.Write("", LOG_INFO, "End of service")
} }
func (c *Client) WriteLog(id string, level LogLevel, fmtstr string, args ...interface{}) { func (c *Client) WriteLog(id string, level LogLevel, fmtstr string, args ...interface{}) {

View File

@ -3,6 +3,9 @@ package hodu
import "encoding/json" import "encoding/json"
import "fmt" import "fmt"
import "net/http" import "net/http"
import "net/url"
import "strconv"
import "strings"
import "sync" import "sync"
import "time" import "time"
@ -24,7 +27,7 @@ type json_out_server_conn struct {
ClientAddr string `json:"client-addr"` ClientAddr string `json:"client-addr"`
ClientToken string `json:"client-token"` ClientToken string `json:"client-token"`
CreatedMilli int64 `json:"created-milli"` CreatedMilli int64 `json:"created-milli"`
Routes []json_out_server_route `json:"routes"` Routes []json_out_server_route `json:"routes,omitempty"`
} }
type json_out_server_route struct { type json_out_server_route struct {
@ -184,13 +187,20 @@ oops:
func (ctl *server_ctl_server_conns) ServeHTTP(w http.ResponseWriter, req *http.Request) (int, error) { func (ctl *server_ctl_server_conns) ServeHTTP(w http.ResponseWriter, req *http.Request) (int, error) {
var s *Server var s *Server
var q url.Values
var status_code int var status_code int
var je *json.Encoder var je *json.Encoder
var routes bool
var err error var err error
s = ctl.s s = ctl.s
je = json.NewEncoder(w) je = json.NewEncoder(w)
q = req.URL.Query()
q = req.URL.Query()
routes, err = strconv.ParseBool(strings.ToLower(q.Get("routes")))
if err != nil { routes = false }
switch req.Method { switch req.Method {
case http.MethodGet: case http.MethodGet:
var js []json_out_server_conn var js []json_out_server_conn
@ -204,6 +214,7 @@ func (ctl *server_ctl_server_conns) ServeHTTP(w http.ResponseWriter, req *http.R
var ri RouteId var ri RouteId
cts = s.cts_map[ci] cts = s.cts_map[ci]
if routes {
jsp = make([]json_out_server_route, 0) jsp = make([]json_out_server_route, 0)
cts.route_mtx.Lock() cts.route_mtx.Lock()
for _, ri = range cts.route_map.get_sorted_keys() { for _, ri = range cts.route_map.get_sorted_keys() {
@ -220,6 +231,8 @@ func (ctl *server_ctl_server_conns) ServeHTTP(w http.ResponseWriter, req *http.R
}) })
} }
cts.route_mtx.Unlock() cts.route_mtx.Unlock()
}
js = append(js, json_out_server_conn{ js = append(js, json_out_server_conn{
Id: cts.Id, Id: cts.Id,
ClientAddr: cts.RemoteAddr.String(), ClientAddr: cts.RemoteAddr.String(),
@ -253,15 +266,21 @@ oops:
func (ctl *server_ctl_server_conns_id) ServeHTTP(w http.ResponseWriter, req *http.Request) (int, error) { func (ctl *server_ctl_server_conns_id) ServeHTTP(w http.ResponseWriter, req *http.Request) (int, error) {
var s *Server var s *Server
var q url.Values
var status_code int var status_code int
var err error
var je *json.Encoder var je *json.Encoder
var conn_id string var conn_id string
var cts *ServerConn var cts *ServerConn
var routes bool
var err error
s = ctl.s s = ctl.s
je = json.NewEncoder(w) je = json.NewEncoder(w)
q = req.URL.Query()
routes, err = strconv.ParseBool(strings.ToLower(q.Get("routes")))
if err != nil { routes = false }
conn_id = req.PathValue("conn_id") conn_id = req.PathValue("conn_id")
cts, err = s.FindServerConnByIdStr(conn_id) cts, err = s.FindServerConnByIdStr(conn_id)
if err != nil { if err != nil {
@ -270,12 +289,14 @@ func (ctl *server_ctl_server_conns_id) ServeHTTP(w http.ResponseWriter, req *htt
goto oops goto oops
} }
switch req.Method { switch req.Method {
case http.MethodGet: case http.MethodGet:
var jsp []json_out_server_route var jsp []json_out_server_route
var js *json_out_server_conn var js *json_out_server_conn
var ri RouteId var ri RouteId
if routes {
jsp = make([]json_out_server_route, 0) jsp = make([]json_out_server_route, 0)
cts.route_mtx.Lock() cts.route_mtx.Lock()
for _, ri = range cts.route_map.get_sorted_keys() { for _, ri = range cts.route_map.get_sorted_keys() {
@ -293,6 +314,7 @@ func (ctl *server_ctl_server_conns_id) ServeHTTP(w http.ResponseWriter, req *htt
}) })
} }
cts.route_mtx.Unlock() cts.route_mtx.Unlock()
}
js = &json_out_server_conn{ js = &json_out_server_conn{
Id: cts.Id, Id: cts.Id,
ClientAddr: cts.RemoteAddr.String(), ClientAddr: cts.RemoteAddr.String(),

View File

@ -1024,6 +1024,10 @@ func (s *Server) PacketStream(strm Hodu_PacketStreamServer) error {
return fmt.Errorf("failed to get peer from packet stream context") return fmt.Errorf("failed to get peer from packet stream context")
} }
if s.stop_req.Load() {
return fmt.Errorf("new conneciton prohibited after stop - %s", p.Addr.String())
}
cts, err = s.AddNewServerConn(&p.Addr, &p.LocalAddr, strm) cts, err = s.AddNewServerConn(&p.Addr, &p.LocalAddr, strm)
if err != nil { if err != nil {
return fmt.Errorf("unable to add client %s - %s", p.Addr.String(), err.Error()) return fmt.Errorf("unable to add client %s - %s", p.Addr.String(), err.Error())
@ -1042,7 +1046,7 @@ func (s *Server) PacketStream(strm Hodu_PacketStreamServer) error {
}, },
) )
// Don't detached the cts task as a go-routine as this function // Don't detach the cts task as a go-routine as this function
// is invoked as a go-routine by the grpc server. // is invoked as a go-routine by the grpc server.
s.cts_wg.Add(1) s.cts_wg.Add(1)
cts.RunTask(&s.cts_wg) cts.RunTask(&s.cts_wg)
@ -1553,7 +1557,7 @@ func (s *Server) RunTask(wg *sync.WaitGroup) {
go s.run_grpc_server(idx, &s.rpc_wg) go s.run_grpc_server(idx, &s.rpc_wg)
} }
// most the work is done by in separate goroutines (s.run_grp_server) // most work is done by in separate goroutines (s.run_grp_server)
// this loop serves as a placeholder to prevent the logic flow from // this loop serves as a placeholder to prevent the logic flow from
// descening down to s.ReqStop() // descening down to s.ReqStop()
task_loop: task_loop:
@ -2117,6 +2121,7 @@ func (s *Server) FixServices() {
func (s *Server) WaitForTermination() { func (s *Server) WaitForTermination() {
s.wg.Wait() s.wg.Wait()
s.log.Write("", LOG_INFO, "End of service")
} }
func (s *Server) WriteLog(id string, level LogLevel, fmtstr string, args ...interface{}) { func (s *Server) WriteLog(id string, level LogLevel, fmtstr string, args ...interface{}) {