Skip to content

Commit b35b688

Browse files
committed
rpc: clean up IPC handler
This avoids logging accept errors on shutdown and removes a bit of duplication.
1 parent ba1030b commit b35b688

File tree

4 files changed

+20
-51
lines changed

4 files changed

+20
-51
lines changed

cmd/clef/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,18 @@ import (
2323
"context"
2424
"crypto/rand"
2525
"crypto/sha256"
26+
"encoding/hex"
2627
"encoding/json"
2728
"fmt"
2829
"io"
2930
"io/ioutil"
3031
"os"
32+
"os/signal"
3133
"os/user"
3234
"path/filepath"
3335
"runtime"
3436
"strings"
3537

36-
"encoding/hex"
3738
"github.com/ethereum/go-ethereum/cmd/utils"
3839
"github.com/ethereum/go-ethereum/common"
3940
"github.com/ethereum/go-ethereum/crypto"
@@ -44,7 +45,6 @@ import (
4445
"github.com/ethereum/go-ethereum/signer/rules"
4546
"github.com/ethereum/go-ethereum/signer/storage"
4647
"gopkg.in/urfave/cli.v1"
47-
"os/signal"
4848
)
4949

5050
// ExternalApiVersion -- see extapi_changelog.md
@@ -435,7 +435,7 @@ func signer(c *cli.Context) error {
435435
ipcApiUrl = filepath.Join(configDir, "clef.ipc")
436436
}
437437

438-
listener, _, err := rpc.StartIPCEndpoint(func() bool { return true }, ipcApiUrl, rpcApi)
438+
listener, _, err := rpc.StartIPCEndpoint(ipcApiUrl, rpcApi)
439439
if err != nil {
440440
utils.Fatalf("Could not start IPC api: %v", err)
441441
}

node/node.go

+2-12
Original file line numberDiff line numberDiff line change
@@ -303,23 +303,13 @@ func (n *Node) stopInProc() {
303303

304304
// startIPC initializes and starts the IPC RPC endpoint.
305305
func (n *Node) startIPC(apis []rpc.API) error {
306-
// Short circuit if the IPC endpoint isn't being exposed
307306
if n.ipcEndpoint == "" {
308-
return nil
309-
310-
}
311-
isClosed := func() bool {
312-
n.lock.RLock()
313-
defer n.lock.RUnlock()
314-
return n.ipcListener == nil
307+
return nil // IPC disabled.
315308
}
316-
317-
listener, handler, err := rpc.StartIPCEndpoint(isClosed, n.ipcEndpoint, apis)
309+
listener, handler, err := rpc.StartIPCEndpoint(n.ipcEndpoint, apis)
318310
if err != nil {
319311
return err
320312
}
321-
322-
// All listeners booted successfully
323313
n.ipcListener = listener
324314
n.ipcHandler = handler
325315
n.log.Info("IPC endpoint opened", "url", n.ipcEndpoint)

rpc/endpoints.go

+9-27
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
package rpc
1818

1919
import (
20-
"github.com/ethereum/go-ethereum/log"
2120
"net"
21+
22+
"github.com/ethereum/go-ethereum/log"
2223
)
2324

2425
// StartHTTPEndpoint starts the HTTP RPC endpoint, configured with cors/vhosts/modules
@@ -81,40 +82,21 @@ func StartWSEndpoint(endpoint string, apis []API, modules []string, wsOrigins []
8182

8283
}
8384

84-
// StartIPCEndpoint starts an IPC endpoint
85-
func StartIPCEndpoint(isClosedFn func() bool, ipcEndpoint string, apis []API) (net.Listener, *Server, error) {
86-
// Register all the APIs exposed by the services
85+
// StartIPCEndpoint starts an IPC endpoint.
86+
func StartIPCEndpoint(ipcEndpoint string, apis []API) (net.Listener, *Server, error) {
87+
// Register all the APIs exposed by the services.
8788
handler := NewServer()
8889
for _, api := range apis {
8990
if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
9091
return nil, nil, err
9192
}
9293
log.Debug("IPC registered", "namespace", api.Namespace)
9394
}
94-
// All APIs registered, start the IPC listener
95-
var (
96-
listener net.Listener
97-
err error
98-
)
99-
if listener, err = CreateIPCListener(ipcEndpoint); err != nil {
95+
// All APIs registered, start the IPC listener.
96+
listener, err := ipcListen(ipcEndpoint)
97+
if err != nil {
10098
return nil, nil, err
10199
}
102-
go func() {
103-
for {
104-
conn, err := listener.Accept()
105-
if err != nil {
106-
// Terminate if the listener was closed
107-
if isClosedFn() {
108-
log.Info("IPC closed", "err", err)
109-
} else {
110-
// Not closed, just some error; report and continue
111-
log.Error("IPC accept failed", "err", err)
112-
}
113-
continue
114-
}
115-
go handler.ServeCodec(NewJSONCodec(conn), OptionMethodInvocation|OptionSubscriptions)
116-
}
117-
}()
118-
100+
go handler.ServeListener(listener)
119101
return listener, handler, nil
120102
}

rpc/ipc.go

+6-9
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,23 @@ package rpc
1818

1919
import (
2020
"context"
21-
"fmt"
2221
"net"
2322

2423
"github.com/ethereum/go-ethereum/log"
24+
"github.com/ethereum/go-ethereum/p2p/netutil"
2525
)
2626

27-
// CreateIPCListener creates an listener, on Unix platforms this is a unix socket, on
28-
// Windows this is a named pipe
29-
func CreateIPCListener(endpoint string) (net.Listener, error) {
30-
return ipcListen(endpoint)
31-
}
32-
3327
// ServeListener accepts connections on l, serving JSON-RPC on them.
3428
func (srv *Server) ServeListener(l net.Listener) error {
3529
for {
3630
conn, err := l.Accept()
37-
if err != nil {
31+
if netutil.IsTemporaryError(err) {
32+
log.Warn("RPC accept error", "err", err)
33+
continue
34+
} else if err != nil {
3835
return err
3936
}
40-
log.Trace(fmt.Sprint("accepted conn", conn.RemoteAddr()))
37+
log.Trace("Accepted connection", "addr", conn.RemoteAddr())
4138
go srv.ServeCodec(NewJSONCodec(conn), OptionMethodInvocation|OptionSubscriptions)
4239
}
4340
}

0 commit comments

Comments
 (0)