main.go150 lines, 4.1 KB
| 1 | // Command listen is a client for GET /live, the way a browser's EventSource is one: |
| 2 | // it opens a stream, prints what arrives, and when the stream ends it comes back |
| 3 | // and says which event it saw last. |
| 4 | // |
| 5 | // listen 16726 one stream for user 16726, every event on a line |
| 6 | // listen -count 1 a counter once a second, not the lines |
| 7 | // listen -count $(seq 2000) two thousand streams, one for each of the users 1 to 2000 |
| 8 | // listen -count -nap 60s 1 a client that connects, and reads nothing for a minute |
| 9 | package main |
| 10 | |
| 11 | import ( |
| 12 | "bufio" |
| 13 | "context" |
| 14 | "flag" |
| 15 | "fmt" |
| 16 | "net/http" |
| 17 | "os" |
| 18 | "os/signal" |
| 19 | "strings" |
| 20 | "sync/atomic" |
| 21 | "time" |
| 22 | ) |
| 23 | |
| 24 | var ( |
| 25 | base = flag.String("url", "http://localhost:8080", "where the API is") |
| 26 | count = flag.Bool("count", false, "print a counter once a second, not every event") |
| 27 | nap = flag.Duration("nap", 0, "open the stream, and read nothing for this long") |
| 28 | |
| 29 | open, events atomic.Int64 |
| 30 | first, last atomic.Int64 // when this second's first and last event arrived, in nanoseconds |
| 31 | ) |
| 32 | |
| 33 | func main() { |
| 34 | flag.Parse() |
| 35 | if flag.NArg() == 0 { |
| 36 | fmt.Fprintln(os.Stderr, "usage: listen [-count] [-nap 60s] [-url base] <user id>...") |
| 37 | os.Exit(2) |
| 38 | } |
| 39 | ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt) |
| 40 | defer stop() |
| 41 | for _, user := range flag.Args() { |
| 42 | go stream(user) |
| 43 | time.Sleep(time.Millisecond) // a crowd that arrives over a second or two, not in one instant |
| 44 | } |
| 45 | if !*count { |
| 46 | <-ctx.Done() |
| 47 | return |
| 48 | } |
| 49 | var streams, before int64 = -1, 0 |
| 50 | tick := time.NewTicker(time.Second) |
| 51 | for { |
| 52 | select { |
| 53 | case <-ctx.Done(): |
| 54 | return |
| 55 | case <-tick.C: |
| 56 | } |
| 57 | now := events.Load() |
| 58 | if open.Load() == streams && now == before { |
| 59 | continue // nothing new to say |
| 60 | } |
| 61 | streams = open.Load() |
| 62 | line := fmt.Sprintf("streams %-5d events %-6d", streams, now) |
| 63 | if now > before { |
| 64 | took := time.Duration(last.Load() - first.Swap(0)).Round(time.Millisecond) |
| 65 | line += fmt.Sprintf(" +%d in %v", now-before, took) |
| 66 | } |
| 67 | fmt.Println(line) |
| 68 | before = now |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // stream keeps one user's stream open: when it ends, it waits a second and connects again. |
| 73 | func stream(user string) { |
| 74 | lastID := "" |
| 75 | for { |
| 76 | req, _ := http.NewRequest("GET", *base+"/live", nil) |
| 77 | req.Header.Set("Authorization", "Bearer seed-token-"+user) |
| 78 | if lastID != "" { |
| 79 | req.Header.Set("Last-Event-ID", lastID) |
| 80 | } |
| 81 | res, err := http.DefaultClient.Do(req) |
| 82 | switch { |
| 83 | case err != nil: |
| 84 | say(user, "no answer: %v", err) |
| 85 | case res.StatusCode != http.StatusOK: |
| 86 | say(user, "the server said %s", res.Status) |
| 87 | res.Body.Close() |
| 88 | default: |
| 89 | open.Add(1) |
| 90 | lastID = read(user, res, lastID) |
| 91 | open.Add(-1) |
| 92 | res.Body.Close() |
| 93 | say(user, "the stream ended. Back in a second, with Last-Event-ID %q", lastID) |
| 94 | } |
| 95 | time.Sleep(time.Second) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // read prints (or counts) the events of one response, until it ends. It returns the id |
| 100 | // of the last event that had one. |
| 101 | func read(user string, res *http.Response, lastID string) string { |
| 102 | if *nap > 0 { |
| 103 | fmt.Printf("%-6s connected, and reading nothing for %v\n", user, *nap) |
| 104 | time.Sleep(*nap) |
| 105 | defer func() { |
| 106 | fmt.Printf("%-6s read %d events, and then the stream ended\n", user, events.Load()) |
| 107 | os.Exit(0) |
| 108 | }() |
| 109 | } |
| 110 | var id, kind, data string |
| 111 | lines := bufio.NewScanner(res.Body) |
| 112 | for lines.Scan() { |
| 113 | field, value, _ := strings.Cut(lines.Text(), ": ") |
| 114 | switch field { |
| 115 | case "id": |
| 116 | id = value |
| 117 | case "event": |
| 118 | kind = value |
| 119 | case "data": |
| 120 | data = value |
| 121 | case "": // a blank line ends an event; a line that starts with a colon is a comment |
| 122 | if kind == "" { |
| 123 | if !*count && strings.HasPrefix(lines.Text(), ":") { |
| 124 | say(user, "%s", strings.TrimPrefix(lines.Text(), ": ")) |
| 125 | } |
| 126 | continue |
| 127 | } |
| 128 | if id != "" { |
| 129 | lastID = id |
| 130 | } |
| 131 | now := time.Now().UnixNano() |
| 132 | first.CompareAndSwap(0, now) |
| 133 | last.Store(now) |
| 134 | events.Add(1) |
| 135 | say(user, "%-5s %-6s %s", kind, id, data) |
| 136 | id, kind, data = "", "", "" |
| 137 | } |
| 138 | } |
| 139 | if err := lines.Err(); err != nil { |
| 140 | say(user, "the stream broke: %v", err) |
| 141 | } |
| 142 | return lastID |
| 143 | } |
| 144 | |
| 145 | // say prints one line for one stream, unless we are only counting. |
| 146 | func say(user, format string, args ...any) { |
| 147 | if !*count { |
| 148 | fmt.Printf("%-6s %s\n", user, fmt.Sprintf(format, args...)) |
| 149 | } |
| 150 | } |