From 57f93d25bd9a09f4a68307342ad44a5af1d5dc26 Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Thu, 16 Apr 2015 12:56:51 +0200 Subject: [PATCH 1/4] admin.stopRPC support added which stops the RPC HTTP listener --- cmd/geth/admin.go | 8 ++++++ rpc/http.go | 21 +++++++++++++-- rpc/types.go | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 2 deletions(-) diff --git a/cmd/geth/admin.go b/cmd/geth/admin.go index f8c717187..3b37cba75 100644 --- a/cmd/geth/admin.go +++ b/cmd/geth/admin.go @@ -26,6 +26,7 @@ func (js *jsre) adminBindings() { admin := t.Object() admin.Set("suggestPeer", js.suggestPeer) admin.Set("startRPC", js.startRPC) + admin.Set("stopRPC", js.stopRPC) admin.Set("nodeInfo", js.nodeInfo) admin.Set("peers", js.peers) admin.Set("newAccount", js.newAccount) @@ -141,6 +142,13 @@ func (js *jsre) startRPC(call otto.FunctionCall) otto.Value { return otto.TrueValue() } +func (js *jsre) stopRPC(call otto.FunctionCall) otto.Value { + if rpc.Stop() == nil { + return otto.TrueValue() + } + return otto.FalseValue() +} + func (js *jsre) suggestPeer(call otto.FunctionCall) otto.Value { nodeURL, err := call.Argument(0).ToString() if err != nil { diff --git a/rpc/http.go b/rpc/http.go index 790442a28..61f8da549 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 *ControllableTCPListener const ( jsonrpcver = "2.0" @@ -22,11 +22,17 @@ 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 { // listener already running + glog.Infoln("RPC listener already running") + return fmt.Errorf("RPC already running on %s", rpclistener.Addr().String()) + } + + l, err := NewControllableTCPListener(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 { @@ -45,6 +51,17 @@ func Start(pipe *xeth.XEth, config RpcConfig) error { return nil } +func Stop() error { + if rpclistener == nil { // listener not running + glog.Infoln("RPC listener not running") + return 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) diff --git a/rpc/types.go b/rpc/types.go index bc9a46ed5..c7dc2cc9a 100644 --- a/rpc/types.go +++ b/rpc/types.go @@ -23,6 +23,10 @@ import ( "math/big" "strings" + "errors" + "net" + "time" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" ) @@ -257,3 +261,66 @@ type RpcErrorObject struct { Message string `json:"message"` // Data interface{} `json:"data"` } + +type ListenerStoppedError struct { + msg string +} + +func (self ListenerStoppedError) Timout() bool { + return false +} + +func (self ListenerStoppedError) Temporary() bool { + return false +} + +func (self ListenerStoppedError) Error() string { + return self.msg +} + +type ControllableTCPListener struct { + *net.TCPListener + stop chan struct{} +} + +var listenerStoppedError ListenerStoppedError + +func (self *ControllableTCPListener) Stop() { + close(self.stop) +} + +func (self *ControllableTCPListener) Accept() (net.Conn, error) { + for { + self.SetDeadline(time.Now().Add(time.Duration(500 * time.Millisecond))) + c, err := self.TCPListener.AcceptTCP() + + select { + case <-self.stop: + self.TCPListener.Close() + return nil, listenerStoppedError + default: // keep on going + } + + if err != nil { + if netErr, ok := err.(net.Error); ok && netErr.Timeout() && netErr.Temporary() { + continue // regular timeout + } + } + + return c, err + } +} + +func NewControllableTCPListener(addr string) (*ControllableTCPListener, error) { + wl, err := net.Listen("tcp", addr) + if err != nil { + return nil, err + } + + if tcpl, ok := wl.(*net.TCPListener); ok { + l := &ControllableTCPListener{tcpl, make(chan struct{})} + return l, nil + } + + return nil, errors.New("Unable to create TCP listener for RPC") +} From ead3dd9759c9cc8076ad716fe10cf641751b65b0 Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Thu, 16 Apr 2015 19:23:57 +0200 Subject: [PATCH 2/4] Stop accepted and alive connections (http keep-alive) when the rpc service is stopped --- rpc/http.go | 22 ++++++------- rpc/types.go | 91 ++++++++++++++++++++++++++++++++++------------------ 2 files changed, 71 insertions(+), 42 deletions(-) diff --git a/rpc/http.go b/rpc/http.go index 61f8da549..882aff7ea 100644 --- a/rpc/http.go +++ b/rpc/http.go @@ -14,7 +14,7 @@ import ( ) var rpclogger = logger.NewLogger("RPC") -var rpclistener *ControllableTCPListener +var rpclistener *StoppableTCPListener const ( jsonrpcver = "2.0" @@ -22,12 +22,14 @@ const ( ) func Start(pipe *xeth.XEth, config RpcConfig) error { - if rpclistener != nil { // listener already running - glog.Infoln("RPC listener already running") - return fmt.Errorf("RPC already running on %s", rpclistener.Addr().String()) + 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 := NewControllableTCPListener(fmt.Sprintf("%s:%d", config.ListenAddress, config.ListenPort)) + 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 @@ -41,7 +43,7 @@ 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) } @@ -52,13 +54,11 @@ func Start(pipe *xeth.XEth, config RpcConfig) error { } func Stop() error { - if rpclistener == nil { // listener not running - glog.Infoln("RPC listener not running") - return nil + if rpclistener != nil { + rpclistener.Stop() + rpclistener = nil } - rpclistener.Stop() - rpclistener = nil return nil } diff --git a/rpc/types.go b/rpc/types.go index c7dc2cc9a..b33621fef 100644 --- a/rpc/types.go +++ b/rpc/types.go @@ -25,8 +25,11 @@ import ( "errors" "net" + "net/http" "time" + "io" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" ) @@ -266,39 +269,64 @@ type ListenerStoppedError struct { msg string } -func (self ListenerStoppedError) Timout() bool { - return false -} - -func (self ListenerStoppedError) Temporary() bool { - return false -} - func (self ListenerStoppedError) Error() string { return self.msg } -type ControllableTCPListener struct { +var listenerStoppedError = ListenerStoppedError{"Listener stopped"} + +type StoppableTCPListener struct { *net.TCPListener - stop chan struct{} + stop *chan struct{} // closed when the listener must stop } -var listenerStoppedError ListenerStoppedError - -func (self *ControllableTCPListener) Stop() { - close(self.stop) +// Wraps the default handler and checks if the RPC service was stopped. In that case it returns an +// error indicating that the service was stopped. This will only happen for connections which are +// kept open (HTTP keep-alive) when the RPC service was shutdown. +func NewStoppableHandler(h http.Handler, stop *chan struct{}) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + select { + case <-*stop: + w.Header().Set("Content-Type", "application/json") + jsonerr := &RpcErrorObject{-32603, "RPC service stopt"} + send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr}) + default: + h.ServeHTTP(w, r) + } + }) } -func (self *ControllableTCPListener) Accept() (net.Conn, error) { +// Stop the listener and all accepted and still active connections. +func (self *StoppableTCPListener) Stop() { + close(*self.stop) +} + +func NewStoppableTCPListener(addr string) (*StoppableTCPListener, error) { + wl, err := net.Listen("tcp", addr) + if err != nil { + return nil, err + } + + if tcpl, ok := wl.(*net.TCPListener); ok { + stop := make(chan struct{}) + l := &StoppableTCPListener{tcpl, &stop} + return l, nil + } + + return nil, errors.New("Unable to create TCP listener for RPC service") +} + +func (self *StoppableTCPListener) Accept() (net.Conn, error) { for { - self.SetDeadline(time.Now().Add(time.Duration(500 * time.Millisecond))) + self.SetDeadline(time.Now().Add(time.Duration(1 * time.Second))) c, err := self.TCPListener.AcceptTCP() select { - case <-self.stop: + case <-*self.stop: + c.Close() self.TCPListener.Close() return nil, listenerStoppedError - default: // keep on going + default: } if err != nil { @@ -307,20 +335,21 @@ func (self *ControllableTCPListener) Accept() (net.Conn, error) { } } - return c, err + return &ClosableConnection{c, self.stop}, err } } -func NewControllableTCPListener(addr string) (*ControllableTCPListener, error) { - wl, err := net.Listen("tcp", addr) - if err != nil { - return nil, err - } - - if tcpl, ok := wl.(*net.TCPListener); ok { - l := &ControllableTCPListener{tcpl, make(chan struct{})} - return l, nil - } - - return nil, errors.New("Unable to create TCP listener for RPC") +type ClosableConnection struct { + *net.TCPConn + closed *chan struct{} +} + +func (self *ClosableConnection) Read(b []byte) (n int, err error) { + select { + case <-*self.closed: + self.TCPConn.Close() + return 0, io.EOF + default: + return self.TCPConn.Read(b) + } } From 2c229bac003b30908fd0dc0d69d35044c6cb2425 Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Sun, 19 Apr 2015 09:55:41 +0200 Subject: [PATCH 3/4] Replaced channel pointer field with non pointer channel --- rpc/http.go | 2 +- rpc/types.go | 23 +++++++++++++---------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/rpc/http.go b/rpc/http.go index 882aff7ea..5ff4f613b 100644 --- a/rpc/http.go +++ b/rpc/http.go @@ -45,7 +45,7 @@ func Start(pipe *xeth.XEth, config RpcConfig) error { c := cors.New(opts) handler = NewStoppableHandler(c.Handler(JSONRPC(pipe)), l.stop) } else { - handler = JSONRPC(pipe) + handler = NewStoppableHandler(JSONRPC(pipe), l.stop) } go http.Serve(l, handler) diff --git a/rpc/types.go b/rpc/types.go index b33621fef..71ed5df49 100644 --- a/rpc/types.go +++ b/rpc/types.go @@ -275,20 +275,21 @@ func (self ListenerStoppedError) Error() string { var listenerStoppedError = ListenerStoppedError{"Listener stopped"} +// When https://github.com/golang/go/issues/4674 is fixed this could be replaced type StoppableTCPListener struct { *net.TCPListener - stop *chan struct{} // closed when the listener must stop + stop chan struct{} // closed when the listener must stop } // Wraps the default handler and checks if the RPC service was stopped. In that case it returns an // error indicating that the service was stopped. This will only happen for connections which are // kept open (HTTP keep-alive) when the RPC service was shutdown. -func NewStoppableHandler(h http.Handler, stop *chan struct{}) http.Handler { +func NewStoppableHandler(h http.Handler, stop chan struct{}) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { select { - case <-*stop: + case <-stop: w.Header().Set("Content-Type", "application/json") - jsonerr := &RpcErrorObject{-32603, "RPC service stopt"} + jsonerr := &RpcErrorObject{-32603, "RPC service stopped"} send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr}) default: h.ServeHTTP(w, r) @@ -298,7 +299,7 @@ func NewStoppableHandler(h http.Handler, stop *chan struct{}) http.Handler { // Stop the listener and all accepted and still active connections. func (self *StoppableTCPListener) Stop() { - close(*self.stop) + close(self.stop) } func NewStoppableTCPListener(addr string) (*StoppableTCPListener, error) { @@ -309,7 +310,7 @@ func NewStoppableTCPListener(addr string) (*StoppableTCPListener, error) { if tcpl, ok := wl.(*net.TCPListener); ok { stop := make(chan struct{}) - l := &StoppableTCPListener{tcpl, &stop} + l := &StoppableTCPListener{tcpl, stop} return l, nil } @@ -322,8 +323,10 @@ func (self *StoppableTCPListener) Accept() (net.Conn, error) { c, err := self.TCPListener.AcceptTCP() select { - case <-*self.stop: - c.Close() + case <-self.stop: + if c != nil { // accept timeout + c.Close() + } self.TCPListener.Close() return nil, listenerStoppedError default: @@ -341,12 +344,12 @@ func (self *StoppableTCPListener) Accept() (net.Conn, error) { type ClosableConnection struct { *net.TCPConn - closed *chan struct{} + closed chan struct{} } func (self *ClosableConnection) Read(b []byte) (n int, err error) { select { - case <-*self.closed: + case <-self.closed: self.TCPConn.Close() return 0, io.EOF default: From 61885aa965282d5879b9c4fbb740e96e9b680558 Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Sun, 19 Apr 2015 10:01:50 +0200 Subject: [PATCH 4/4] Don't export types/functions --- rpc/http.go | 8 ++++---- rpc/types.go | 24 ++++++++++++------------ 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/rpc/http.go b/rpc/http.go index 5ff4f613b..f9c646908 100644 --- a/rpc/http.go +++ b/rpc/http.go @@ -14,7 +14,7 @@ import ( ) var rpclogger = logger.NewLogger("RPC") -var rpclistener *StoppableTCPListener +var rpclistener *stoppableTCPListener const ( jsonrpcver = "2.0" @@ -29,7 +29,7 @@ func Start(pipe *xeth.XEth, config RpcConfig) error { return nil // RPC service already running on given host/port } - l, err := NewStoppableTCPListener(fmt.Sprintf("%s:%d", config.ListenAddress, config.ListenPort)) + 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 @@ -43,9 +43,9 @@ func Start(pipe *xeth.XEth, config RpcConfig) error { opts.AllowedOrigins = []string{config.CorsDomain} c := cors.New(opts) - handler = NewStoppableHandler(c.Handler(JSONRPC(pipe)), l.stop) + handler = newStoppableHandler(c.Handler(JSONRPC(pipe)), l.stop) } else { - handler = NewStoppableHandler(JSONRPC(pipe), l.stop) + handler = newStoppableHandler(JSONRPC(pipe), l.stop) } go http.Serve(l, handler) diff --git a/rpc/types.go b/rpc/types.go index 71ed5df49..1784759a4 100644 --- a/rpc/types.go +++ b/rpc/types.go @@ -265,18 +265,18 @@ type RpcErrorObject struct { // Data interface{} `json:"data"` } -type ListenerStoppedError struct { +type listenerHasStoppedError struct { msg string } -func (self ListenerStoppedError) Error() string { +func (self listenerHasStoppedError) Error() string { return self.msg } -var listenerStoppedError = ListenerStoppedError{"Listener stopped"} +var listenerStoppedError = listenerHasStoppedError{"Listener stopped"} // When https://github.com/golang/go/issues/4674 is fixed this could be replaced -type StoppableTCPListener struct { +type stoppableTCPListener struct { *net.TCPListener stop chan struct{} // closed when the listener must stop } @@ -284,7 +284,7 @@ type StoppableTCPListener struct { // Wraps the default handler and checks if the RPC service was stopped. In that case it returns an // error indicating that the service was stopped. This will only happen for connections which are // kept open (HTTP keep-alive) when the RPC service was shutdown. -func NewStoppableHandler(h http.Handler, stop chan struct{}) http.Handler { +func newStoppableHandler(h http.Handler, stop chan struct{}) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { select { case <-stop: @@ -298,11 +298,11 @@ func NewStoppableHandler(h http.Handler, stop chan struct{}) http.Handler { } // Stop the listener and all accepted and still active connections. -func (self *StoppableTCPListener) Stop() { +func (self *stoppableTCPListener) Stop() { close(self.stop) } -func NewStoppableTCPListener(addr string) (*StoppableTCPListener, error) { +func newStoppableTCPListener(addr string) (*stoppableTCPListener, error) { wl, err := net.Listen("tcp", addr) if err != nil { return nil, err @@ -310,14 +310,14 @@ func NewStoppableTCPListener(addr string) (*StoppableTCPListener, error) { if tcpl, ok := wl.(*net.TCPListener); ok { stop := make(chan struct{}) - l := &StoppableTCPListener{tcpl, stop} + l := &stoppableTCPListener{tcpl, stop} return l, nil } return nil, errors.New("Unable to create TCP listener for RPC service") } -func (self *StoppableTCPListener) Accept() (net.Conn, error) { +func (self *stoppableTCPListener) Accept() (net.Conn, error) { for { self.SetDeadline(time.Now().Add(time.Duration(1 * time.Second))) c, err := self.TCPListener.AcceptTCP() @@ -338,16 +338,16 @@ func (self *StoppableTCPListener) Accept() (net.Conn, error) { } } - return &ClosableConnection{c, self.stop}, err + return &closableConnection{c, self.stop}, err } } -type ClosableConnection struct { +type closableConnection struct { *net.TCPConn closed chan struct{} } -func (self *ClosableConnection) Read(b []byte) (n int, err error) { +func (self *closableConnection) Read(b []byte) (n int, err error) { select { case <-self.closed: self.TCPConn.Close()