Kurlyk
Loading...
Searching...
No Matches
HttpRateLimitHandle.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _KURLYK_HTTP_RATE_LIMIT_HANDLE_HPP_INCLUDED
3#define _KURLYK_HTTP_RATE_LIMIT_HANDLE_HPP_INCLUDED
4
7
8namespace kurlyk {
9
23 public:
26
31
34 long id() const {
35 return m_id;
36 }
37
40 explicit operator bool() const {
41 return m_id != 0;
42 }
43
44 private:
45 friend class HttpRateLimiter;
46
47 using remove_fn_t = std::function<void(long)>;
48
52 HttpRateLimitHandle(long id, remove_fn_t remove_fn)
53 : m_id(id),
54 m_remove_fn(std::move(remove_fn)) {}
55
60 void reset() {
61 const long id = m_id;
62 m_id = 0;
63
64 if (id != 0 && m_remove_fn) {
65 m_remove_fn(id);
66 }
67 }
68
69 private:
70 long m_id = 0;
72 };
73
78 using HttpRateLimitHandlePtr = std::shared_ptr<HttpRateLimitHandle>;
79
80} // namespace kurlyk
81
82#endif // _KURLYK_HTTP_RATE_LIMIT_HANDLE_HPP_INCLUDED
~HttpRateLimitHandle()
Destroys the handle and releases the owned rate-limit entry.
HttpRateLimitHandle(long id, remove_fn_t remove_fn)
Creates a handle for a registered rate-limit ID.
std::function< void(long)> remove_fn_t
HttpRateLimitHandle & operator=(const HttpRateLimitHandle &)=delete
long m_id
Owned rate-limit ID, or 0 after reset.
remove_fn_t m_remove_fn
Callback that physically removes the rate-limit entry.
long id() const
Returns immutable rate-limit ID associated with this handle.
void reset()
Releases the owned rate-limit entry once.
HttpRateLimitHandle(const HttpRateLimitHandle &)=delete
Primary namespace for the Kurlyk library, encompassing initialization, request management,...
std::shared_ptr< HttpRateLimitHandle > HttpRateLimitHandlePtr
Shared RAII handle for HTTP rate limits.
Enables use of ClientError with std::error_code.