Skip to content

Commit 116322d

Browse files
ellemoutonpositiveblue
authored andcommitted
rpc_proxy: add hasStarted method to rpcProxy
Add a `started` variable to the rpcProxy that is used to indicate if the proxy is ready to handle requests. This is because currently the webserver is dependent on the rpcProxy to start and we want to be able to start the webserver without being dependent on the rpcProxy so that it can be used to handle status requests in a future commit. So with this commit, we can now saftely start the webserver earlier on and then if requests come through for the rpcProxy, an error will be displayed to the user.
1 parent 73c36ac commit 116322d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

rpc_proxy.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net"
1111
"net/http"
1212
"strings"
13+
"sync/atomic"
1314
"time"
1415

1516
"github.com/improbable-eng/grpc-web/go/grpcweb"
@@ -37,6 +38,10 @@ const (
3738
HeaderMacaroon = "Macaroon"
3839
)
3940

41+
// ErrWaitingToStart is returned if Lit's rpcProxy is not yet ready to handle
42+
// calls.
43+
var ErrWaitingToStart = fmt.Errorf("waiting for the RPC server to start")
44+
4045
// proxyErr is an error type that adds more context to an error occurring in the
4146
// proxy.
4247
type proxyErr struct {
@@ -147,6 +152,10 @@ func newRpcProxy(cfg *Config, validator macaroons.MacaroonValidator,
147152
type rpcProxy struct {
148153
litrpc.UnimplementedProxyServer
149154

155+
// started is set to 1 once the rpcProxy has successfully started. It
156+
// must only ever be used atomically.
157+
started int32
158+
150159
cfg *Config
151160
basicAuth string
152161
permsMgr *perms.Manager
@@ -218,9 +227,17 @@ func (p *rpcProxy) Start() error {
218227
}
219228
}
220229

230+
atomic.CompareAndSwapInt32(&p.started, 0, 1)
231+
221232
return nil
222233
}
223234

235+
// hasStarted returns true if the rpcProxy has started and is ready to handle
236+
// requests.
237+
func (p *rpcProxy) hasStarted() bool {
238+
return atomic.LoadInt32(&p.started) == 1
239+
}
240+
224241
// Stop shuts down the lnd connection.
225242
func (p *rpcProxy) Stop() error {
226243
p.grpcServer.Stop()
@@ -399,6 +416,10 @@ func (p *rpcProxy) UnaryServerInterceptor(ctx context.Context, req interface{},
399416
info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{},
400417
error) {
401418

419+
if !p.hasStarted() {
420+
return nil, ErrWaitingToStart
421+
}
422+
402423
uriPermissions, ok := p.permsMgr.URIPermissions(info.FullMethod)
403424
if !ok {
404425
return nil, fmt.Errorf("%s: unknown permissions "+
@@ -440,6 +461,10 @@ func (p *rpcProxy) StreamServerInterceptor(srv interface{},
440461
ss grpc.ServerStream, info *grpc.StreamServerInfo,
441462
handler grpc.StreamHandler) error {
442463

464+
if !p.hasStarted() {
465+
return ErrWaitingToStart
466+
}
467+
443468
uriPermissions, ok := p.permsMgr.URIPermissions(info.FullMethod)
444469
if !ok {
445470
return fmt.Errorf("%s: unknown permissions required "+

0 commit comments

Comments
 (0)