]> git.scottworley.com Git - tattlekey/commitdiff
server: Bind port before messing with log files as a lock
authorScott Worley <scottworley@scottworley.com>
Tue, 10 Oct 2023 23:57:22 +0000 (16:57 -0700)
committerScott Worley <scottworley@scottworley.com>
Wed, 11 Oct 2023 01:50:46 +0000 (18:50 -0700)
Rather than messing with flock, fnctl-F_SETLK, etc., just bind the port
before touching the log file & let the failure to bind the port keep
two instances of the server from stepping on each others' log i/o.

server/src/main.rs

index 5edadbb9bc3b671244e46f6d24dcb1f471958ab0..e7351db4ffa17f70a8c3ad9617b23155b09894b1 100644 (file)
@@ -93,9 +93,9 @@ fn open_log_for_writing() -> csv::Writer<std::fs::File> {
 }
 
 fn main() {
-    let mut log = open_log_for_writing();
-    let mut presses = HashMap::<MessageKey, Range>::new();
     let socket = UdpSocket::bind("0.0.0.0:29803").expect("couldn't bind to address");
+    let mut presses = HashMap::<MessageKey, Range>::new();
+    let mut log = open_log_for_writing();
     loop {
         let mut buf = [0; MESSAGE_SIZE];
         match socket.recv_from(&mut buf) {