aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/matching/querylimiter.h
blob: 897fdd6b4a0f08bc8ff39906d3d08f3aabc063ad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <memory>
#include <vespa/vespalib/util/doom.h>
#include <mutex>
#include <condition_variable>

namespace proton::matching {

class QueryLimiter
{
private:
    using Doom = vespalib::Doom;
public:
    class Token {
    public:
        using UP = std::unique_ptr<Token>;
        virtual ~Token() = default;
    };
public:
    QueryLimiter();
    void configure(int maxThreads, double coverage, uint32_t minHits);
    Token::UP getToken(const Doom & doom, uint32_t numDocs, uint32_t numHits, bool hasSorting, bool hasGrouping);
private:
    class NoLimitToken : public Token {
    };
    class LimitedToken : public Token {
    private:
        QueryLimiter & _limiter;
    public:
        LimitedToken(const Doom & doom, QueryLimiter & limiter);
        LimitedToken(const NoLimitToken &) = delete;
        LimitedToken & operator =(const NoLimitToken &) = delete;
        ~LimitedToken() override;
    };
    void grabToken(const Doom & doom);
    void releaseToken();
    std::mutex              _lock;
    std::condition_variable _cond;
    int _activeThreads;

    // These are updated asynchronously at reconfig.
    std::atomic<int>      _maxThreads;
    std::atomic<double>   _coverage;
    std::atomic<uint32_t> _minHits;

    [[nodiscard]] int get_max_threads() const noexcept { return _maxThreads.load(std::memory_order_relaxed); }
    [[nodiscard]] double get_coverage() const noexcept { return _coverage.load(std::memory_order_relaxed); }
    [[nodiscard]] uint32_t get_min_hits() const noexcept { return _minHits.load(std::memory_order_relaxed); }
};

}