Skip to content

Commit 9c851ec

Browse files
committed
replace dep once_cell with std library equivalent lazylock/oncelock
1 parent a6dbd1a commit 9c851ec

File tree

33 files changed

+114
-110
lines changed

33 files changed

+114
-110
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ derivative = "2.2.0"
3737
http = "1.0.0"
3838
log = "0.4"
3939
h2 = ">=0.4.11"
40-
once_cell = "1"
4140
lru = "0.14"
4241
ahash = ">=0.8.9"
4342

docs/user_guide/rate_limiter.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ Pingora provides a crate `pingora-limits` which provides a simple and easy to us
77
async-trait="0.1"
88
pingora = { version = "0.3", features = [ "lb" ] }
99
pingora-limits = "0.3.0"
10-
once_cell = "1.19.0"
1110
```
1211
2. Declare a global rate limiter map to store the rate limiter for each client. In this example, we use `appid`.
1312
3. Override the `request_filter` method in the `ProxyHttp` trait to implement rate limiting.
@@ -19,7 +18,7 @@ Pingora provides a crate `pingora-limits` which provides a simple and easy to us
1918
## Example
2019
```rust
2120
use async_trait::async_trait;
22-
use once_cell::sync::Lazy;
21+
use std::sync::LazyLock;
2322
use pingora::http::ResponseHeader;
2423
use pingora::prelude::*;
2524
use pingora_limits::rate::Rate;
@@ -67,7 +66,7 @@ impl LB {
6766
}
6867

6968
// Rate limiter
70-
static RATE_LIMITER: Lazy<Rate> = Lazy::new(|| Rate::new(Duration::from_secs(1)));
69+
static RATE_LIMITER: LazyLock<Rate> = LazyLock::new(|| Rate::new(Duration::from_secs(1)));
7170

7271
// max request per second per client
7372
static MAX_REQ_PER_SEC: isize = 1;

pingora-cache/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ pingora-lru = { version = "0.6.0", path = "../pingora-lru" }
2525
pingora-timeout = { version = "0.6.0", path = "../pingora-timeout" }
2626
http = { workspace = true }
2727
indexmap = "1"
28-
once_cell = { workspace = true }
2928
regex = "1"
3029
blake2 = "0.10"
3130
serde = { version = "1.0", features = ["derive"] }

pingora-cache/src/cache_control.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ use super::*;
1919
use http::header::HeaderName;
2020
use http::HeaderValue;
2121
use indexmap::IndexMap;
22-
use once_cell::sync::Lazy;
2322
use pingora_error::{Error, ErrorType};
2423
use regex::bytes::Regex;
2524
use std::num::IntErrorKind;
2625
use std::slice;
2726
use std::str;
27+
use std::sync::LazyLock;
2828

