Skip to content

Commit

Permalink
await all handlers to end
Browse files Browse the repository at this point in the history
  • Loading branch information
ninedraft committed Nov 12, 2022
1 parent 3d02558 commit a83a2ca
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions gemax/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func (server *Server) init() {
}

// ListenAndServe starts a TLS gemini server at specified server.
// It will block until context is canceled.
// It respects the MaxConnections setting.
// It will await all running handlers to end.
func (server *Server) ListenAndServe(ctx context.Context, tlsCfg *tls.Config) error {
server.init()
ctx, cancel := context.WithCancel(ctx)
Expand Down Expand Up @@ -79,16 +82,21 @@ func (server *Server) ListenAndServe(ctx context.Context, tlsCfg *tls.Config) er
}

// Serve starts server on provided listener. Provided context will be passed to handlers.
// Serve will await all running handlers to end.
func (server *Server) Serve(ctx context.Context, listener net.Listener) error {
server.init()
server.addListener(listener)
var wg sync.WaitGroup
for {
var conn, errAccept = listener.Accept()
if errAccept != nil {
wg.Wait()
return fmt.Errorf("gemini server: %w", errAccept)
}
var track = server.addConn(conn)
wg.Add(1)
go func() {
defer wg.Done()
defer server.removeTrack(track)
server.handle(ctx, conn)
}()
Expand Down

0 comments on commit a83a2ca

Please sign in to comment.