echo reads from the WebSocket connection and then writes the received message back to it. The entire function has 10s to complete.
(c *websocket.Conn, l *rate.Limiter)
| 52 | // the received message back to it. |
| 53 | // The entire function has 10s to complete. |
| 54 | func echo(c *websocket.Conn, l *rate.Limiter) error { |
| 55 | ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) |
| 56 | defer cancel() |
| 57 | |
| 58 | err := l.Wait(ctx) |
| 59 | if err != nil { |
| 60 | return err |
| 61 | } |
| 62 | |
| 63 | typ, r, err := c.Reader(ctx) |
| 64 | if err != nil { |
| 65 | return err |
| 66 | } |
| 67 | |
| 68 | w, err := c.Writer(ctx, typ) |
| 69 | if err != nil { |
| 70 | return err |
| 71 | } |
| 72 | |
| 73 | _, err = io.Copy(w, r) |
| 74 | if err != nil { |
| 75 | return fmt.Errorf("failed to io.Copy: %w", err) |
| 76 | } |
| 77 | |
| 78 | err = w.Close() |
| 79 | return err |
| 80 | } |