-
Notifications
You must be signed in to change notification settings - Fork 652
/
publicrpcRunnable.go
80 lines (64 loc) · 2.8 KB
/
publicrpcRunnable.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package node
import (
"context"
"fmt"
"net"
"os"
"github.com/certusone/wormhole/node/pkg/common"
"github.com/certusone/wormhole/node/pkg/db"
"github.com/certusone/wormhole/node/pkg/governor"
publicrpcv1 "github.com/certusone/wormhole/node/pkg/proto/publicrpc/v1"
"github.com/certusone/wormhole/node/pkg/publicrpc"
"github.com/certusone/wormhole/node/pkg/supervisor"
"go.uber.org/zap"
"google.golang.org/grpc"
)
func publicrpcTcpServiceRunnable(logger *zap.Logger, listenAddr string, publicRpcLogDetail common.GrpcLogDetail, db *db.Database, gst *common.GuardianSetState, gov *governor.ChainGovernor) supervisor.Runnable {
return func(ctx context.Context) error {
l, err := net.Listen("tcp", listenAddr)
if err != nil {
return fmt.Errorf("failed to listen: %w", err)
}
logger.Info("publicrpc server listening", zap.String("addr", l.Addr().String()))
rpcServer := publicrpc.NewPublicrpcServer(logger, db, gst, gov)
grpcServer := common.NewInstrumentedGRPCServer(logger, publicRpcLogDetail)
publicrpcv1.RegisterPublicRPCServiceServer(grpcServer, rpcServer)
if err := supervisor.Run(ctx, "grpcserver", supervisor.GRPCServer(grpcServer, l, false)); err != nil {
return err
}
<-ctx.Done()
return nil
}
}
func publicrpcUnixServiceRunnable(logger *zap.Logger, socketPath string, publicRpcLogDetail common.GrpcLogDetail, db *db.Database, gst *common.GuardianSetState, gov *governor.ChainGovernor) (supervisor.Runnable, *grpc.Server, error) {
// Delete existing UNIX socket, if present.
fi, err := os.Stat(socketPath)
if err == nil {
fmode := fi.Mode()
if fmode&os.ModeType == os.ModeSocket {
err = os.Remove(socketPath)
if err != nil {
return nil, nil, fmt.Errorf("failed to remove existing socket at %s: %w", socketPath, err)
}
} else {
return nil, nil, fmt.Errorf("%s is not a UNIX socket", socketPath)
}
}
// Create a new UNIX socket and listen to it.
// The socket is created with the default umask. We set a restrictive umask in setRestrictiveUmask
// to ensure that any files we create are only readable by the user - this is much harder to mess up.
// The umask avoids a race condition between file creation and chmod.
laddr, err := net.ResolveUnixAddr("unix", socketPath)
if err != nil {
return nil, nil, fmt.Errorf("invalid listen address: %v", err)
}
l, err := net.ListenUnix("unix", laddr)
if err != nil {
return nil, nil, fmt.Errorf("failed to listen on %s: %w", socketPath, err)
}
logger.Info("publicrpc (unix socket) server listening on", zap.String("path", socketPath))
publicrpcService := publicrpc.NewPublicrpcServer(logger, db, gst, gov)
grpcServer := common.NewInstrumentedGRPCServer(logger, publicRpcLogDetail)
publicrpcv1.RegisterPublicRPCServiceServer(grpcServer, publicrpcService)
return supervisor.GRPCServer(grpcServer, l, false), grpcServer, nil
}