Repository: envoyproxy/envoy
File: source/common/access_log/access_log_manager_impl.h (C/C++)

Code before:

struct AccessLogFileStats {
  ACCESS_LOG_FILE_STATS(GENERATE_COUNTER_STRUCT, GENERATE_GAUGE_STRUCT)
};

namespace AccessLog {

class AccessLogManagerImpl : public AccessLogManager, Logger::Loggable<Logger::Id::main> {
public:
  AccessLogManagerImpl(std::chrono::milliseconds file_flush_interval_msec,
                       uint64_t min_flush_size_kb, Api::Api& api, Event::Dispatcher& dispatcher,
                       Thread::BasicLockable& lock, Stats::Store& stats_store)
      : file_flush_interval_msec_(file_flush_interval_msec),
        file_min_flush_size_kb_(min_flush_size_kb), api_(api), dispatcher_(dispatcher), lock_(lock),
        file_stats_{ACCESS_LOG_FILE_STATS(POOL_COUNTER_PREFIX(stats_store, "filesystem."),
                                          POOL_GAUGE_PREFIX(stats_store, "filesystem."))} {}
  ~AccessLogManagerImpl() override;

  // AccessLog::AccessLogManager
  void reopen() override;
  absl::StatusOr<AccessLogFileSharedPtr>
  createAccessLog(const Filesystem::FilePathAndType& file_info) override;

private:
  const std::chrono::milliseconds file_flush_interval_msec_;
  const uint64_t file_min_flush_size_kb_;
  Api::Api& api_;
  Event::Dispatcher& dispatcher_;
  Thread::BasicLockable& lock_;
  AccessLogFileStats file_stats_;
  absl::node_hash_map<std::string, AccessLogFileSharedPtr> access_logs_;
};

/**
 * This is a file implementation geared for writing out access logs. It turn out that in certain
 * cases even if a standard file is opened with O_NONBLOCK, the kernel can still block when writing.
 * This implementation uses a flush thread per file, with the idea there aren't that many
 * files. If this turns out to be a good implementation we can potentially have a single flush
 * thread that flushes all files, but we will start with this.
 */
class AccessLogFileImpl : public AccessLogFile {
public:
  AccessLogFileImpl(Filesystem::FilePtr&& file, Event::Dispatcher& dispatcher,
                    Thread::BasicLockable& lock, AccessLogFileStats& stats,
                    std::chrono::milliseconds flush_interval_msec, uint64_t min_flush_size_kb,
                    Thread::ThreadFactory& thread_factory);
  ~AccessLogFileImpl() override;

  // AccessLog::AccessLogFile
  void write(absl::string_view data) override;


Review comment (suggestion):
```suggestion
  const uint64_t file_min_flush_size_kb_{64};
```

Code after:

struct AccessLogFileStats {
  ACCESS_LOG_FILE_STATS(GENERATE_COUNTER_STRUCT, GENERATE_GAUGE_STRUCT)
};

namespace AccessLog {

class AccessLogManagerImpl : public AccessLogManager, Logger::Loggable<Logger::Id::main> {
public:
  AccessLogManagerImpl(std::chrono::milliseconds file_flush_interval_msec,
                       uint64_t min_flush_size_kb, Api::Api& api, Event::Dispatcher& dispatcher,
                       Thread::BasicLockable& lock, Stats::Store& stats_store)
      : file_flush_interval_msec_(file_flush_interval_msec),
        file_min_flush_size_kb_(min_flush_size_kb), api_(api), dispatcher_(dispatcher), lock_(lock),
        file_stats_{ACCESS_LOG_FILE_STATS(POOL_COUNTER_PREFIX(stats_store, "filesystem."),
                                          POOL_GAUGE_PREFIX(stats_store, "filesystem."))} {}
  ~AccessLogManagerImpl() override;

  // AccessLog::AccessLogManager
  void reopen() override;
  absl::StatusOr<AccessLogFileSharedPtr>
  createAccessLog(const Filesystem::FilePathAndType& file_info) override;

private:
  const std::chrono::milliseconds file_flush_interval_msec_;
  const uint64_t file_min_flush_size_kb_{64};
  Api::Api& api_;
  Event::Dispatcher& dispatcher_;
  Thread::BasicLockable& lock_;
  AccessLogFileStats file_stats_;
  absl::node_hash_map<std::string, AccessLogFileSharedPtr> access_logs_;
};

/**
 * This is a file implementation geared for writing out access logs. It turn out that in certain
 * cases even if a standard file is opened with O_NONBLOCK, the kernel can still block when writing.
 * This implementation uses a flush thread per file, with the idea there aren't that many
 * files. If this turns out to be a good implementation we can potentially have a single flush
 * thread that flushes all files, but we will start with this.
 */
class AccessLogFileImpl : public AccessLogFile {
public:
  AccessLogFileImpl(Filesystem::FilePtr&& file, Event::Dispatcher& dispatcher,
                    Thread::BasicLockable& lock, AccessLogFileStats& stats,
                    std::chrono::milliseconds flush_interval_msec, uint64_t min_flush_size_kb,
                    Thread::ThreadFactory& thread_factory);
  ~AccessLogFileImpl() override;

  // AccessLog::AccessLogFile
  void write(absl::string_view data) override;