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)
| 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. |
| 269 | func 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 | } |
no test coverage detected