mirror of
https://github.com/status-im/consul.git
synced 2025-01-27 05:57:03 +00:00
rpc: extract rpcQueryTimeout method
This helps keep the logic in blockingQuery more focused. In the future we may have a separate struct for RPC queries which may allow us to move this off of Server.
This commit is contained in:
parent
fd0a9fd4f3
commit
72a733bed8
@ -938,19 +938,8 @@ func (s *Server) blockingQuery(queryOpts structs.QueryOptionsCompat, queryMeta s
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
queryTimeout := queryOpts.GetMaxQueryTime()
|
timeout := s.rpcQueryTimeout(queryOpts.GetMaxQueryTime())
|
||||||
// Restrict the max query time, and ensure there is always one.
|
ctx, cancel := context.WithTimeout(ctx, timeout)
|
||||||
if queryTimeout > s.config.MaxQueryTime {
|
|
||||||
queryTimeout = s.config.MaxQueryTime
|
|
||||||
} else if queryTimeout <= 0 {
|
|
||||||
queryTimeout = s.config.DefaultQueryTime
|
|
||||||
}
|
|
||||||
|
|
||||||
// Apply a small amount of jitter to the request.
|
|
||||||
queryTimeout += lib.RandomStagger(queryTimeout / structs.JitterFraction)
|
|
||||||
|
|
||||||
// wrap the base context with a deadline
|
|
||||||
ctx, cancel := context.WithTimeout(ctx, queryTimeout)
|
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
// instrument blockingQueries
|
// instrument blockingQueries
|
||||||
@ -1068,6 +1057,22 @@ func (s *Server) consistentRead() error {
|
|||||||
return structs.ErrNotReadyForConsistentReads
|
return structs.ErrNotReadyForConsistentReads
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// rpcQueryTimeout calculates the timeout for the query, ensures it is
|
||||||
|
// constrained to the configured limit, and adds jitter to prevent multiple
|
||||||
|
// blocking queries from all timing out at the same time.
|
||||||
|
func (s *Server) rpcQueryTimeout(queryTimeout time.Duration) time.Duration {
|
||||||
|
// Restrict the max query time, and ensure there is always one.
|
||||||
|
if queryTimeout > s.config.MaxQueryTime {
|
||||||
|
queryTimeout = s.config.MaxQueryTime
|
||||||
|
} else if queryTimeout <= 0 {
|
||||||
|
queryTimeout = s.config.DefaultQueryTime
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply a small amount of jitter to the request.
|
||||||
|
queryTimeout += lib.RandomStagger(queryTimeout / structs.JitterFraction)
|
||||||
|
return queryTimeout
|
||||||
|
}
|
||||||
|
|
||||||
// maskResultsFilteredByACLs blanks out the ResultsFilteredByACLs flag if the
|
// maskResultsFilteredByACLs blanks out the ResultsFilteredByACLs flag if the
|
||||||
// request is unauthenticated, to limit information leaking.
|
// request is unauthenticated, to limit information leaking.
|
||||||
//
|
//
|
||||||
|
Loading…
x
Reference in New Issue
Block a user