2929
/// The max delta-second per [RFC 9111](https://datatracker.ietf.org/doc/html/rfc9111#section-1.2.2)
3030
// "If a cache receives a delta-seconds value
@@ -157,13 +157,13 @@ impl<'a> Iterator for ListValueIter<'a> {
157157
// note the `token` implementation excludes disallowed ASCII ranges
158158
// and disallowed delimiters: https://datatracker.ietf.org/doc/html/rfc9110#section-5.6.2
159159
// though it does not forbid `obs-text`: %x80-FF
160-
static RE_CACHE_DIRECTIVE: Lazy<Regex> =
160+
static RE_CACHE_DIRECTIVE: LazyLock<Regex> =
161161
// to break our version down further:
162162
// `(?-u)`: unicode support disabled, which puts the regex into "ASCII compatible mode" for specifying literal bytes like \x7F: https://docs.rs/regex/1.10.4/regex/bytes/index.html#syntax
163163
// `(?:^|(?:\s*[,;]\s*)`: allow either , or ; as a delimiter
164164
// `([^\x00-\x20\(\)<>@,;:\\"/\[\]\?=\{\}\x7F]+)`: token (directive name capture group)
165165
// `(?:=((?:[^\x00-\x20\(\)<>@,;:\\"/\[\]\?=\{\}\x7F]+|(?:"(?:[^"\\]|\\.)*"))))`: token OR quoted-string (directive value capture-group)
166-
Lazy::new(|| {
166+
LazyLock::new(|| {
167167
Regex::new(r#"(?-u)(?:^|(?:\s*[,;]\s*))([^\x00-\x20\(\)<>@,;:\\"/\[\]\?=\{\}\x7F]+)(?:=((?:[^\x00-\x20\(\)<>@,;:\\"/\[\]\?=\{\}\x7F]+|(?:"(?:[^"\\]|\\.)*"))))?"#).unwrap()
168168
});
169169

pingora-cache/src/memory.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ impl Storage for MemCache {
427427
mod test {
428428
use super::*;
429429
use cf_rustracing::span::Span;
430-
use once_cell::sync::Lazy;
430+
use std::sync::LazyLock;
431431

432432
fn gen_meta() -> CacheMeta {
433433
let mut header = ResponseHeader::build(200, None).unwrap();
@@ -445,7 +445,7 @@ mod test {
445445

446446
#[tokio::test]
447447
async fn test_write_then_read() {
448-
static MEM_CACHE: Lazy<MemCache> = Lazy::new(MemCache::new);
448+
static MEM_CACHE: LazyLock<MemCache> = LazyLock::new(MemCache::new);
449449
let span = &Span::inactive().handle();
450450

451451
let key1 = CacheKey::new("", "a", "1");
@@ -482,7 +482,7 @@ mod test {
482482

483483
#[tokio::test]
484484
async fn test_read_range() {
485-
static MEM_CACHE: Lazy<MemCache> = Lazy::new(MemCache::new);
485+
static MEM_CACHE: LazyLock<MemCache> = LazyLock::new(MemCache::new);
486486
let span = &Span::inactive().handle();
487487

488488
let key1 = CacheKey::new("", "a", "1");
@@ -527,7 +527,7 @@ mod test {
527527
async fn test_write_while_read() {
528528
use futures::FutureExt;
529529

530-
static MEM_CACHE: Lazy<MemCache> = Lazy::new(MemCache::new);
530+
static MEM_CACHE: LazyLock<MemCache> = LazyLock::new(MemCache::new);
531531
let span = &Span::inactive().handle();
532532

533533
let key1 = CacheKey::new("", "a", "1");
@@ -594,7 +594,7 @@ mod test {
594594

595595
#[tokio::test]
596596
async fn test_purge_partial() {
597-
static MEM_CACHE: Lazy<MemCache> = Lazy::new(MemCache::new);
597+
static MEM_CACHE: LazyLock<MemCache> = LazyLock::new(MemCache::new);
598598
let cache = &MEM_CACHE;
599599

600600
let key = CacheKey::new("", "a", "1").to_compact();
@@ -621,7 +621,7 @@ mod test {
621621

622622
#[tokio::test]
623623
async fn test_purge_complete() {
624-
static MEM_CACHE: Lazy<MemCache> = Lazy::new(MemCache::new);
624+
static MEM_CACHE: LazyLock<MemCache> = LazyLock::new(MemCache::new);
625625
let cache = &MEM_CACHE;
626626

627627
let key = CacheKey::new("", "a", "1").to_compact();

pingora-cache/src/meta.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
1717
pub use http::Extensions;
1818
use log::warn;
19-
use once_cell::sync::{Lazy, OnceCell};
2019
use pingora_error::{Error, ErrorType::*, OrErr, Result};
2120
use pingora_header_serde::HeaderSerde;
2221
use pingora_http::{HMap, ResponseHeader};
2322
use serde::{Deserialize, Serialize};
2423
use std::borrow::Cow;
24+
use std::sync::{LazyLock, OnceLock};
2525
use std::time::{Duration, SystemTime};
2626

2727
use crate::key::HashBinary;
@@ -568,9 +568,9 @@ impl CacheMetaDefaults {
568568
/// The dictionary content for header compression.
569569
///
570570
/// Used during initialization of [`HEADER_SERDE`].
571-
static COMPRESSION_DICT_CONTENT: OnceCell<Cow<'static, [u8]>> = OnceCell::new();
571+
static COMPRESSION_DICT_CONTENT: OnceLock<Cow<'static, [u8]>> = OnceLock::new();
572572

573-
static HEADER_SERDE: Lazy<HeaderSerde> = Lazy::new(|| {
573+
static HEADER_SERDE: LazyLock<HeaderSerde> = LazyLock::new(|| {
574574
let dict_opt = if let Some(dict_content) = COMPRESSION_DICT_CONTENT.get() {
575575
Some(dict_content.to_vec())
576576
} else {

pingora-cache/src/put.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ impl<C: CachePut> CachePutCtx<C> {
215215
mod test {
216216
use super::*;
217217
use cf_rustracing::span::Span;
218-
use once_cell::sync::Lazy;
218+
use std::sync::LazyLock;
219219

220220
struct TestCachePut();
221221
impl CachePut for TestCachePut {
@@ -227,7 +227,7 @@ mod test {
227227
}
228228

229229
type TestCachePutCtx = CachePutCtx<TestCachePut>;
230-
static CACHE_BACKEND: Lazy<MemCache> = Lazy::new(MemCache::new);
230+
static CACHE_BACKEND: LazyLock<MemCache> = LazyLock::new(MemCache::new);
231231

232232
#[tokio::test]
233233
async fn test_cache_put() {

pingora-core/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ log = { workspace = true }
3838
h2 = { workspace = true }
3939
derivative.workspace = true
4040
clap = { version = "3.2.25", features = ["derive"] }
41-
once_cell = { workspace = true }
4241
serde = { version = "1.0", features = ["derive"] }
4342
serde_yaml = "0.8"
4443
strum = "0.26.2"

pingora-core/src/connectors/offload.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
// limitations under the License.
1414

1515
use log::debug;
16-
use once_cell::sync::OnceCell;
1716
use rand::Rng;
17+
use std::sync::OnceLock;
1818
use tokio::runtime::{Builder, Handle};
1919
use tokio::sync::oneshot::{channel, Sender};
2020

@@ -25,7 +25,7 @@ pub(crate) struct OffloadRuntime {
2525
thread_per_shard: usize,
2626
// Lazily init the runtimes so that they are created after pingora
2727
// daemonize itself. Otherwise the runtime threads are lost.
28-
pools: OnceCell<Box<[(Handle, Sender<()>)]>>,
28+
pools: OnceLock<Box<[(Handle, Sender<()>)]>>,
2929
}
3030

3131
impl OffloadRuntime {
@@ -35,7 +35,7 @@ impl OffloadRuntime {
3535
OffloadRuntime {
3636
shards,
3737
thread_per_shard,
38-
pools: OnceCell::new(),
38+
pools: OnceLock::new(),
3939
}
4040
}
4141

pingora-core/src/modules/http/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ pub mod grpc_web;
2626
use async_trait::async_trait;
2727
use bytes::Bytes;
2828
use http::HeaderMap;
29-
use once_cell::sync::OnceCell;
3029
use pingora_error::Result;
3130
use pingora_http::{RequestHeader, ResponseHeader};
3231
use std::any::Any;
3332
use std::any::TypeId;
3433
use std::collections::HashMap;
3534
use std::sync::Arc;
35+
use std::sync::OnceLock;
3636

3737
/// The trait an HTTP traffic module needs to implement
3838
#[async_trait]
@@ -101,15 +101,15 @@ pub type ModuleBuilder = Box<dyn HttpModuleBuilder + 'static + Send + Sync>;
101101
/// The object to hold multiple http modules
102102
pub struct HttpModules {
103103
modules: Vec<ModuleBuilder>,
104-
module_index: OnceCell<Arc<HashMap<TypeId, usize>>>,
104+
module_index: OnceLock<Arc<HashMap<TypeId, usize>>>,
105105
}
106106

107107
impl HttpModules {
108108
/// Create a new [HttpModules]
109109
pub fn new() -> Self {
110110
HttpModules {
111111
modules: vec![],
112-
module_index: OnceCell::new(),
112+
module_index: OnceLock::new(),
113113
}
114114
}
115115

0 commit comments

Comments
 (0)