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()