Skip to content

Commit 593b080

Browse files
authored
feat(bindings/python): export ConcurrentLimitLayer (#5140)
1 parent 3947f6d commit 593b080

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

bindings/python/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ async def main():
4141
asyncio.run(main())
4242
```
4343

44-
s3 service example:
44+
s3 service example:
4545
```python
4646
import opendal
4747

bindings/python/python/opendal/layers.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ class RetryLayer(Layer):
3030
max_delay: Optional[float] = None,
3131
min_delay: Optional[float] = None,
3232
) -> None: ...
33+
34+
@final
35+
class ConcurrentLimitLayer(Layer):
36+
def __init__(self, limit: int) -> None: ...

bindings/python/src/layers.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,26 @@ impl RetryLayer {
8080
Ok(class)
8181
}
8282
}
83+
84+
#[pyclass(module = "opendal.layers", extends=Layer)]
85+
#[derive(Clone)]
86+
pub struct ConcurrentLimitLayer(ocore::layers::ConcurrentLimitLayer);
87+
88+
impl PythonLayer for ConcurrentLimitLayer {
89+
fn layer(&self, op: Operator) -> Operator {
90+
op.layer(self.0.clone())
91+
}
92+
}
93+
94+
#[pymethods]
95+
impl ConcurrentLimitLayer {
96+
#[new]
97+
#[pyo3(signature = (limit))]
98+
fn new(limit: usize) -> PyResult<PyClassInitializer<Self>> {
99+
let concurrent_limit = Self(ocore::layers::ConcurrentLimitLayer::new(limit));
100+
let class = PyClassInitializer::from(Layer(Box::new(concurrent_limit.clone())))
101+
.add_subclass(concurrent_limit);
102+
103+
Ok(class)
104+
}
105+
}

bindings/python/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ fn _opendal(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
9090
let layers_module = PyModule::new_bound(py, "layers")?;
9191
layers_module.add_class::<Layer>()?;
9292
layers_module.add_class::<RetryLayer>()?;
93+
layers_module.add_class::<ConcurrentLimitLayer>()?;
9394
m.add_submodule(&layers_module)?;
9495
py.import_bound("sys")?
9596
.getattr("modules")?

bindings/python/tests/conftest.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ def setup_config(service_name):
6060

6161
@pytest.fixture(scope="session")
6262
def async_operator(service_name, setup_config):
63-
return opendal.AsyncOperator(service_name, **setup_config).layer(
64-
opendal.layers.RetryLayer()
63+
return (
64+
opendal.AsyncOperator(service_name, **setup_config)
65+
.layer(opendal.layers.RetryLayer())
66+
.layer(opendal.layers.ConcurrentLimitLayer(1024))
6567
)
6668

6769

0 commit comments

Comments
 (0)