|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use std::time::Duration; |
| 19 | + |
| 20 | +use super::Layer; |
| 21 | +use super::PythonLayer; |
| 22 | +use crate::*; |
| 23 | +use opendal::Operator; |
| 24 | + |
| 25 | +/// Parse a timeout given in seconds into a [`Duration`]. |
| 26 | +/// |
| 27 | +/// Rejects non-positive, non-finite, or out-of-range values. A zero timeout |
| 28 | +/// is rejected because it would make every operation time out immediately. |
| 29 | +fn parse_timeout_secs(name: &str, secs: f64) -> PyResult<Duration> { |
| 30 | + if !secs.is_finite() || secs <= 0.0 { |
| 31 | + return Err(ConfigInvalid::new_err(format!( |
| 32 | + "{name} must be a positive, finite number of seconds" |
| 33 | + ))); |
| 34 | + } |
| 35 | + Duration::try_from_secs_f64(secs).map_err(|_| { |
| 36 | + ConfigInvalid::new_err(format!( |
| 37 | + "{name} must be a positive, finite number of seconds" |
| 38 | + )) |
| 39 | + }) |
| 40 | +} |
| 41 | + |
| 42 | +/// A layer that adds timeouts to operations. |
| 43 | +/// |
| 44 | +/// Timeouts prevent slow or stalled work from hanging indefinitely, for |
| 45 | +/// example when a TCP connection stops emitting IO events. Control |
| 46 | +/// operations (such as `stat` and `delete`) and IO operations (such as |
| 47 | +/// `read` and `write`) are bounded by separate timeouts. |
| 48 | +/// |
| 49 | +/// Notes |
| 50 | +/// ----- |
| 51 | +/// A small amount of overhead is added to IO operations to implement the |
| 52 | +/// timeout correctly. |
| 53 | +#[pyclass(module = "opendal.layers", extends = Layer, skip_from_py_object)] |
| 54 | +#[derive(Clone)] |
| 55 | +pub struct TimeoutLayer(ocore::layers::TimeoutLayer); |
| 56 | + |
| 57 | +impl PythonLayer for TimeoutLayer { |
| 58 | + fn layer(&self, op: Operator) -> Operator { |
| 59 | + op.layer(self.0.clone()) |
| 60 | + } |
| 61 | +} |
| 62 | +#[pymethods] |
| 63 | +impl TimeoutLayer { |
| 64 | + /// Create a new TimeoutLayer. |
| 65 | + /// |
| 66 | + /// Parameters |
| 67 | + /// ---------- |
| 68 | + /// timeout : Optional[float] |
| 69 | + /// Timeout (in seconds) for control operations like ``stat`` and |
| 70 | + /// ``delete``. Must be a positive, finite number. A value of ``0`` |
| 71 | + /// is rejected because it would make every operation time out |
| 72 | + /// immediately. Defaults to ``60.0``. |
| 73 | + /// io_timeout : Optional[float] |
| 74 | + /// Timeout (in seconds) for IO operations like ``read`` and |
| 75 | + /// ``write``. Must be a positive, finite number. A value of ``0`` |
| 76 | + /// is rejected because it would make every operation time out |
| 77 | + /// immediately. Defaults to ``10.0``. |
| 78 | + /// |
| 79 | + /// Returns |
| 80 | + /// ------- |
| 81 | + /// TimeoutLayer |
| 82 | + /// |
| 83 | + /// Raises |
| 84 | + /// ------ |
| 85 | + /// ConfigInvalid |
| 86 | + /// If ``timeout`` or ``io_timeout`` is out of range. |
| 87 | + #[new] |
| 88 | + #[pyo3(signature = (timeout = None, io_timeout = None))] |
| 89 | + fn new(timeout: Option<f64>, io_timeout: Option<f64>) -> PyResult<PyClassInitializer<Self>> { |
| 90 | + let mut timeout_layer = ocore::layers::TimeoutLayer::default(); |
| 91 | + if let Some(timeout) = timeout { |
| 92 | + let timeout = parse_timeout_secs("timeout", timeout)?; |
| 93 | + timeout_layer = timeout_layer.with_timeout(timeout); |
| 94 | + } |
| 95 | + if let Some(io_timeout) = io_timeout { |
| 96 | + let io_timeout = parse_timeout_secs("io_timeout", io_timeout)?; |
| 97 | + timeout_layer = timeout_layer.with_io_timeout(io_timeout); |
| 98 | + } |
| 99 | + |
| 100 | + let timeout_layer = Self(timeout_layer); |
| 101 | + let class = PyClassInitializer::from(Layer(Box::new(timeout_layer.clone()))) |
| 102 | + .add_subclass(timeout_layer); |
| 103 | + |
| 104 | + Ok(class) |
| 105 | + } |
| 106 | +} |
0 commit comments