MCPcopy Create free account
hub / github.com/coder/coder / NewMultiReplicaSubscribeFn

Function NewMultiReplicaSubscribeFn

enterprise/coderd/x/chatd/chatd.go:149–628  ·  view source on GitHub ↗

NewMultiReplicaSubscribeFn returns a SubscribeFn that manages relay connections to remote replicas and returns relay message_part events only. OSS handles pubsub subscription, message catch-up, queue updates, status forwarding, and local parts merging. nolint:gocognit // Complexity is inherent to t

(
	cfg MultiReplicaSubscribeConfig,
)

Source from the content-addressed store, hash-verified

147//
148//nolint:gocognit // Complexity is inherent to the multi-source merge loop.
149func NewMultiReplicaSubscribeFn(
150 cfg MultiReplicaSubscribeConfig,
151) osschatd.SubscribeFn {
152 return func(ctx context.Context, params osschatd.SubscribeFnParams) <-chan codersdk.ChatStreamEvent {
153 chatID := params.ChatID
154 requestHeader := params.RequestHeader
155 logger := params.Logger
156
157 var relayCancel func()
158 var relayParts <-chan codersdk.ChatStreamEvent
159
160 // If the chat is currently running on a different worker
161 // and we have a remote parts provider, open an initial
162 // relay synchronously so the caller gets in-flight
163 // message_part events right away.
164 var initialRelaySnapshot []codersdk.ChatStreamEvent
165 if params.Chat.Status == database.ChatStatusRunning &&
166 params.Chat.WorkerID.Valid &&
167 params.Chat.WorkerID.UUID != params.WorkerID &&
168 cfg.dial() != nil {
169 snapshot, parts, cancel, err := cfg.dial()(ctx, chatID, params.Chat.WorkerID.UUID, requestHeader)
170 if err == nil {
171 relayCancel = cancel
172 relayParts = parts
173 // Collect relay message_parts to forward at the
174 // start of the merge goroutine.
175 for _, event := range snapshot {
176 if event.Type == codersdk.ChatStreamEventTypeMessagePart {
177 initialRelaySnapshot = append(initialRelaySnapshot, event)
178 }
179 }
180 } else {
181 logger.Warn(ctx, "failed to open initial relay for chat stream",
182 slog.F("chat_id", chatID),
183 slog.Error(err),
184 )
185 }
186 }
187
188 // Merge all event sources.
189 mergedEvents := make(chan codersdk.ChatStreamEvent, 128)
190 // Channel for async relay establishment.
191 type relayResult struct {
192 parts <-chan codersdk.ChatStreamEvent
193 cancel func()
194 workerID uuid.UUID // the worker this dial targeted
195 // err and parts are mutually exclusive: success sets
196 // parts; failure sets err (unwrap to *RelayDialError
197 // for classification).
198 err error
199 }
200 relayReadyCh := make(chan relayResult, 4)
201
202 // Reset on successful dial or when the relay target
203 // changes, so a fresh target starts at the floor delay.
204 retryState := newRelayRetryState()
205 // Per-dial context so in-flight dials can be canceled when
206 // a new dial is initiated or the relay is closed.

Callers

nothing calls this directly

Calls 13

IsUnrecoverableMethod · 0.95
newRelayRetryStateFunction · 0.85
dialMethod · 0.80
ErrMethod · 0.80
AsMethod · 0.80
clockMethod · 0.80
nextMethod · 0.80
StopMethod · 0.65
GetChatByIDMethod · 0.65
ErrorMethod · 0.45
cancelMethod · 0.45
resetMethod · 0.45

Tested by

no test coverage detected