Skip to content

Commit 1f907b2

Browse files
committed
feat: Make count and delay configurable in /sse endpoint
1 parent fff1702 commit 1f907b2

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

assets/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ <h3 id=client-tuned-responses>Client Tuned Response <a href="#client-tuned-respo
248248

249249
<dt id=sse>/sse</dt>
250250
<dd>Responds with 10 <a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events">
251-
Server sent events</a>, each after 1s of delay. The count and delay aren't configurable today.
251+
Server sent events</a>, each after 1s of delay. The count and delay can be set as query params. Count should be between 1
252+
and 100. Delay should be between 1 and 10.
252253
</dd>
253254

254255
<dt id=links>/links/<span class=var>{count}</span></dt>

routes/sse/handler.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,40 @@ var RouteList = []ex.Route{
1616
}
1717

1818
func handleServerSentEvents(ex *ex.Exchange) response.Response {
19+
delay, err := ex.QueryParamInt("delay", 1)
20+
if err != nil {
21+
return response.BadRequest("Invalid delay value")
22+
}
23+
if delay < 1 {
24+
return response.BadRequest("Delay must be greater than 0")
25+
}
26+
if delay > 10 {
27+
return response.BadRequest("Delay must be less than 10")
28+
}
29+
30+
count, err := ex.QueryParamInt("count", 10)
31+
if err != nil {
32+
return response.BadRequest("Invalid count value")
33+
}
34+
if count < 1 {
35+
return response.BadRequest("Count must be greater than 0")
36+
}
37+
if count > 100 {
38+
return response.BadRequest("Count must be less than 100")
39+
}
40+
1941
return response.Response{
2042
Header: map[string][]string{
2143
"Cache-Control": {"no-store"},
2244
c.ContentType: {"text/event-stream"},
2345
},
2446
Writer: func(w response.BodyWriter) {
25-
for id := range 10 {
47+
for id := range count {
2648
err := w.Write(strings.Join(pingMessage(id+1), "\n") + "\n\n")
2749
if err != nil {
2850
log.Printf("Error writing to response: %v\n", err)
2951
}
30-
time.Sleep(1 * time.Second)
52+
time.Sleep(time.Duration(delay) * time.Second)
3153
}
3254
},
3355
}

0 commit comments

Comments
 (0)