Repository: DiceDB/dice
File: internal/server/server.go (Go)

Code before:
				if event.Fd == s.serverFD {
					if err := s.acceptConnection(); err != nil {
						s.logger.Warn(err.Error())
					}
				} else {
					if err := s.handleClientEvent(event); err != nil {
						if errors.Is(err, ErrAborted) {
							s.logger.Debug("Received abort command, initiating graceful shutdown")
							return err
						} else if !errors.Is(err, syscall.ECONNRESET) && !errors.Is(err, net.ErrClosed) {
							s.logger.Warn(err.Error())
						}
					}
				}
			}
		}
	}
}

// acceptConnection accepts a new client connection and subscribes to read events on the connection.
func (s *AsyncServer) acceptConnection() error {
	fd, _, err := syscall.Accept(s.serverFD)
	if err != nil {
		return err
	}

	s.connectedClients[fd] = comm.NewClient(fd)
	if err := syscall.SetNonblock(fd, true); err != nil {
		return err
	}

	return s.multiplexer.Subscribe(iomultiplexer.Event{
		Fd: fd,
		Op: iomultiplexer.OpRead,
	})
}

// handleClientEvent reads commands from the client connection and responds to the client. It also handles disconnections.
func (s *AsyncServer) handleClientEvent(event iomultiplexer.Event) error {
	client := s.connectedClients[event.Fd]
	if client == nil {
		return nil
	}

	commands, hasAbort, err := readCommands(client)
	if err != nil {
		if err := syscall.Close(event.Fd); err != nil {
			s.logger.Error("error closing client connection", slog.Any("error", err))
		}
		delete(s.connectedClients, event.Fd)
		return err

Review comment (suggestion):
Ideally it's not a good practice have same number of maxClients value as the backlog queue max number which is being set during `listen()`. Shouldn't be a blocker for this PR, but we should revisit this later.

Code after:
				if event.Fd == s.serverFD {
					if err := s.acceptConnection(); err != nil {
						s.logger.Warn(err.Error())
						// Close the event FD on error
						if closeErr := syscall.Close(event.Fd); closeErr != nil {
							s.logger.Error("Failed to close event FD:", slog.Any("error", closeErr))
						}
					}
				} else {
					if err := s.handleClientEvent(event); err != nil {
						if errors.Is(err, ErrAborted) {
							s.logger.Debug("Received abort command, initiating graceful shutdown")
							return err
						} else if !errors.Is(err, syscall.ECONNRESET) && !errors.Is(err, net.ErrClosed) {
							s.logger.Warn(err.Error())
						}
					}
				}
			}
		}
	}
}

// acceptConnection accepts a new client connection and subscribes to read events on the connection.
func (s *AsyncServer) acceptConnection() error {
	if len(s.connectedClients) > s.maxClients {
		return errors.New("connection refused. Reached the max-connection limit")
	}

	fd, _, err := syscall.Accept(s.serverFD)
	if err != nil {
		return err
	}

	s.connectedClients[fd] = comm.NewClient(fd)
	if err := syscall.SetNonblock(fd, true); err != nil {
		return err
	}

	return s.multiplexer.Subscribe(iomultiplexer.Event{
		Fd: fd,
		Op: iomultiplexer.OpRead,
	})
}

// handleClientEvent reads commands from the client connection and responds to the client. It also handles disconnections.
func (s *AsyncServer) handleClientEvent(event iomultiplexer.Event) error {
	client := s.connectedClients[event.Fd]
	if client == nil {
		return nil
	}