Serve starts the server to handle incoming connections on the provided listener. It returns an error if no host keys are set or if there is an issue accepting connections.
(l net.Listener)
| 1042 | // Serve starts the server to handle incoming connections on the provided listener. |
| 1043 | // It returns an error if no host keys are set or if there is an issue accepting connections. |
| 1044 | func (s *Server) Serve(l net.Listener) (retErr error) { |
| 1045 | // Ensure we're not mutating HostSigners as we're reading it. |
| 1046 | s.mu.RLock() |
| 1047 | noHostKeys := len(s.srv.HostSigners) == 0 |
| 1048 | s.mu.RUnlock() |
| 1049 | |
| 1050 | if noHostKeys { |
| 1051 | return xerrors.New("no host keys set") |
| 1052 | } |
| 1053 | |
| 1054 | s.logger.Info(context.Background(), "started serving listener", slog.F("listen_addr", l.Addr())) |
| 1055 | defer func() { |
| 1056 | s.logger.Info(context.Background(), "stopped serving listener", |
| 1057 | slog.F("listen_addr", l.Addr()), slog.Error(retErr)) |
| 1058 | }() |
| 1059 | defer l.Close() |
| 1060 | |
| 1061 | s.trackListener(l, true) |
| 1062 | defer s.trackListener(l, false) |
| 1063 | for { |
| 1064 | conn, err := l.Accept() |
| 1065 | if err != nil { |
| 1066 | return err |
| 1067 | } |
| 1068 | go s.handleConn(l, conn) |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | func (s *Server) handleConn(l net.Listener, c net.Conn) { |
| 1073 | logger := s.logger.With( |