Skip to content

Commit 3aee64a

Browse files
fix: timeout issues for list.peerings
The previously used `DefaultContextTimeout` does not work well with the user-definable `WaitTime`, which can easily exceed this global timeout. Fixes #2041
1 parent 781ce19 commit 3aee64a

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

dependency/consul_peering.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,15 @@ func (l *ListPeeringQuery) Fetch(clients *ClientSet, opts *QueryOptions) (interf
9999
RawQuery: opts.String(),
100100
})
101101

102-
// list peering is a blocking API, so making sure the ctx passed while calling it
103-
// times out after the default wait time.
104-
ctx, cancel := context.WithTimeout(context.Background(), DefaultContextTimeout)
105-
defer cancel()
102+
// The first result needs to be "immediate", in which case we use the default timeout settings.
103+
// For all other requests, we use a 'blocking' query, therefore setting the appropriate timeout.
104+
// We add one second to allow for the Consul client to finish sending the response before
105+
// declaring it an exceeded deadline.
106+
var ctx context.Context
107+
if opts.WaitTime != 0 {
108+
ctx, cancel := context.WithTimeout(context.Background(), opts.WaitTime + time.Second)
109+
defer cancel()
110+
}
106111

107112
p, meta, err := clients.Consul().Peerings().List(ctx, opts.ToConsulOpts())
108113
if err != nil {

0 commit comments

Comments
 (0)