Handle failures with goto instead of break. Update error logging.

This commit is contained in:
Jonathan Moody 2022-09-28 11:26:30 -05:00
parent c42a4689cd
commit e56edf0c9a
2 changed files with 32 additions and 15 deletions

View file

@ -55,17 +55,17 @@ 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 != 0 { if 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)
break goto fail1
} }
listener, err := net.ListenTCP("tcp", laddr) listener, err := net.ListenTCP("tcp", laddr)
if err != nil { if err != nil {
log.Errorf("ListenTCP: %v\n", err) log.Errorf("ListenTCP: %v\n", err)
break goto fail1
} }
log.Infof("JSONRPC server listening on %s", listener.Addr().String()) log.Infof("JSONRPC server listening on %s", listener.Addr().String())
acceptConnections := func(listener net.Listener) { acceptConnections := func(listener net.Listener) {
@ -80,11 +80,11 @@ func (s *Server) StartJsonRPC() error {
} }
} }
go acceptConnections(listener) go acceptConnections(listener)
break
} }
fail1:
// Set up the JSONRPC over HTTP server. // Set up the JSONRPC over HTTP server.
for s.Args.JSONRPCHTTPPort != 0 { if 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")
@ -93,22 +93,31 @@ func (s *Server) StartJsonRPC() error {
claimtrieSvc := &ClaimtrieService{s.DB} claimtrieSvc := &ClaimtrieService{s.DB}
err := s1.RegisterTCPService(claimtrieSvc, "blockchain_claimtrie") err := s1.RegisterTCPService(claimtrieSvc, "blockchain_claimtrie")
if err != nil { if err != nil {
log.Errorf("RegisterService: %v\n", err) log.Errorf("RegisterTCPService: %v\n", err)
goto fail2
} }
// Register other "blockchain.{block,address,scripthash}.*" handlers. // Register other "blockchain.{block,address,scripthash}.*" handlers.
blockchainSvc := &BlockchainBlockService{s.DB, s.Chain} blockchainSvc := &BlockchainBlockService{s.DB, s.Chain}
err = s1.RegisterTCPService(blockchainSvc, "blockchain_block") err = s1.RegisterTCPService(blockchainSvc, "blockchain_block")
if err != nil { if err != nil {
log.Errorf("RegisterService: %v\n", err) log.Errorf("RegisterTCPService: %v\n", err)
goto fail2
}
err = s1.RegisterTCPService(&BlockchainHeadersService{s.DB, s.Chain, nil, nil}, "blockchain_headers")
if err != nil {
log.Errorf("RegisterTCPService: %v\n", err)
goto fail2
} }
err = s1.RegisterTCPService(&BlockchainAddressService{s.DB, s.Chain, nil, nil}, "blockchain_address") err = s1.RegisterTCPService(&BlockchainAddressService{s.DB, s.Chain, nil, nil}, "blockchain_address")
if err != nil { if err != nil {
log.Errorf("RegisterService: %v\n", err) log.Errorf("RegisterTCPService: %v\n", err)
goto fail2
} }
err = s1.RegisterTCPService(&BlockchainScripthashService{s.DB, s.Chain, nil, nil}, "blockchain_scripthash") err = s1.RegisterTCPService(&BlockchainScripthashService{s.DB, s.Chain, nil, nil}, "blockchain_scripthash")
if err != nil { if err != nil {
log.Errorf("RegisterService: %v\n", err) log.Errorf("RegisterTCPService: %v\n", err)
goto fail2
} }
r := gorilla_mux.NewRouter() r := gorilla_mux.NewRouter()
@ -116,8 +125,8 @@ func (s *Server) StartJsonRPC() error {
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.Infof("HTTP JSONRPC server listening on %s", port)
log.Fatal(http.ListenAndServe(port, r)) log.Fatal(http.ListenAndServe(port, r))
break
} }
fail2:
return nil return nil
} }

View file

@ -201,22 +201,30 @@ func (sm *sessionManager) addSession(conn net.Conn) {
blockchainSvc := &BlockchainBlockService{sm.db, sm.chain} blockchainSvc := &BlockchainBlockService{sm.db, sm.chain}
err = s1.RegisterName("blockchain.block", blockchainSvc) err = s1.RegisterName("blockchain.block", blockchainSvc)
if err != nil { if err != nil {
log.Errorf("RegisterService: %v\n", err) log.Errorf("RegisterName: %v\n", err)
goto fail
} }
err = s1.RegisterName("blockchain.headers", &BlockchainHeadersService{sm.db, sm.chain, sm, sess}) err = s1.RegisterName("blockchain.headers", &BlockchainHeadersService{sm.db, sm.chain, sm, sess})
if err != nil { if err != nil {
log.Errorf("RegisterService: %v\n", err) log.Errorf("RegisterName: %v\n", err)
goto fail
} }
err = s1.RegisterName("blockchain.address", &BlockchainAddressService{sm.db, sm.chain, sm, sess}) err = s1.RegisterName("blockchain.address", &BlockchainAddressService{sm.db, sm.chain, sm, sess})
if err != nil { if err != nil {
log.Errorf("RegisterService: %v\n", err) log.Errorf("RegisterName: %v\n", err)
goto fail
} }
err = s1.RegisterName("blockchain.scripthash", &BlockchainScripthashService{sm.db, sm.chain, sm, sess}) err = s1.RegisterName("blockchain.scripthash", &BlockchainScripthashService{sm.db, sm.chain, sm, sess})
if err != nil { if err != nil {
log.Errorf("RegisterService: %v\n", err) log.Errorf("RegisterName: %v\n", err)
goto fail
} }
go s1.ServeCodec(&SessionServerCodec{jsonrpc.NewServerCodec(conn), sess}) go s1.ServeCodec(&SessionServerCodec{jsonrpc.NewServerCodec(conn), sess})
return
fail:
sm.removeSession(sess)
} }
func (sm *sessionManager) removeSession(sess *session) { func (sm *sessionManager) removeSession(sess *session) {