MCPcopy
hub / github.com/coder/websocket / CloseRead

Method CloseRead

read.go:64–86  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

62//
63// This function is idempotent.
64func (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.

Callers 6

subscribeMethod · 0.95
TestConnFunction · 0.45
TestConcurrentClosePingFunction · 0.45
TestConnClosePropagationFunction · 0.45
Example_writeOnlyFunction · 0.45
ExampleConn_PingFunction · 0.45

Calls 4

closeMethod · 0.95
ReaderMethod · 0.95
CloseMethod · 0.95
LockMethod · 0.45

Tested by 5

TestConnFunction · 0.36
TestConcurrentClosePingFunction · 0.36
TestConnClosePropagationFunction · 0.36
Example_writeOnlyFunction · 0.36
ExampleConn_PingFunction · 0.36