Skip to content

Commit 1650f3a

Browse files
committed
client+lsat: specify global timeout
1 parent fa62caa commit 1650f3a

File tree

4 files changed

+38
-19
lines changed

4 files changed

+38
-19
lines changed

client.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,15 @@ var (
4949
ErrSweepConfTargetTooFar = errors.New("sweep confirmation target is " +
5050
"beyond swap expiration height")
5151

52+
// serverRPCTimeout is the maximum time a gRPC request to the server
53+
// should be allowed to take.
5254
serverRPCTimeout = 30 * time.Second
5355

56+
// globalCallTimeout is the maximum time any call of the client to the
57+
// server is allowed to take, including the time it may take to get
58+
// and pay for an LSAT token.
59+
globalCallTimeout = serverRPCTimeout + lsat.PaymentTimeout
60+
5461
republishDelay = 10 * time.Second
5562
)
5663

lsat/interceptor.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,22 @@ var (
6060
// challenges with embedded payment requests. It uses a connection to lnd to
6161
// automatically pay for an authentication token.
6262
type Interceptor struct {
63-
lnd *lndclient.LndServices
64-
store Store
65-
lock sync.Mutex
63+
lnd *lndclient.LndServices
64+
store Store
65+
callTimeout time.Duration
66+
lock sync.Mutex
6667
}
6768

6869
// NewInterceptor creates a new gRPC client interceptor that uses the provided
6970
// lnd connection to automatically acquire and pay for LSAT tokens, unless the
7071
// indicated store already contains a usable token.
71-
func NewInterceptor(lnd *lndclient.LndServices, store Store) *Interceptor {
72+
func NewInterceptor(lnd *lndclient.LndServices, store Store,
73+
rpcCallTimeout time.Duration) *Interceptor {
74+
7275
return &Interceptor{
73-
lnd: lnd,
74-
store: store,
76+
lnd: lnd,
77+
store: store,
78+
callTimeout: rpcCallTimeout,
7579
}
7680
}
7781

@@ -133,7 +137,9 @@ func (i *Interceptor) UnaryInterceptor(ctx context.Context, method string,
133137
// method again later with the paid LSAT token.
134138
trailerMetadata := &metadata.MD{}
135139
opts = append(opts, grpc.Trailer(trailerMetadata))
136-
err = invoker(ctx, method, req, reply, cc, opts...)
140+
rpcCtx, cancel := context.WithTimeout(ctx, i.callTimeout)
141+
defer cancel()
142+
err = invoker(rpcCtx, method, req, reply, cc, opts...)
137143

138144
// Only handle the LSAT error message that comes in the form of
139145
// a gRPC status error.
@@ -149,7 +155,9 @@ func (i *Interceptor) UnaryInterceptor(ctx context.Context, method string,
149155

150156
// Execute the same request again, now with the LSAT
151157
// token added as an RPC credential.
152-
return invoker(ctx, method, req, reply, cc, opts...)
158+
rpcCtx2, cancel2 := context.WithTimeout(ctx, i.callTimeout)
159+
defer cancel2()
160+
return invoker(rpcCtx2, method, req, reply, cc, opts...)
153161
}
154162
return err
155163
}

lsat/interceptor_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ func TestInterceptor(t *testing.T) {
4545
t.Parallel()
4646

4747
var (
48-
lnd = test.NewMockLnd()
49-
store = &mockStore{}
50-
testTimeout = 5 * time.Second
51-
interceptor = NewInterceptor(&lnd.LndServices, store)
48+
lnd = test.NewMockLnd()
49+
store = &mockStore{}
50+
testTimeout = 5 * time.Second
51+
interceptor = NewInterceptor(
52+
&lnd.LndServices, store, testTimeout,
53+
)
5254
testMac = makeMac(t)
5355
testMacBytes = serializeMac(t, testMac)
5456
testMacHex = hex.EncodeToString(testMacBytes)

swap_server_client.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ func newSwapServerClient(address string, insecure bool, tlsPath string,
5656

5757
// Create the server connection with the interceptor that will handle
5858
// the LSAT protocol for us.
59-
clientInterceptor := lsat.NewInterceptor(lnd, lsatStore)
59+
clientInterceptor := lsat.NewInterceptor(
60+
lnd, lsatStore, serverRPCTimeout,
61+
)
6062
serverConn, err := getSwapServerConn(
6163
address, insecure, tlsPath, clientInterceptor,
6264
)
@@ -75,7 +77,7 @@ func newSwapServerClient(address string, insecure bool, tlsPath string,
7577
func (s *grpcSwapServerClient) GetLoopOutTerms(ctx context.Context) (
7678
*LoopOutTerms, error) {
7779

78-
rpcCtx, rpcCancel := context.WithTimeout(ctx, serverRPCTimeout)
80+
rpcCtx, rpcCancel := context.WithTimeout(ctx, globalCallTimeout)
7981
defer rpcCancel()
8082
terms, err := s.server.LoopOutTerms(rpcCtx,
8183
&looprpc.ServerLoopOutTermsRequest{},
@@ -93,7 +95,7 @@ func (s *grpcSwapServerClient) GetLoopOutTerms(ctx context.Context) (
9395
func (s *grpcSwapServerClient) GetLoopOutQuote(ctx context.Context,
9496
amt btcutil.Amount) (*LoopOutQuote, error) {
9597

96-
rpcCtx, rpcCancel := context.WithTimeout(ctx, serverRPCTimeout)
98+
rpcCtx, rpcCancel := context.WithTimeout(ctx, globalCallTimeout)
9799
defer rpcCancel()
98100
quoteResp, err := s.server.LoopOutQuote(rpcCtx,
99101
&looprpc.ServerLoopOutQuoteRequest{
@@ -125,7 +127,7 @@ func (s *grpcSwapServerClient) GetLoopOutQuote(ctx context.Context,
125127
func (s *grpcSwapServerClient) GetLoopInTerms(ctx context.Context) (
126128
*LoopInTerms, error) {
127129

128-
rpcCtx, rpcCancel := context.WithTimeout(ctx, serverRPCTimeout)
130+
rpcCtx, rpcCancel := context.WithTimeout(ctx, globalCallTimeout)
129131
defer rpcCancel()
130132
terms, err := s.server.LoopInTerms(rpcCtx,
131133
&looprpc.ServerLoopInTermsRequest{},
@@ -143,7 +145,7 @@ func (s *grpcSwapServerClient) GetLoopInTerms(ctx context.Context) (
143145
func (s *grpcSwapServerClient) GetLoopInQuote(ctx context.Context,
144146
amt btcutil.Amount) (*LoopInQuote, error) {
145147

146-
rpcCtx, rpcCancel := context.WithTimeout(ctx, serverRPCTimeout)
148+
rpcCtx, rpcCancel := context.WithTimeout(ctx, globalCallTimeout)
147149
defer rpcCancel()
148150
quoteResp, err := s.server.LoopInQuote(rpcCtx,
149151
&looprpc.ServerLoopInQuoteRequest{
@@ -165,7 +167,7 @@ func (s *grpcSwapServerClient) NewLoopOutSwap(ctx context.Context,
165167
receiverKey [33]byte, swapPublicationDeadline time.Time) (
166168
*newLoopOutResponse, error) {
167169

168-
rpcCtx, rpcCancel := context.WithTimeout(ctx, serverRPCTimeout)
170+
rpcCtx, rpcCancel := context.WithTimeout(ctx, globalCallTimeout)
169171
defer rpcCancel()
170172
swapResp, err := s.server.NewLoopOutSwap(rpcCtx,
171173
&looprpc.ServerLoopOutRequest{
@@ -200,7 +202,7 @@ func (s *grpcSwapServerClient) NewLoopInSwap(ctx context.Context,
200202
swapHash lntypes.Hash, amount btcutil.Amount, senderKey [33]byte,
201203
swapInvoice string) (*newLoopInResponse, error) {
202204

203-
rpcCtx, rpcCancel := context.WithTimeout(ctx, serverRPCTimeout)
205+
rpcCtx, rpcCancel := context.WithTimeout(ctx, globalCallTimeout)
204206
defer rpcCancel()
205207
swapResp, err := s.server.NewLoopInSwap(rpcCtx,
206208
&looprpc.ServerLoopInRequest{

0 commit comments

Comments
 (0)