Class RateLimitingSearcher

  • All Implemented Interfaces:, java.lang.Comparable<>

    public class RateLimitingSearcher
    extends Searcher
    A simple rate limiter.

    This takes these query parameter arguments:

    • - (String) the id of the client from rate limiting perspective
    • rate.cost - (Double) the cost Double of this query. This is read after executing the query and hence can be set by downstream searchers inspecting the result to allow differencing the cost of various queries. Default is 1.
    • rate.quota - (Double) the cost per second a particular id is allowed to consume in this system.
    • rate.idDimension - (String) the name of the rate-id dimension used when logging metrics. If this is not specified, the metric will be logged without dimensions.
    • rate.dryRun - (Boolean) emit metrics on rejected requests but don't actually reject them

    Whenever quota is exceeded for an id this searcher will reject queries from that id by returning a result containing a status 429 error.

    If or rate.quota is not set in this searcher will do nothing.

    Metrics: This will emit the count metric requestsOverQuota with the dimension [] counting rejected requests.

    Ordering: This searcher Provides rateLimiting

    • Field Summary

      Modifier and Type Field Description
      static costKey  
      static dryRunKey  
      static idDimensionKey  
      static idKey  
      static quotaKey  
      static java.lang.String RATE_LIMITING
      Constant containing the name this Provides - "rateLimiting", for ordering constraints
      • Fields inherited from class

    • Constructor Summary

      Constructor Description
      RateLimitingSearcher​(RateLimitingConfig rateLimitingConfig, clusterInfoConfig, metric)  
      RateLimitingSearcher​(RateLimitingConfig rateLimitingConfig, clusterInfoConfig, metric, java.time.Clock clock)
      For testing - allows injection of a timer to avoid depending on the system clock
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Result search​(Query query, Execution execution)
      Override this to implement your searcher.
      • Methods inherited from class

        getAnnotatedDependencies, getDefaultAnnotatedDependencies, getDependencies, initDependencies
      • Methods inherited from class

        clone, compareTo, deconstruct, getClassName, getId, getIdString, hasInitializedId, initId, isDeconstructable, setIsDeconstructable
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail


        public static final java.lang.String RATE_LIMITING
        Constant containing the name this Provides - "rateLimiting", for ordering constraints
        See Also:
        Constant Field Values
      • idKey

        public static final idKey
      • costKey

        public static final costKey
      • quotaKey

        public static final quotaKey
      • idDimensionKey

        public static final idDimensionKey
      • dryRunKey

        public static final dryRunKey
    • Constructor Detail

      • RateLimitingSearcher

        public RateLimitingSearcher​(RateLimitingConfig rateLimitingConfig,
      • RateLimitingSearcher

        public RateLimitingSearcher​(RateLimitingConfig rateLimitingConfig,
                                    java.time.Clock clock)
        For testing - allows injection of a timer to avoid depending on the system clock
    • Method Detail

      • search

        public Result search​(Query query,
                             Execution execution)
        Description copied from class: Searcher
        Override this to implement your searcher.

        Searcher implementation subclasses will, depending on their type of logic, do one of the following:

        • Query processors: Access the query, then call and return the result
        • Result processors: Call to get the result, access it and return
        • Sources (which produces results): Create a result, add the desired hits and return it.
        • Federators (which forwards the search to multiple subchains): Call search on the desired subchains in parallel and get the results. Combine the results to one and return it.
        • Workflows: Call as many times as desired, using different queries. Eventually return a result.

        Hits come in two kinds - concrete hits are actual content of the kind requested by the user, meta hits are hits which provides information about the collection of hits, on the query, the service and so on.

        The query specifies a window into a larger result list that must be returned from the searcher through hits and offset; Searchers which returns list of hits in the top level in the result must return at least hits number of hits (or if impossible; all that are available), starting at the given offset. In addition, searchers are allowed to return any number of meta hits (although this number is expected to be low). For hits contained in nested hit groups, the concept of a window defined by hits and offset is not well defined and does not apply.

        Error handling in searchers:

        • Unexpected events: Throw any RuntimeException. This query will fail with the exception message, and the error will be logged
        • Expected events: Create (new Result(Query, ErrorMessage) or add result.setErrorIfNoOtherErrors(ErrorMessage) an error message to the Result.
        • Recoverable user errors: Add a FeedbackHit explaining the condition and how to correct it.
        Specified by:
        search in class Searcher
        query - the query
        the result of making this query