CloseRead starts a goroutine to read from the connection until it is closed or a data message is received. Once CloseRead is called you cannot read any messages from the connection. The returned context will be cancelled when the connection is closed. If a data message is received, the connection
(ctx context.Context)
| 62 | // |
| 63 | // This function is idempotent. |
| 64 | func (c *Conn) CloseRead(ctx context.Context) context.Context { |
| 65 | c.closeReadMu.Lock() |
| 66 | ctx2 := c.closeReadCtx |
| 67 | if ctx2 != nil { |
| 68 | c.closeReadMu.Unlock() |
| 69 | return ctx2 |
| 70 | } |
| 71 | ctx, cancel := context.WithCancel(ctx) |
| 72 | c.closeReadCtx = ctx |
| 73 | c.closeReadDone = make(chan struct{}) |
| 74 | c.closeReadMu.Unlock() |
| 75 | |
| 76 | go func() { |
| 77 | defer close(c.closeReadDone) |
| 78 | defer cancel() |
| 79 | defer c.close() |
| 80 | _, _, err := c.Reader(ctx) |
| 81 | if err == nil { |
| 82 | c.Close(StatusPolicyViolation, "unexpected data message") |
| 83 | } |
| 84 | }() |
| 85 | return ctx |
| 86 | } |
| 87 | |
| 88 | // SetReadLimit sets the max number of bytes to read for a single message. |
| 89 | // It applies to the Reader and Read methods. |