Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why ws.outQueue is need to check if the Channel is closed? #308

Open
AndrewYEEE opened this issue Oct 26, 2024 · 1 comment
Open

Why ws.outQueue is need to check if the Channel is closed? #308

AndrewYEEE opened this issue Oct 26, 2024 · 1 comment

Comments

@AndrewYEEE
Copy link

AndrewYEEE commented Oct 26, 2024

Sorry I dont understand why ws.outQueue is need to check that Channel is closed ?
This will cause writePump closed but server.connections still retains the ws object (ws.connection still work).

And
Why doesn't ws.pingMessage need to check if the Channel closed unexpectedly?

func (server *Server) writePump(ws *WebSocket) {
   conn := ws.connection

   for {
   	select {
   	case data, ok := <-ws.outQueue:
   		_ = conn.SetWriteDeadline(time.Now().Add(server.timeoutConfig.WriteWait))
   		if !ok {
   			// Unexpected closed queue, should never happen
   			server.error(fmt.Errorf("output queue for socket %v was closed, forcefully closing", ws.id))
   			// Don't invoke cleanup


   			return      **//<===== will exit writePump(), but server.connections still retains the WS object  !!!**
   		}
   		// Send data
   		err := conn.WriteMessage(websocket.TextMessage, data)
   		if err != nil {
   			server.error(fmt.Errorf("write failed for %s: %w", ws.ID(), err))
   			// Invoking cleanup, as socket was forcefully closed
   			server.cleanupConnection(ws)
   			return
   		}
   		log.Debugf("written %d bytes to %s", len(data), ws.ID())
   	case ping := <-ws.pingMessage:

                      **// why don't need check channel closed unexpectedly?**

   		_ = conn.SetWriteDeadline(time.Now().Add(server.timeoutConfig.WriteWait))
   		err := conn.WriteMessage(websocket.PongMessage, ping)
   		if err != nil {
   			server.error(fmt.Errorf("write failed for %s: %w", ws.ID(), err))
   			// Invoking cleanup, as socket was forcefully closed
   			server.cleanupConnection(ws)
   			return
   		}
   		log.Debugf("pong sent to %s", ws.ID())
   		...
@AndrewYEEE AndrewYEEE changed the title Why ws.outQueue is need to check that Channel is closed and return ? Why ws.outQueue is need to check that Channel is closed ? Oct 26, 2024
@AndrewYEEE AndrewYEEE changed the title Why ws.outQueue is need to check that Channel is closed ? Why ws.outQueue is need to check if the Channel is closed? Oct 26, 2024
@AndrewYEEE
Copy link
Author

AndrewYEEE commented Oct 26, 2024

I have the idea to remove ws object, but I couldn't prove whether the ws object obtained by server.connections[] is the same as the one to be removed.

func (server *Server) writePump(ws *WebSocket) {
   conn := ws.connection
   for {
   	select {
   	      case data, ok := <-ws.outQueue:


                    if !ok {
	                    // Unexpected closed queue, should never happen
	                    server.error(fmt.Errorf("output queue for socket %v was closed, forcefully closing", ws.id))
	                    // Don't invoke cleanup
	                    
	                    //delete chargepoint ws
	                    server.connMutex.Lock()
	                    if _, ok := server.connections[ws.id]; ok {
		                    delete(server.connections, ws.id)                   // is same?
		                    log.Infof("closed connection to %s", ws.ID())
		                    if server.disconnectedHandler != nil {
			                    server.disconnectedHandler(ws) 
		                    }
                            }
	                    server.connMutex.Unlock()
	                    
	                    return
                    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant