shithub: hell

Download patch

ref: 1f40f47ce09940c3f34ba469eb2f0162599372d2
parent: 9d38f0a5cb1e494c072de5e4e1d33954f143faa2
author: penny <penny@limitedideas.org>
date: Sat Aug 30 21:24:33 EDT 2025

Refactor dispatcher

--- a/dispatch.go
+++ b/dispatch.go
@@ -18,35 +18,43 @@
 	//Last time the user sent us a line
 	var lastreceive time.Time
 	var comingfast bool
+
+	receiveStatus := func(statustoot *mastodon.Toot) {
+		//Got multiple lines within a second
+		if time.Since(lastreceive) < time.Second {
+			comingfast = true
+		}
+		lastreceive = time.Now()
+		tootQueue = append(tootQueue, statustoot)
+	}
+
 	for {
 		select {
 		case statustoot := <-hc.dispatch:
-			//Got multiple lines within a second
-			if time.Since(lastreceive) < time.Second {
-				comingfast = true
-			}
-			lastreceive = time.Now()
-			tootQueue = append(tootQueue, statustoot)
+			receiveStatus(statustoot)
 		//API delay needs to be tracked without being reset by new inputs
 		case <-time.After(hc.preferences.apidelay - time.Since(lastfire)):
 			if 1 > len(tootQueue) {
+				//Queue is empty, block the loop until we get a new input
 				statustoot := <-hc.dispatch
-				if time.Since(lastreceive) < time.Second {
-					comingfast = true
-				}
-				lastreceive = time.Now()
-				tootQueue = append(tootQueue, statustoot)
+				receiveStatus(statustoot)
 				break
 			}
 			//User is sending lines faster than one a second, flood control
 			if comingfast {
-				if time.Since(lastreceive) < time.Second {
-					//looks like they're still coming
-					break
+				//Take over the loop until we stop getting messages fast
+				for comingfast {
+					select {
+					case statustoot := <-hc.dispatch:
+						receiveStatus(statustoot)
+					//We already know we're flooding so we can probably be a little less sensitive
+					case <-time.After(time.Second / 5):
+						fmt.Printf("Got %v lines fast!\n", len(tootQueue))
+						tootQueue = nil
+						comingfast = false
+					}
 				}
-				fmt.Printf("Got %v lines fast!\n", len(tootQueue))
-				tootQueue = nil
-				comingfast = false
+				//Nothing to send
 				break
 			}
 
--