MCPcopy
hub / github.com/name5566/leaf / ServeHTTP

Method ServeHTTP

network/ws_server.go:34–74  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

32}
33
34func (handler *WSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
35 if r.Method != "GET" {
36 http.Error(w, "Method not allowed", 405)
37 return
38 }
39 conn, err := handler.upgrader.Upgrade(w, r, nil)
40 if err != nil {
41 log.Debug("upgrade error: %v", err)
42 return
43 }
44 conn.SetReadLimit(int64(handler.maxMsgLen))
45
46 handler.wg.Add(1)
47 defer handler.wg.Done()
48
49 handler.mutexConns.Lock()
50 if handler.conns == nil {
51 handler.mutexConns.Unlock()
52 conn.Close()
53 return
54 }
55 if len(handler.conns) >= handler.maxConnNum {
56 handler.mutexConns.Unlock()
57 conn.Close()
58 log.Debug("too many connections")
59 return
60 }
61 handler.conns[conn] = struct{}{}
62 handler.mutexConns.Unlock()
63
64 wsConn := newWSConn(conn, handler.pendingWriteNum, handler.maxMsgLen)
65 agent := handler.newAgent(wsConn)
66 agent.Run()
67
68 // cleanup
69 wsConn.Close()
70 handler.mutexConns.Lock()
71 delete(handler.conns, conn)
72 handler.mutexConns.Unlock()
73 agent.OnClose()
74}
75
76func (server *WSServer) Start() {
77 ln, err := net.Listen("tcp", server.Addr)

Callers

nothing calls this directly

Calls 6

newWSConnFunction · 0.85
ErrorMethod · 0.80
DebugMethod · 0.80
CloseMethod · 0.65
RunMethod · 0.65
OnCloseMethod · 0.65

Tested by

no test coverage detected