MCPcopy
hub / github.com/grafana/dskit / handleConnection

Method handleConnection

kv/memberlist/tcp_transport.go:275–360  ·  view source on GitHub ↗
(conn net.Conn)

Source from the content-addressed store, hash-verified

273}
274
275func (t *TCPTransport) handleConnection(conn net.Conn) {
276 t.debugLog().Log("msg", "New connection", "addr", conn.RemoteAddr())
277
278 // Wrap the connection to track sent/received bytes.
279 conn = newMeteredConn(conn, t.sentBytes, t.receivedBytes)
280
281 closeConn := true
282 defer func() {
283 if closeConn {
284 _ = conn.Close()
285 }
286 }()
287
288 // let's read first byte, and determine what to do about this connection
289 msgType := []byte{0}
290 _, err := io.ReadFull(conn, msgType)
291 if err != nil {
292 level.Warn(t.logger).Log("msg", "failed to read message type", "err", err, "remote", conn.RemoteAddr())
293 return
294 }
295
296 if messageType(msgType[0]) == stream {
297 t.incomingStreams.Inc()
298
299 // hand over this connection to memberlist
300 closeConn = false
301 t.connCh <- conn
302 } else if messageType(msgType[0]) == packet {
303 // it's a memberlist "packet", which contains an address and data.
304 t.receivedPackets.Inc()
305
306 // before reading packet, read the address
307 addrLengthBuf := []byte{0}
308 _, err := io.ReadFull(conn, addrLengthBuf)
309 if err != nil {
310 t.receivedPacketsErrors.Inc()
311 level.Warn(t.logger).Log("msg", "error while reading node address length from packet", "err", err, "remote", conn.RemoteAddr())
312 return
313 }
314
315 addrBuf := make([]byte, addrLengthBuf[0])
316 _, err = io.ReadFull(conn, addrBuf)
317 if err != nil {
318 t.receivedPacketsErrors.Inc()
319 level.Warn(t.logger).Log("msg", "error while reading node address from packet", "err", err, "remote", conn.RemoteAddr())
320 return
321 }
322
323 // read the rest to buffer -- this is the "packet" itself
324 buf, err := io.ReadAll(conn)
325 if err != nil {
326 t.receivedPacketsErrors.Inc()
327 level.Warn(t.logger).Log("msg", "error while reading packet data", "err", err, "remote", conn.RemoteAddr())
328 return
329 }
330
331 if len(buf) < md5.Size {
332 t.receivedPacketsErrors.Inc()

Callers 1

tcpListenMethod · 0.95

Calls 9

debugLogMethod · 0.95
newMeteredConnFunction · 0.85
messageTypeTypeAlias · 0.85
addrTypeAlias · 0.85
CloseMethod · 0.65
AddMethod · 0.65
LogMethod · 0.45
EqualMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected