File tree Expand file tree Collapse file tree 2 files changed +26
-3
lines changed
Expand file tree Collapse file tree 2 files changed +26
-3
lines changed Original file line number Diff line number Diff 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 >
Original file line number Diff line number Diff line change @@ -16,18 +16,40 @@ var RouteList = []ex.Route{
1616}
1717
1818func 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 }
You can’t perform that action at this time.
0 commit comments