aboutsummaryrefslogtreecommitdiffstats
path: root/rpc/http.go
diff options
context:
space:
mode:
Diffstat (limited to 'rpc/http.go')
-rw-r--r--rpc/http.go25
1 files changed, 21 insertions, 4 deletions
diff --git a/rpc/http.go b/rpc/http.go
index 790442a28..f9c646908 100644
--- a/rpc/http.go
+++ b/rpc/http.go
@@ -5,7 +5,6 @@ import (
"fmt"
"io"
"io/ioutil"
- "net"
"net/http"
"github.com/ethereum/go-ethereum/logger"
@@ -15,6 +14,7 @@ import (
)
var rpclogger = logger.NewLogger("RPC")
+var rpclistener *stoppableTCPListener
const (
jsonrpcver = "2.0"
@@ -22,11 +22,19 @@ const (
)
func Start(pipe *xeth.XEth, config RpcConfig) error {
- l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", config.ListenAddress, config.ListenPort))
+ if rpclistener != nil {
+ if fmt.Sprintf("%s:%d", config.ListenAddress, config.ListenPort) != rpclistener.Addr().String() {
+ return fmt.Errorf("RPC service already running on %s ", rpclistener.Addr().String())
+ }
+ return nil // RPC service already running on given host/port
+ }
+
+ l, err := newStoppableTCPListener(fmt.Sprintf("%s:%d", config.ListenAddress, config.ListenPort))
if err != nil {
rpclogger.Errorf("Can't listen on %s:%d: %v", config.ListenAddress, config.ListenPort, err)
return err
}
+ rpclistener = l
var handler http.Handler
if len(config.CorsDomain) > 0 {
@@ -35,9 +43,9 @@ func Start(pipe *xeth.XEth, config RpcConfig) error {
opts.AllowedOrigins = []string{config.CorsDomain}
c := cors.New(opts)
- handler = c.Handler(JSONRPC(pipe))
+ handler = newStoppableHandler(c.Handler(JSONRPC(pipe)), l.stop)
} else {
- handler = JSONRPC(pipe)
+ handler = newStoppableHandler(JSONRPC(pipe), l.stop)
}
go http.Serve(l, handler)
@@ -45,6 +53,15 @@ func Start(pipe *xeth.XEth, config RpcConfig) error {
return nil
}
+func Stop() error {
+ if rpclistener != nil {
+ rpclistener.Stop()
+ rpclistener = nil
+ }
+
+ return nil
+}
+
// JSONRPC returns a handler that implements the Ethereum JSON-RPC API.
func JSONRPC(pipe *xeth.XEth) http.Handler {
api := NewEthereumApi(pipe)