From e56edf0c9a8242e8527ef218315b1ad8cc6a19f5 Mon Sep 17 00:00:00 2001 From: Jonathan Moody <103143855+moodyjon@users.noreply.github.com> Date: Wed, 28 Sep 2022 11:26:30 -0500 Subject: [PATCH] Handle failures with goto instead of break. Update error logging. --- server/jsonrpc_service.go | 31 ++++++++++++++++++++----------- server/session.go | 16 ++++++++++++---- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/server/jsonrpc_service.go b/server/jsonrpc_service.go index 9c29ef4..ee73de7 100644 --- a/server/jsonrpc_service.go +++ b/server/jsonrpc_service.go @@ -55,17 +55,17 @@ func (s *Server) StartJsonRPC() error { defer s.sessionManager.stop() // 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) laddr, err := net.ResolveTCPAddr("tcp", port) if err != nil { log.Errorf("ResoveIPAddr: %v\n", err) - break + goto fail1 } listener, err := net.ListenTCP("tcp", laddr) if err != nil { log.Errorf("ListenTCP: %v\n", err) - break + goto fail1 } log.Infof("JSONRPC server listening on %s", listener.Addr().String()) acceptConnections := func(listener net.Listener) { @@ -80,11 +80,11 @@ func (s *Server) StartJsonRPC() error { } } go acceptConnections(listener) - break } +fail1: // 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 // Register the type of data requested as JSON, with custom codec. s1.RegisterCodec(&gorillaRpcCodec{gorilla_json.NewCodec()}, "application/json") @@ -93,22 +93,31 @@ func (s *Server) StartJsonRPC() error { claimtrieSvc := &ClaimtrieService{s.DB} err := s1.RegisterTCPService(claimtrieSvc, "blockchain_claimtrie") if err != nil { - log.Errorf("RegisterService: %v\n", err) + log.Errorf("RegisterTCPService: %v\n", err) + goto fail2 } // Register other "blockchain.{block,address,scripthash}.*" handlers. blockchainSvc := &BlockchainBlockService{s.DB, s.Chain} err = s1.RegisterTCPService(blockchainSvc, "blockchain_block") if err != nil { - log.Errorf("RegisterService: %v\n", err) + log.Errorf("RegisterTCPService: %v\n", err) + goto fail2 } - err = s1.RegisterTCPService(&BlockchainAddressService{s.DB, s.Chain, nil, nil}, "blockchain_address") + err = s1.RegisterTCPService(&BlockchainHeadersService{s.DB, s.Chain, nil, nil}, "blockchain_headers") if err != nil { - log.Errorf("RegisterService: %v\n", err) + log.Errorf("RegisterTCPService: %v\n", err) + goto fail2 + } + err = s1.RegisterTCPService(&BlockchainAddressService{s.DB, s.Chain, nil, nil}, "blockchain_address") + if err != nil { + log.Errorf("RegisterTCPService: %v\n", err) + goto fail2 } err = s1.RegisterTCPService(&BlockchainScripthashService{s.DB, s.Chain, nil, nil}, "blockchain_scripthash") if err != nil { - log.Errorf("RegisterService: %v\n", err) + log.Errorf("RegisterTCPService: %v\n", err) + goto fail2 } r := gorilla_mux.NewRouter() @@ -116,8 +125,8 @@ func (s *Server) StartJsonRPC() error { port := ":" + strconv.FormatUint(uint64(s.Args.JSONRPCHTTPPort), 10) log.Infof("HTTP JSONRPC server listening on %s", port) log.Fatal(http.ListenAndServe(port, r)) - break } +fail2: return nil } diff --git a/server/session.go b/server/session.go index 4b95ae4..180c45c 100644 --- a/server/session.go +++ b/server/session.go @@ -201,22 +201,30 @@ func (sm *sessionManager) addSession(conn net.Conn) { blockchainSvc := &BlockchainBlockService{sm.db, sm.chain} err = s1.RegisterName("blockchain.block", blockchainSvc) 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}) 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}) 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}) 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}) + return + +fail: + sm.removeSession(sess) } func (sm *sessionManager) removeSession(sess *session) {