Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
amaury1093 committed Dec 14, 2024
1 parent 2871d76 commit 3deef85
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 28 deletions.
43 changes: 24 additions & 19 deletions backend/backend_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,30 @@ hotmailb2c = "headless"
# recommended.
yahoo = "headless"

# Throttle the maximum number of requests per second, per minute, per hour, and
# per day for this worker.
# All fields are optional; comment them out to disable the limit.
#
# We however recommend setting the throttle for at least the per-minute and
# per-day limits to prevent the IPs from being blocked by the email providers.
# The default values are set to 60 requests per minute and 10,000 requests per
# day.
#
# Important: these throttle configurations only apply to /v1/* endpoints, and
# not to the previous /v0/check_email endpoint. The latter endpoint always
# executes the verification immediately, regardless of the throttle settings.
#
# Env variables:
# - RCH__WORKER__THROTTLE__MAX_REQUESTS_PER_SECOND
# - RCH__WORKER__THROTTLE__MAX_REQUESTS_PER_MINUTE
# - RCH__WORKER__THROTTLE__MAX_REQUESTS_PER_HOUR
# - RCH__WORKER__THROTTLE__MAX_REQUESTS_PER_DAY
[throttle]
# max_requests_per_second = 20
max_requests_per_minute = 60
# max_requests_per_hour = 1000
max_requests_per_day = 10000

[worker]
# Enable the worker to consume emails from the RabbitMQ queues. If set, the
# RabbitMQ configuration below must be set as well.
Expand All @@ -105,25 +129,6 @@ url = "amqp://guest:guest@localhost:5672"
# Env variable: RCH__WORKER__RABBITMQ__CONCURRENCY
concurrency = 5

# Throttle the maximum number of requests per second, per minute, per hour, and
# per day for this worker.
# All fields are optional; comment them out to disable the limit.
#
# Important: these throttle configurations only apply to /v1/* endpoints, and
# not to the previous /v0/check_email endpoint. The latter endpoint always
# executes the verification immediately, regardless of the throttle settings.
#
# Env variables:
# - RCH__WORKER__THROTTLE__MAX_REQUESTS_PER_SECOND
# - RCH__WORKER__THROTTLE__MAX_REQUESTS_PER_MINUTE
# - RCH__WORKER__THROTTLE__MAX_REQUESTS_PER_HOUR
# - RCH__WORKER__THROTTLE__MAX_REQUESTS_PER_DAY
[worker.throttle]
# max_requests_per_second = 20
# max_requests_per_minute = 100
# max_requests_per_hour = 1000
# max_requests_per_day = 20000

# Below are the configurations for the storage of the email verification
# results. We currently support the following storage backends:
# - Postgres
Expand Down
4 changes: 2 additions & 2 deletions backend/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@
"duration": {
"$ref": "#/components/schemas/Duration"
},
"server_name": {
"backend_name": {
"type": "string",
"x-stoplight": {
"id": "2jrbdecvqh4t5"
Expand All @@ -717,7 +717,7 @@
"start_time",
"end_time",
"duration",
"server_name",
"backend_name",
"smtp"
]
},
Expand Down
32 changes: 27 additions & 5 deletions backend/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,39 @@ pub struct BackendConfig {
}

impl BackendConfig {
/// Create an empty BackendConfig. This is useful for testing purposes.
pub fn empty() -> Self {
Self {
backend_name: "".to_string(),
from_email: "".to_string(),
hello_name: "".to_string(),
webdriver_addr: "".to_string(),
proxy: None,
verif_method: VerifMethodConfig::default(),
http_host: "127.0.0.1".to_string(),
http_port: 8080,
header_secret: None,
smtp_timeout: None,
sentry_dsn: None,
worker: WorkerConfig::default(),
storage: Some(StorageConfig::Noop),
commercial_license_trial: None,
throttle: ThrottleConfig::new_without_throttle(),
channel: None,
storage_adapter: Arc::new(StorageAdapter::Noop),
throttle_manager: Arc::new(
ThrottleManager::new(ThrottleConfig::new_without_throttle()),
),
}
}

/// Get the worker configuration.
///
/// # Panics
///
/// Panics if the worker configuration is missing.
pub fn must_worker_config(&self) -> Result<MustWorkerConfig, anyhow::Error> {
match (
self.worker.enable,
&self.worker.rabbitmq,
&self.channel,
) {
match (self.worker.enable, &self.worker.rabbitmq, &self.channel) {
(true, Some(rabbitmq), Some(channel)) => Ok(MustWorkerConfig {
channel: channel.clone(),
rabbitmq: rabbitmq.clone(),
Expand Down
4 changes: 2 additions & 2 deletions backend/tests/check_email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#[cfg(not(feature = "worker"))]
#[cfg(test)]
mod tests {
use std::sync::Arc;

Expand All @@ -27,7 +27,7 @@ mod tests {
const FOO_BAR_BAZ_RESPONSE: &str = r#"{"input":"foo@bar.baz","is_reachable":"invalid","misc":{"is_disposable":false,"is_role_account":false,"gravatar_url":null,"haveibeenpwned":null},"mx":{"accepts_mail":false,"records":[]},"smtp":{"can_connect_smtp":false,"has_full_inbox":false,"is_catch_all":false,"is_deliverable":false,"is_disabled":false},"syntax":{"address":"foo@bar.baz","domain":"bar.baz","is_valid_syntax":true,"username":"foo","normalized_email":"foo@bar.baz","suggestion":null}"#;

fn create_backend_config(header_secret: &str) -> Arc<BackendConfig> {
let mut config = BackendConfig::default();
let mut config = BackendConfig::empty();
config.header_secret = Some(header_secret.to_string());
Arc::new(config)
}
Expand Down

0 comments on commit 3deef85

Please sign in to comment.