| 1 | // Copyright 2024 Lance Developers. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | use napi_derive::*; |
| 16 | |
| 17 | /// Timeout configuration for remote HTTP client. |
| 18 | #[napi(object)] |
| 19 | #[derive(Debug)] |
| 20 | pub struct TimeoutConfig { |
| 21 | /// The timeout for establishing a connection in seconds. Default is 120 |
| 22 | /// seconds (2 minutes). This can also be set via the environment variable |
| 23 | /// `LANCE_CLIENT_CONNECT_TIMEOUT`, as an integer number of seconds. |
| 24 | pub connect_timeout: Option<f64>, |
| 25 | /// The timeout for reading data from the server in seconds. Default is 300 |
| 26 | /// seconds (5 minutes). This can also be set via the environment variable |
| 27 | /// `LANCE_CLIENT_READ_TIMEOUT`, as an integer number of seconds. |
| 28 | pub read_timeout: Option<f64>, |
| 29 | /// The timeout for keeping idle connections in the connection pool in seconds. |
| 30 | /// Default is 300 seconds (5 minutes). This can also be set via the |
| 31 | /// environment variable `LANCE_CLIENT_CONNECTION_TIMEOUT`, as an integer |
| 32 | /// number of seconds. |
| 33 | pub pool_idle_timeout: Option<f64>, |
| 34 | } |
| 35 | |
| 36 | /// Retry configuration for the remote HTTP client. |
| 37 | #[napi(object)] |
| 38 | #[derive(Debug)] |
| 39 | pub struct RetryConfig { |
| 40 | /// The maximum number of retries for a request. Default is 3. You can also |
| 41 | /// set this via the environment variable `LANCE_CLIENT_MAX_RETRIES`. |
| 42 | pub retries: Option<u8>, |
| 43 | /// The maximum number of retries for connection errors. Default is 3. You |
| 44 | /// can also set this via the environment variable `LANCE_CLIENT_CONNECT_RETRIES`. |
| 45 | pub connect_retries: Option<u8>, |
| 46 | /// The maximum number of retries for read errors. Default is 3. You can also |
| 47 | /// set this via the environment variable `LANCE_CLIENT_READ_RETRIES`. |
| 48 | pub read_retries: Option<u8>, |
| 49 | /// The backoff factor to apply between retries. Default is 0.25. Between each retry |
| 50 | /// the client will wait for the amount of seconds: |
| 51 | /// `{backoff factor} * (2 ** ({number of previous retries}))`. So for the default |
| 52 | /// of 0.25, the first retry will wait 0.25 seconds, the second retry will wait 0.5 |
| 53 | /// seconds, the third retry will wait 1 second, etc. |
| 54 | /// |
| 55 | /// You can also set this via the environment variable |
| 56 | /// `LANCE_CLIENT_RETRY_BACKOFF_FACTOR`. |
| 57 | pub backoff_factor: Option<f64>, |
| 58 | /// The jitter to apply to the backoff factor, in seconds. Default is 0.25. |
| 59 | /// |
| 60 | /// A random value between 0 and `backoff_jitter` will be added to the backoff |
| 61 | /// factor in seconds. So for the default of 0.25 seconds, between 0 and 250 |
| 62 | /// milliseconds will be added to the sleep between each retry. |
| 63 | /// |
| 64 | /// You can also set this via the environment variable |
| 65 | /// `LANCE_CLIENT_RETRY_BACKOFF_JITTER`. |
| 66 | pub backoff_jitter: Option<f64>, |
| 67 | /// The HTTP status codes for which to retry the request. Default is |
| 68 | /// [429, 500, 502, 503]. |
| 69 | /// |
| 70 | /// You can also set this via the environment variable |
| 71 | /// `LANCE_CLIENT_RETRY_STATUSES`. Use a comma-separated list of integers. |
| 72 | pub statuses: Option<Vec<u16>>, |
| 73 | } |
| 74 | |
| 75 | #[napi(object)] |
| 76 | #[derive(Debug, Default)] |
| 77 | pub struct ClientConfig { |
| 78 | pub user_agent: Option<String>, |
| 79 | pub retry_config: Option<RetryConfig>, |
| 80 | pub timeout_config: Option<TimeoutConfig>, |
| 81 | } |
| 82 | |
| 83 | impl From<TimeoutConfig> for lancedb::remote::TimeoutConfig { |
| 84 | fn from(config: TimeoutConfig) -> Self { |
| 85 | Self { |
| 86 | connect_timeout: config |
| 87 | .connect_timeout |
| 88 | .map(std::time::Duration::from_secs_f64), |
| 89 | read_timeout: config.read_timeout.map(std::time::Duration::from_secs_f64), |
| 90 | pool_idle_timeout: config |
| 91 | .pool_idle_timeout |
| 92 | .map(std::time::Duration::from_secs_f64), |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | impl From<RetryConfig> for lancedb::remote::RetryConfig { |
| 98 | fn from(config: RetryConfig) -> Self { |
| 99 | Self { |
| 100 | retries: config.retries, |
| 101 | connect_retries: config.connect_retries, |
| 102 | read_retries: config.read_retries, |
| 103 | backoff_factor: config.backoff_factor.map(|v| v as f32), |
| 104 | backoff_jitter: config.backoff_jitter.map(|v| v as f32), |
| 105 | statuses: config.statuses, |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | impl From<ClientConfig> for lancedb::remote::ClientConfig { |
| 111 | fn from(config: ClientConfig) -> Self { |
| 112 | Self { |
| 113 | user_agent: config |
| 114 | .user_agent |
| 115 | .unwrap_or(concat!("LanceDB-Node-Client/", env!("CARGO_PKG_VERSION")).to_string()), |
| 116 | retry_config: config.retry_config.map(Into::into).unwrap_or_default(), |
| 117 | timeout_config: config.timeout_config.map(Into::into).unwrap_or_default(), |
| 118 | } |
| 119 | } |
| 120 | } |