MCPcopy Index your code
hub / github.com/coder/coder / readConnLoop

Function readConnLoop

agent/reconnectingpty/reconnectingpty.go:269–298  ·  view source on GitHub ↗

readConnLoop reads messages from conn and writes to ptty as needed. Blocks until EOF or an error writing to ptty or reading from conn.

(ctx context.Context, conn net.Conn, ptty pty.PTYCmd, metrics *prometheus.CounterVec, logger slog.Logger)

Source from the content-addressed store, hash-verified

267// readConnLoop reads messages from conn and writes to ptty as needed. Blocks
268// until EOF or an error writing to ptty or reading from conn.
269func readConnLoop(ctx context.Context, conn net.Conn, ptty pty.PTYCmd, metrics *prometheus.CounterVec, logger slog.Logger) {
270 decoder := json.NewDecoder(conn)
271 for {
272 var req workspacesdk.ReconnectingPTYRequest
273 err := decoder.Decode(&req)
274 if xerrors.Is(err, io.EOF) {
275 return
276 }
277 if err != nil {
278 logger.Warn(ctx, "reconnecting pty failed with read error", slog.Error(err))
279 return
280 }
281 _, err = ptty.InputWriter().Write([]byte(req.Data))
282 if err != nil {
283 logger.Warn(ctx, "reconnecting pty failed with write error", slog.Error(err))
284 metrics.WithLabelValues("input_writer").Add(1)
285 return
286 }
287 // Check if a resize needs to happen!
288 if req.Height == 0 || req.Width == 0 {
289 continue
290 }
291 err = ptty.Resize(req.Height, req.Width)
292 if err != nil {
293 // We can continue after this, it's not fatal!
294 logger.Warn(ctx, "reconnecting pty resize failed, but will continue", slog.Error(err))
295 metrics.WithLabelValues("resize").Add(1)
296 }
297 }
298}

Callers 2

AttachMethod · 0.85
AttachMethod · 0.85

Calls 7

WithLabelValuesMethod · 0.80
WriteMethod · 0.65
InputWriterMethod · 0.65
AddMethod · 0.65
ResizeMethod · 0.65
IsMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected