This is useful for graceful shutdown of a service. When e.g. SIGTERM is sent to your service just stopping the process is pretty rough. dbus-daemon seems to handle it well enough but it would be nice to actually close the connection properly. For that the reading should be interruptable.
This could be done by using the good ol' reliable self-pipe trick and using select() to wait for data before using a blocking recvmsg. This should also provide a way to send a code so the error handling on the reading call-site knows what's what.
The read loop would look something like this:
loop {
match conn.get_next_message() {
Err(Error::Interrupted(code)) => {/* probably want to return, depending on the user defined code*/}
Err(error) => {/*other error handling*/}
Ok(msg) => {/*message handling*/}
}
}
When creating a conn, a second (cloneable) struct should be generated, that allows to interrupt reads on the conn at any time.
This will contain a Rc<FdWrapper> for the write end of the self-pipe. All it would do is writing the code to the pipe.
FdWrapper will close the contained RawFd on drop.
const INTR_SHUTDOWN: u8 = 0xFF;
conn_interrupter.interrupt_read(INTR_SHUTDOWN).expect("Can't even send interrupts to the conn anymore :((");
This is useful for graceful shutdown of a service. When e.g. SIGTERM is sent to your service just stopping the process is pretty rough. dbus-daemon seems to handle it well enough but it would be nice to actually close the connection properly. For that the reading should be interruptable.
This could be done by using the good ol' reliable self-pipe trick and using
select()to wait for data before using a blockingrecvmsg. This should also provide a way to send a code so the error handling on the reading call-site knows what's what.The read loop would look something like this:
When creating a conn, a second (cloneable) struct should be generated, that allows to interrupt reads on the conn at any time.
This will contain a
Rc<FdWrapper>for the write end of the self-pipe. All it would do is writing the code to the pipe.FdWrapperwill close the containedRawFdon drop.