From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 09B9364A82 for ; Fri, 28 Jan 2022 15:10:32 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id F222E2F4FC for ; Fri, 28 Jan 2022 15:10:31 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 45F622F4F2 for ; Fri, 28 Jan 2022 15:10:31 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 16D2F46D7D for ; Fri, 28 Jan 2022 15:10:31 +0100 (CET) From: Dominik Csapak To: pbs-devel@lists.proxmox.com Date: Fri, 28 Jan 2022 15:10:30 +0100 Message-Id: <20220128141030.1373160-1-d.csapak@proxmox.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.164 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [proxmox-backup-proxy.rs] Subject: [pbs-devel] [RFC PATCH proxmox-backup] traffic-control: use SocketAddr from 'accept()' X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 28 Jan 2022 14:10:32 -0000 instead of getting the 'peer_addr()' from the socket. The advantage is that we must get this and thus can drop the mapping from result -> option, and can drop the testing for None and a test case Signed-off-by: Dominik Csapak --- did stumble upon this while debugging. looks cleaner to me and i see no obvious disadvantage src/bin/proxmox-backup-proxy.rs | 5 ++--- src/cached_traffic_control.rs | 22 ++++++---------------- 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/src/bin/proxmox-backup-proxy.rs b/src/bin/proxmox-backup-proxy.rs index 30b730ef..8d0033de 100644 --- a/src/bin/proxmox-backup-proxy.rs +++ b/src/bin/proxmox-backup-proxy.rs @@ -391,7 +391,7 @@ async fn accept_connection( let accept_counter = Arc::new(()); loop { - let (sock, _addr) = match listener.accept().await { + let (sock, peer) = match listener.accept().await { Ok(conn) => conn, Err(err) => { eprintln!("error accepting tcp connection: {}", err); @@ -402,7 +402,6 @@ async fn accept_connection( sock.set_nodelay(true).unwrap(); let _ = set_tcp_keepalive(sock.as_raw_fd(), PROXMOX_BACKUP_TCP_KEEPALIVE_TIME); - let peer = sock.peer_addr().ok(); let sock = RateLimitedStream::with_limiter_update_cb(sock, move || lookup_rate_limiter(peer)); let ssl = { // limit acceptor_guard scope @@ -1144,7 +1143,7 @@ async fn run_traffic_control_updater() { } fn lookup_rate_limiter( - peer: Option, + peer: std::net::SocketAddr, ) -> (Option>, Option>) { let mut cache = TRAFFIC_CONTROL_CACHE.lock().unwrap(); diff --git a/src/cached_traffic_control.rs b/src/cached_traffic_control.rs index ba552215..2f077d36 100644 --- a/src/cached_traffic_control.rs +++ b/src/cached_traffic_control.rs @@ -305,15 +305,10 @@ impl TrafficControlCache { pub fn lookup_rate_limiter( &self, - peer: Option, + peer: SocketAddr, now: i64, ) -> (&str, Option>, Option>) { - let peer = match peer { - None => return ("", None, None), - Some(peer) => peer, - }; - let peer_ip = cannonical_ip(peer.ip()); log::debug!("lookup_rate_limiter: {:?}", peer_ip); @@ -427,32 +422,27 @@ rule: somewhere let private = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(192, 168, 2, 35)), 1234); let somewhere = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(1, 2, 3, 4)), 1234); - let (rule, read_limiter, write_limiter) = cache.lookup_rate_limiter(None, THURSDAY_80_00); - assert_eq!(rule, ""); - assert!(read_limiter.is_none()); - assert!(write_limiter.is_none()); - - let (rule, read_limiter, write_limiter) = cache.lookup_rate_limiter(Some(somewhere), THURSDAY_80_00); + let (rule, read_limiter, write_limiter) = cache.lookup_rate_limiter(somewhere, THURSDAY_80_00); assert_eq!(rule, "somewhere"); assert!(read_limiter.is_some()); assert!(write_limiter.is_some()); - let (rule, read_limiter, write_limiter) = cache.lookup_rate_limiter(Some(local), THURSDAY_19_00); + let (rule, read_limiter, write_limiter) = cache.lookup_rate_limiter(local, THURSDAY_19_00); assert_eq!(rule, "rule2"); assert!(read_limiter.is_some()); assert!(write_limiter.is_some()); - let (rule, read_limiter, write_limiter) = cache.lookup_rate_limiter(Some(gateway), THURSDAY_15_00); + let (rule, read_limiter, write_limiter) = cache.lookup_rate_limiter(gateway, THURSDAY_15_00); assert_eq!(rule, "rule1"); assert!(read_limiter.is_some()); assert!(write_limiter.is_some()); - let (rule, read_limiter, write_limiter) = cache.lookup_rate_limiter(Some(gateway), THURSDAY_19_00); + let (rule, read_limiter, write_limiter) = cache.lookup_rate_limiter(gateway, THURSDAY_19_00); assert_eq!(rule, "somewhere"); assert!(read_limiter.is_some()); assert!(write_limiter.is_some()); - let (rule, read_limiter, write_limiter) = cache.lookup_rate_limiter(Some(private), THURSDAY_19_00); + let (rule, read_limiter, write_limiter) = cache.lookup_rate_limiter(private, THURSDAY_19_00); assert_eq!(rule, "rule2"); assert!(read_limiter.is_some()); assert!(write_limiter.is_some()); -- 2.30.2