<commit_before>#pragma once
// seastar
#include <core/distributed.hh>
#include <core/gate.hh>

namespace smf {
/// \brief Resource limits for an RPC server
///
/// A request's memory use will be estimated as
///
///     req_mem = basic_request_size + sizeof(serialized_request) * bloat_factor
///
/// Concurrent requests will be limited so that
///
///     sum(req_mem) <= max_memory
///
struct rpc_connection_limits {
  rpc_connection_limits(
    size_t basic_req_size = 1024,
    size_t bloat_mult = 1.57, // same as folly::vector
    /// The defaults are the ones from scylladb as of 8e124b3a
    size_t max_mem = std::max<size_t>(0.08 * memory::stats().total_memory(),
                                      1000000))
    : basic_request_size(basic_req_size)
    , bloat_factor(bloat_mult)
    , max_memory(max_mem) {}

  /// Minimum request footprint in memory
  const size_t basic_request_size;
  /// Serialized size multiplied by this to estimate
  /// memory used by request
  const size_t bloat_factor;
  const size_t max_memory;

  size_t estimate_request_size(size_t serialized_size) {
    return (basic_request_size + serialized_size) * bloat_factor;
  }

  semaphore resources_available{max_memory};
  seastar::gate reply_gate;
};
}<commit_msg>rpc: use seastar::semaphore to guard mem abuse<commit_after>#pragma once
// seastar
#include <core/distributed.hh>
#include <core/gate.hh>

namespace smf {
/// \brief Resource limits for an RPC server
///
/// A request's memory use will be estimated as
///
///     req_mem = basic_request_size + sizeof(serialized_request) * bloat_factor
///
/// Concurrent requests will be limited so that
///
///     sum(req_mem) <= max_memory
///
struct rpc_connection_limits {
  rpc_connection_limits(
    size_t basic_req_size = 1024,
    size_t bloat_mult = 1.57, // same as folly::vector
    /// The defaults are the ones from scylladb as of 8e124b3a
    size_t max_mem = std::max<size_t>(0.08 * memory::stats().total_memory(),
                                      1000000))
    : basic_request_size(basic_req_size)
    , bloat_factor(bloat_mult)
    , max_memory(max_mem) {}

  /// Minimum request footprint in memory
  const size_t basic_request_size;
  /// Serialized size multiplied by this to estimate
  /// memory used by request
  const size_t bloat_factor;
  const size_t max_memory;

  size_t estimate_request_size(size_t serialized_size) {
    return (basic_request_size + serialized_size) * bloat_factor;
  }

  future<> wait_for_resources(size_t memory_consumed) {
    return resources_available.wait(memory_consumed);
  }
  void release_resources(size_t memory_consumed) {
    resources_available.signal(memory_consumed);
  }

  ~rpc_connection_limits() {}

  semaphore resources_available{max_memory};

  // TODO(agallego) - rename to connection drain accept()
  seastar::gate reply_gate;
};
}
{'fraction_non_alphanumeric': 0.10336370007007709, 'fraction_numerical': 0.015416958654519973, 'mean_word_length': 3.3923076923076922, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2340257', 'n_tokens_mistral': 959, 'n_tokens_neox': 905, 'n_words': 228}