diff options
author | Matthias Beyer <matthias.beyer@ifm.com> | 2022-09-10 09:59:27 +0200 |
---|---|---|
committer | Matthias Beyer <matthias.beyer@ifm.com> | 2022-09-10 12:13:33 +0200 |
commit | 9b74aa41d802b27e69e742c0306b633ad0ff3f89 (patch) | |
tree | 060cc22c17462b6fc33c91a4cfe3891572b9440f | |
parent | 688ac4af52ffd6a8925f5685adc4b8bb67347c1e (diff) |
Refactor: Error should indicate how many handles are received
This patch refactors the error handling if there are too few handles
available by returning an error that indicates how many handles there
are left.
Signed-off-by: Matthias Beyer <matthias.beyer@ifm.com>
-rw-r--r-- | plugins/plugin_fdman/src/error.rs | 4 | ||||
-rw-r--r-- | plugins/plugin_fdman/src/plugin.rs | 49 |
2 files changed, 34 insertions, 19 deletions
diff --git a/plugins/plugin_fdman/src/error.rs b/plugins/plugin_fdman/src/error.rs index 77e63b37..0d95b50f 100644 --- a/plugins/plugin_fdman/src/error.rs +++ b/plugins/plugin_fdman/src/error.rs @@ -9,6 +9,6 @@ pub enum Error { #[error("Sending reply message failed")] SendingReply, - #[error("Out of file handles")] - NoHandleLeft, + #[error("Insufficient handles: required {required}, received {received}")] + InsufficientHandles { required: u64, received: u64 }, } diff --git a/plugins/plugin_fdman/src/plugin.rs b/plugins/plugin_fdman/src/plugin.rs index 5c02c37f..de36855c 100644 --- a/plugins/plugin_fdman/src/plugin.rs +++ b/plugins/plugin_fdman/src/plugin.rs @@ -26,22 +26,25 @@ impl FdManPlugin { } } - fn aquire_handle(&self) -> Result<FdHandle, Error> { + fn aquire_handles(&self, count: u64) -> Result<FdHandles, Error> { let old_value = self .currently_held_count - .fetch_add(1, std::sync::atomic::Ordering::SeqCst); + .fetch_add(count, std::sync::atomic::Ordering::SeqCst); if old_value >= self.max_fds { self.currently_held_count - .fetch_sub(1, std::sync::atomic::Ordering::SeqCst); - Err(Error::NoHandleLeft) + .fetch_sub(count, std::sync::atomic::Ordering::SeqCst); + Err(Error::InsufficientHandles { + required: count, + received: (old_value - self.max_fds), + }) } else { - Ok(FdHandle) + Ok(FdHandles(count)) } } } -struct FdHandle; +struct FdHandles(u64); impl tedge_api::plugin::PluginDeclaration for FdManPlugin { type HandledMessages = (OpenOptions,); @@ -57,8 +60,23 @@ impl Handle<OpenOptions> for FdManPlugin { message: OpenOptions, sender: tedge_api::address::ReplySenderFor<OpenOptions>, ) -> Result<(), PluginError> { - match self.aquire_handle() { - Ok(FdHandle) => { + let handles_to_aquire = 1; + match self.aquire_handles(handles_to_aquire) { + Ok(FdHandles(aquired_handles)) => { + if aquired_handles != handles_to_aquire { + let err = Error::InsufficientHandles { + required: handles_to_aquire, + received: aquired_handles, + }; + return sender + .reply(OpenOptionsResult::new( + message, + Err(OpenOptionsError::from(err)), + )) + .map_err(|_| Error::SendingReply) + .map_err(PluginError::from) + } + let file_result = message .as_std() .open(message.path()) @@ -76,16 +94,13 @@ impl Handle<OpenOptions> for FdManPlugin { .map_err(|_| Error::SendingReply) .map_err(PluginError::from) } - Err(Error::NoHandleLeft) => { - let res = OpenOptionsResult::new( + Err(err @ Error::InsufficientHandles { .. }) => sender + .reply(OpenOptionsResult::new( message, - Err(OpenOptionsError::from(Error::NoHandleLeft)), - ); - sender - .reply(res) - .map_err(|_| Error::SendingReply) - .map_err(PluginError::from) - } + Err(OpenOptionsError::from(err)), + )) + .map_err(|_| Error::SendingReply) + .map_err(PluginError::from), Err(other) => Err(PluginError::from(other)), } } |