Coverage Report

Created: 2023-11-12 09:30

/proc/self/cwd/source/common/http/date_provider_impl.h
Line
Count
Source
1
#pragma once
2
3
#include <cstdint>
4
#include <string>
5
6
#include "envoy/event/dispatcher.h"
7
#include "envoy/singleton/instance.h"
8
#include "envoy/thread_local/thread_local.h"
9
10
#include "source/common/common/utility.h"
11
12
#include "date_provider.h"
13
14
namespace Envoy {
15
namespace Http {
16
17
/**
18
 * Base for all providers.
19
 */
20
class DateProviderImplBase : public DateProvider {
21
public:
22
14.8k
  explicit DateProviderImplBase(TimeSource& time_source) : time_source_(time_source) {}
23
24
protected:
25
  TimeSource& time_source_;
26
};
27
28
/**
29
 * A caching thread local provider. This implementation updates the date string every 500ms and
30
 * caches on each thread.
31
 */
32
class TlsCachingDateProviderImpl : public DateProviderImplBase, public Singleton::Instance {
33
public:
34
  TlsCachingDateProviderImpl(Event::Dispatcher& dispatcher, ThreadLocal::SlotAllocator& tls);
35
36
  // Http::DateProvider
37
  void setDateHeader(ResponseHeaderMap& headers) override;
38
39
private:
40
  struct ThreadLocalCachedDate : public ThreadLocal::ThreadLocalObject {
41
20.8k
    ThreadLocalCachedDate(const std::string& date_string) : date_string_(date_string) {}
42
43
    const std::string date_string_;
44
  };
45
46
  void onRefreshDate();
47
48
  ThreadLocal::SlotPtr tls_;
49
  Event::TimerPtr refresh_timer_;
50
};
51
52
/**
53
 * A basic provider that just creates the date string every time.
54
 */
55
class SlowDateProviderImpl : public DateProviderImplBase {
56
  using DateProviderImplBase::DateProviderImplBase;
57
58
public:
59
  // Http::DateProvider
60
  void setDateHeader(ResponseHeaderMap& headers) override;
61
};
62
63
} // namespace Http
64
} // namespace Envoy