|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/gin-gonic/gin" |
| 9 | + controlv1 "github.com/persys-dev/persys-cloud/persys-gateway/internal/controlv1" |
| 10 | + "github.com/persys-dev/persys-cloud/persys-gateway/services" |
| 11 | +) |
| 12 | + |
| 13 | +// EventsController exposes cluster-wide scheduler events to browser-based |
| 14 | +// dashboard clients over Server-Sent Events. WatchEvents is a |
| 15 | +// server-streaming gRPC RPC, which grpcbridge explicitly does not |
| 16 | +// auto-bridge (see internal/grpcbridge/bridge.go) — this hand-written |
| 17 | +// endpoint is the browser-facing equivalent. A gRPC-native client (e.g. |
| 18 | +// persysctl) should call the scheduler's WatchEvents RPC directly instead |
| 19 | +// of going through this endpoint. |
| 20 | +type EventsController struct { |
| 21 | + clusterControl *services.ClusterControlService |
| 22 | +} |
| 23 | + |
| 24 | +func NewEventsController(clusterControl *services.ClusterControlService) *EventsController { |
| 25 | + return &EventsController{clusterControl: clusterControl} |
| 26 | +} |
| 27 | + |
| 28 | +// Register mounts /events/watch on rg, — this |
| 29 | +// is dashboard-user-facing mTLS-gated SSE endpoint for cluster-wide scheduler events. A gRPC-native client. |
| 30 | +func (c *EventsController) Register(rg *gin.RouterGroup) { |
| 31 | + rg.GET("/events/watch", c.WatchHandler()) |
| 32 | +} |
| 33 | + |
| 34 | +// WatchHandler streams cluster-wide scheduler events to the client as |
| 35 | +// Server-Sent Events, replaying recent history first (same semantics as |
| 36 | +// the underlying WatchEvents RPC). Supports the same optional filters as |
| 37 | +// query params: ?type=&workload_id=&node_id=&cluster_id= |
| 38 | +func (c *EventsController) WatchHandler() gin.HandlerFunc { |
| 39 | + return func(ctx *gin.Context) { |
| 40 | + clusterID := strings.TrimSpace(ctx.Query("cluster_id")) |
| 41 | + req := &controlv1.WatchEventsRequest{ |
| 42 | + Type: strings.TrimSpace(ctx.Query("type")), |
| 43 | + WorkloadId: strings.TrimSpace(ctx.Query("workload_id")), |
| 44 | + NodeId: strings.TrimSpace(ctx.Query("node_id")), |
| 45 | + } |
| 46 | + |
| 47 | + ctx.Header("Content-Type", "text/event-stream") |
| 48 | + ctx.Header("Cache-Control", "no-cache") |
| 49 | + ctx.Header("Connection", "keep-alive") |
| 50 | + // Disable response buffering if this gateway ever sits behind |
| 51 | + // nginx — otherwise SSE events sit in a buffer instead of |
| 52 | + // reaching the browser as they arrive. |
| 53 | + ctx.Header("X-Accel-Buffering", "no") |
| 54 | + |
| 55 | + streamCtx, cancel := context.WithCancel(ctx.Request.Context()) |
| 56 | + defer cancel() |
| 57 | + |
| 58 | + events := make(chan *controlv1.SchedulerEventView, 16) |
| 59 | + go func() { |
| 60 | + defer close(events) |
| 61 | + _ = c.clusterControl.WatchEventsForClient(streamCtx, clusterID, req, func(event *controlv1.SchedulerEventView) error { |
| 62 | + select { |
| 63 | + case events <- event: |
| 64 | + return nil |
| 65 | + case <-streamCtx.Done(): |
| 66 | + return streamCtx.Err() |
| 67 | + } |
| 68 | + }) |
| 69 | + // Errors here (scheduler unreachable, stream ended, etc) are |
| 70 | + // intentionally swallowed rather than surfaced to the HTTP |
| 71 | + // response: by the time an error could occur, headers are |
| 72 | + // already flushed and the client is mid-stream — the |
| 73 | + // connection simply closes, and a well-behaved EventSource |
| 74 | + // client on the dashboard side reconnects on its own. |
| 75 | + }() |
| 76 | + |
| 77 | + ctx.Stream(func(w io.Writer) bool { |
| 78 | + select { |
| 79 | + case event, ok := <-events: |
| 80 | + if !ok { |
| 81 | + return false |
| 82 | + } |
| 83 | + ctx.SSEvent("event", event) |
| 84 | + return true |
| 85 | + case <-streamCtx.Done(): |
| 86 | + return false |
| 87 | + } |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
0 commit comments