MCPcopy Create free account
hub / github.com/buggregator/server / handleMessagesWait

Function handleMessagesWait

modules/smtp/api.go:112–154  ·  view source on GitHub ↗

handleMessagesWait long-polls for a matching SMTP message. It holds the connection until a match arrives or the timeout expires. Returns 200 with the first matching event, or 408 on timeout.

(store event.Store, mod *Module)

Source from the content-addressed store, hash-verified

110// It holds the connection until a match arrives or the timeout expires.
111// Returns 200 with the first matching event, or 408 on timeout.
112func handleMessagesWait(store event.Store, mod *Module) http.HandlerFunc {
113 return func(w http.ResponseWriter, r *http.Request) {
114 f := parseFilter(r)
115
116 // Parse timeout (default 30 s, max 60 s).
117 timeout := 30 * time.Second
118 if t := r.URL.Query().Get("timeout"); t != "" {
119 if d, err := time.ParseDuration(t); err == nil && d > 0 {
120 if d > 60*time.Second {
121 d = 60 * time.Second
122 }
123 timeout = d
124 }
125 }
126
127 // Subscribe BEFORE checking existing events to avoid the race where a
128 // matching event arrives between the check and the wait.
129 ch, unsub := mod.subscribe(f)
130 defer unsub()
131
132 // Check for an already-stored matching event.
133 events, err := store.FindAll(r.Context(), event.FindOptions{Type: "smtp", Project: f.Project})
134 if err != nil {
135 smtpError(w, err.Error(), http.StatusInternalServerError)
136 return
137 }
138 if matched := applyFilter(events, f); len(matched) > 0 {
139 smtpJSON(w, matched[0])
140 return
141 }
142
143 // Wait for a new matching event.
144 ctx, cancel := context.WithTimeout(r.Context(), timeout)
145 defer cancel()
146
147 select {
148 case ev := <-ch:
149 smtpJSON(w, ev)
150 case <-ctx.Done():
151 smtpError(w, "timeout waiting for message", http.StatusRequestTimeout)
152 }
153 }
154}
155
156// handleDeleteMessages purges SMTP messages, optionally filtered by project.
157func handleDeleteMessages(store event.Store) http.HandlerFunc {

Callers 1

registerSMTPAPIFunction · 0.85

Calls 7

parseFilterFunction · 0.85
smtpErrorFunction · 0.85
applyFilterFunction · 0.85
smtpJSONFunction · 0.85
subscribeMethod · 0.80
FindAllMethod · 0.65
GetMethod · 0.45

Tested by

no test coverage detected