repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

commit
b9567a1
parent
9e5b256
author
Eric Bower
date
2024-09-24 15:07:50 +0000 UTC
refactor(pubsub): default timeout set to 30 days
1 files changed,  +9, -13
M pubsub/cli.go
+9, -13
 1@@ -185,10 +185,12 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
 2 			)
 3 
 4 			if cmd == "pub" {
 5+				defaultTimeout, _ := time.ParseDuration("720h")
 6+
 7 				pubCmd := flagSet("pub", sesh)
 8 				empty := pubCmd.Bool("e", false, "Send an empty message to subs")
 9 				public := pubCmd.Bool("p", false, "Anyone can sub to this channel")
10-				timeout := pubCmd.Duration("t", -1, "Timeout as a Go duration before cancelling the pub event. Valid time units are 'ns', 'us' (or 'µs'), 'ms', 's', 'm', 'h'. Default is no timeout.")
11+				timeout := pubCmd.Duration("t", defaultTimeout, "Timeout as a Go duration before cancelling the pub event. Valid time units are 'ns', 'us' (or 'µs'), 'ms', 's', 'm', 'h'. Default is 30 days.")
12 				if !flagCheck(pubCmd, channelName, cmdArgs) {
13 					return
14 				}
15@@ -241,19 +243,13 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
16 				}
17 
18 				go func() {
19-					<-ctx.Done()
20-					pub.Cleanup()
21-				}()
22-
23-				go func() {
24-					// never cancel pub event
25-					if tt == -1 {
26-						return
27+					select {
28+					case <-ctx.Done():
29+						pub.Cleanup()
30+					case <-time.After(tt):
31+						wish.Fatalln(sesh, "timeout reached, exiting ...")
32+						pub.Cleanup()
33 					}
34-
35-					<-time.After(tt)
36-					pub.Cleanup()
37-					wish.Fatalln(sesh, "timeout reached, exiting ...")
38 				}()
39 
40 				err = pubsub.PubSub.Pub(name, pub)