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
#include "source/common/http/date_provider.h"
12

            
13
namespace Envoy {
14
namespace Http {
15

            
16
/**
17
 * Base for all providers.
18
 */
19
class DateProviderImplBase : public DateProvider {
20
public:
21
20723
  explicit DateProviderImplBase(TimeSource& time_source) : time_source_(time_source) {}
22

            
23
protected:
24
  TimeSource& time_source_;
25
};
26

            
27
/**
28
 * A caching thread local provider. This implementation updates the date string every 500ms and
29
 * caches on each thread.
30
 */
31
class TlsCachingDateProviderImpl : public DateProviderImplBase, public Singleton::Instance {
32
public:
33
  TlsCachingDateProviderImpl(Event::Dispatcher& dispatcher, ThreadLocal::SlotAllocator& tls);
34

            
35
  // Http::DateProvider
36
  void setDateHeader(ResponseHeaderMap& headers) override;
37

            
38
private:
39
  struct ThreadLocalCachedDate : public ThreadLocal::ThreadLocalObject {
40
38869
    ThreadLocalCachedDate(const std::string& date_string) : date_string_(date_string) {}
41

            
42
    const std::string date_string_;
43
  };
44

            
45
  void onRefreshDate();
46

            
47
  ThreadLocal::SlotPtr tls_;
48
  Event::TimerPtr refresh_timer_;
49
};
50

            
51
/**
52
 * A basic provider that just creates the date string every time.
53
 */
54
class SlowDateProviderImpl : public DateProviderImplBase {
55
  using DateProviderImplBase::DateProviderImplBase;
56

            
57
public:
58
  // Http::DateProvider
59
  void setDateHeader(ResponseHeaderMap& headers) override;
60
};
61

            
62
} // namespace Http
63
} // namespace Envoy