Only assign default port (50001) if neither --json-rpc-port nor

--json-rpc-http-port are specified.
This commit is contained in:
Jonathan Moody 2022-09-27 20:00:42 -05:00
parent 1d227dbca8
commit c42a4689cd
3 changed files with 17 additions and 11 deletions

View file

@ -28,8 +28,8 @@ type Args struct {
EsPort string EsPort string
PrometheusPort string PrometheusPort string
NotifierPort string NotifierPort string
JSONRPCPort *int JSONRPCPort int
JSONRPCHTTPPort *int JSONRPCHTTPPort int
EsIndex string EsIndex string
RefreshDelta int RefreshDelta int
CacheTTL int CacheTTL int
@ -127,7 +127,7 @@ func ParseArgs(searchRequest *pb.SearchRequest) *Args {
esPort := parser.String("", "esport", &argparse.Options{Required: false, Help: "elasticsearch port", Default: DefaultEsPort}) esPort := parser.String("", "esport", &argparse.Options{Required: false, Help: "elasticsearch port", Default: DefaultEsPort})
prometheusPort := parser.String("", "prometheus-port", &argparse.Options{Required: false, Help: "prometheus port", Default: DefaultPrometheusPort}) prometheusPort := parser.String("", "prometheus-port", &argparse.Options{Required: false, Help: "prometheus port", Default: DefaultPrometheusPort})
notifierPort := parser.String("", "notifier-port", &argparse.Options{Required: false, Help: "notifier port", Default: DefaultNotifierPort}) notifierPort := parser.String("", "notifier-port", &argparse.Options{Required: false, Help: "notifier port", Default: DefaultNotifierPort})
jsonRPCPort := parser.Int("", "json-rpc-port", &argparse.Options{Required: false, Help: "JSON RPC port", Validate: validatePort, Default: DefaultJSONRPCPort}) jsonRPCPort := parser.Int("", "json-rpc-port", &argparse.Options{Required: false, Help: "JSON RPC port", Validate: validatePort})
jsonRPCHTTPPort := parser.Int("", "json-rpc-http-port", &argparse.Options{Required: false, Help: "JSON RPC over HTTP port", Validate: validatePort}) jsonRPCHTTPPort := parser.Int("", "json-rpc-http-port", &argparse.Options{Required: false, Help: "JSON RPC over HTTP port", Validate: validatePort})
esIndex := parser.String("", "esindex", &argparse.Options{Required: false, Help: "elasticsearch index name", Default: DefaultEsIndex}) esIndex := parser.String("", "esindex", &argparse.Options{Required: false, Help: "elasticsearch index name", Default: DefaultEsIndex})
refreshDelta := parser.Int("", "refresh-delta", &argparse.Options{Required: false, Help: "elasticsearch index refresh delta in seconds", Default: DefaultRefreshDelta}) refreshDelta := parser.Int("", "refresh-delta", &argparse.Options{Required: false, Help: "elasticsearch index refresh delta in seconds", Default: DefaultRefreshDelta})
@ -166,6 +166,11 @@ func ParseArgs(searchRequest *pb.SearchRequest) *Args {
log.Fatalln(parser.Usage(err)) log.Fatalln(parser.Usage(err))
} }
// Use default JSON RPC port only if *neither* JSON RPC arg is specified.
if *jsonRPCPort == 0 && *jsonRPCHTTPPort == 0 {
*jsonRPCPort = DefaultJSONRPCPort
}
args := &Args{ args := &Args{
CmdType: SearchCmd, CmdType: SearchCmd,
Host: *host, Host: *host,
@ -176,8 +181,8 @@ func ParseArgs(searchRequest *pb.SearchRequest) *Args {
EsPort: *esPort, EsPort: *esPort,
PrometheusPort: *prometheusPort, PrometheusPort: *prometheusPort,
NotifierPort: *notifierPort, NotifierPort: *notifierPort,
JSONRPCPort: jsonRPCPort, JSONRPCPort: *jsonRPCPort,
JSONRPCHTTPPort: jsonRPCHTTPPort, JSONRPCHTTPPort: *jsonRPCHTTPPort,
EsIndex: *esIndex, EsIndex: *esIndex,
RefreshDelta: *refreshDelta, RefreshDelta: *refreshDelta,
CacheTTL: *cacheTTL, CacheTTL: *cacheTTL,

View file

@ -46,7 +46,6 @@ func removeFile(fileName string) {
// makeDefaultArgs creates a default set of arguments for testing the server. // makeDefaultArgs creates a default set of arguments for testing the server.
func makeDefaultArgs() *server.Args { func makeDefaultArgs() *server.Args {
port := server.DefaultJSONRPCPort
args := &server.Args{ args := &server.Args{
CmdType: server.ServeCmd, CmdType: server.ServeCmd,
Host: server.DefaultHost, Host: server.DefaultHost,
@ -56,7 +55,7 @@ func makeDefaultArgs() *server.Args {
EsPort: server.DefaultEsPort, EsPort: server.DefaultEsPort,
PrometheusPort: server.DefaultPrometheusPort, PrometheusPort: server.DefaultPrometheusPort,
NotifierPort: server.DefaultNotifierPort, NotifierPort: server.DefaultNotifierPort,
JSONRPCPort: &port, JSONRPCPort: server.DefaultJSONRPCPort,
EsIndex: server.DefaultEsIndex, EsIndex: server.DefaultEsIndex,
RefreshDelta: server.DefaultRefreshDelta, RefreshDelta: server.DefaultRefreshDelta,
CacheTTL: server.DefaultCacheTTL, CacheTTL: server.DefaultCacheTTL,

View file

@ -55,8 +55,8 @@ func (s *Server) StartJsonRPC() error {
defer s.sessionManager.stop() defer s.sessionManager.stop()
// Set up the pure JSONRPC server with persistent connections/sessions. // Set up the pure JSONRPC server with persistent connections/sessions.
for s.Args.JSONRPCPort != nil { for s.Args.JSONRPCPort != 0 {
port := ":" + strconv.FormatUint(uint64(*s.Args.JSONRPCPort), 10) port := ":" + strconv.FormatUint(uint64(s.Args.JSONRPCPort), 10)
laddr, err := net.ResolveTCPAddr("tcp", port) laddr, err := net.ResolveTCPAddr("tcp", port)
if err != nil { if err != nil {
log.Errorf("ResoveIPAddr: %v\n", err) log.Errorf("ResoveIPAddr: %v\n", err)
@ -67,6 +67,7 @@ func (s *Server) StartJsonRPC() error {
log.Errorf("ListenTCP: %v\n", err) log.Errorf("ListenTCP: %v\n", err)
break break
} }
log.Infof("JSONRPC server listening on %s", listener.Addr().String())
acceptConnections := func(listener net.Listener) { acceptConnections := func(listener net.Listener) {
for { for {
conn, err := listener.Accept() conn, err := listener.Accept()
@ -83,7 +84,7 @@ func (s *Server) StartJsonRPC() error {
} }
// Set up the JSONRPC over HTTP server. // Set up the JSONRPC over HTTP server.
for s.Args.JSONRPCHTTPPort != nil { for s.Args.JSONRPCHTTPPort != 0 {
s1 := gorilla_rpc.NewServer() // Create a new RPC server s1 := gorilla_rpc.NewServer() // Create a new RPC server
// Register the type of data requested as JSON, with custom codec. // Register the type of data requested as JSON, with custom codec.
s1.RegisterCodec(&gorillaRpcCodec{gorilla_json.NewCodec()}, "application/json") s1.RegisterCodec(&gorillaRpcCodec{gorilla_json.NewCodec()}, "application/json")
@ -112,7 +113,8 @@ func (s *Server) StartJsonRPC() error {
r := gorilla_mux.NewRouter() r := gorilla_mux.NewRouter()
r.Handle("/rpc", s1) r.Handle("/rpc", s1)
port := ":" + strconv.FormatUint(uint64(*s.Args.JSONRPCHTTPPort), 10) port := ":" + strconv.FormatUint(uint64(s.Args.JSONRPCHTTPPort), 10)
log.Infof("HTTP JSONRPC server listening on %s", port)
log.Fatal(http.ListenAndServe(port, r)) log.Fatal(http.ListenAndServe(port, r))
break break
} }