Skip to content

Commit

Permalink
feat(bindings/python): export ConcurrentLimitLayer
Browse files Browse the repository at this point in the history
  • Loading branch information
TennyZhuang committed Sep 24, 2024
1 parent 3947f6d commit 38d9255
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion bindings/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async def main():
asyncio.run(main())
```

s3 service example:
s3 service example:
```python
import opendal

Expand Down
4 changes: 4 additions & 0 deletions bindings/python/python/opendal/layers.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ class RetryLayer(Layer):
max_delay: Optional[float] = None,
min_delay: Optional[float] = None,
) -> None: ...

@final
class ConcurrentLimitLayer(Layer):
def __init__(self, limit: int) -> None: ...
23 changes: 23 additions & 0 deletions bindings/python/src/layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,26 @@ impl RetryLayer {
Ok(class)
}
}

#[pyclass(module = "opendal.layers", extends=Layer)]
#[derive(Clone)]
pub struct ConcurrentLimitLayer(ocore::layers::ConcurrentLimitLayer);

impl PythonLayer for ConcurrentLimitLayer {
fn layer(&self, op: Operator) -> Operator {
op.layer(self.0.clone())
}
}

#[pymethods]
impl ConcurrentLimitLayer {
#[new]
#[pyo3(signature = (limit))]
fn new(limit: usize) -> PyResult<PyClassInitializer<Self>> {
let concurrent_limit = Self(ocore::layers::ConcurrentLimitLayer::new(limit));
let class = PyClassInitializer::from(Layer(Box::new(concurrent_limit.clone())))
.add_subclass(concurrent_limit);

Ok(class)
}
}
1 change: 1 addition & 0 deletions bindings/python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ fn _opendal(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
let layers_module = PyModule::new_bound(py, "layers")?;
layers_module.add_class::<Layer>()?;
layers_module.add_class::<RetryLayer>()?;
layers_module.add_class::<ConcurrentLimitLayer>()?;
m.add_submodule(&layers_module)?;
py.import_bound("sys")?
.getattr("modules")?
Expand Down
6 changes: 4 additions & 2 deletions bindings/python/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ def setup_config(service_name):

@pytest.fixture(scope="session")
def async_operator(service_name, setup_config):
return opendal.AsyncOperator(service_name, **setup_config).layer(
opendal.layers.RetryLayer()
return (
opendal.AsyncOperator(service_name, **setup_config)
.layer(opendal.layers.RetryLayer())
.layer(opendal.layers.ConcurrentLimitLayer(1024))
)


Expand Down

0 comments on commit 38d9255

Please sign in to comment.