warner/wireguard-vanity-address

stop process at broken pipe

Closed this issue · 5 comments

the process still tries to generate keys once the downstream stdout pipe is broken.
it seems more ideal to halt the process after a broken pipe.

here is an easy way to test:
wireguard-vanity-address foo | head -n 5

Ouch, yeah, that's a good idea

Any idea how to accomplish that? I don't know how to manage signals like EPIPE in Rust.

Hm, looks like we want to do writeln! instead of println! and react to the io::Error it can return. I think we'll need to move the printing from lib.rs trial() into bin.rs main(), to avoid trying to share a mutable io::stdout among the multiple Rayon threads.

Ok, I think I got it. I've moved the printing into a central place, and I'm using .try_for_each(print) at the end of the iterator chain to stop after the first error (and rayon takes care of stopping the other threads, yay). The process exits with an error that mentions the EPIPE:

% cargo run --release hello |head -5
    Finished release [optimized] target(s) in 0.02s
     Running `target/release/wireguard-vanity-address hello`
prefix: hello, expect 6710886 trials, Ctrl-C to stop
private wCURVpa7ibknB+WLQEhz2P3oMBGdb2HsAC++hhbuJ0Y=  public yHeLlo2bUP+NtqKXN48DIczf9teE6x2NvRCqKU8jf3c=
private 0BMw58SABQaw9awKDzk/ogaywpie67WoPVuFve+aaX8=  public 8jwEhelLOAtFVBz4U0IgHLNSUp6zH54ThFGt/Zhd1zc=
private wMPEqSdLZyls9/ArRoVCtmKCilSBrDdcQQznQ0ciFn0=  public HeLloO9sYgpsdIplilAFqboE6fv4P6T6cbNpxix292s=
private UN8R03aCbjMqDp5PDcR88EsBbTFpkJDOebovoYDHmXc=  public zHELlo2vtLEosVM/x1RR1IqwV90lQZTYxEOUa3hAbzA=
Error: Os { code: 32, kind: BrokenPipe, message: "Broken pipe" }
%

this is working great, thanks.
here is how i use it in scripts now: wireguard-vanity-address foo |& grep private | head -n 10