Skip to content

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
WeidiDeng committed Apr 10, 2023
1 parent 49ba1b8 commit 7c8fe21
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
4 changes: 2 additions & 2 deletions modules/caddyhttp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ func (app *App) Start() error {
server: srv.server,
h2server: h2server,
}
srv.http2listeners = append(srv.http2listeners, http2lnWrapper)
srv.h2listeners = append(srv.h2listeners, http2lnWrapper)
ln = http2lnWrapper
}

Expand Down Expand Up @@ -608,7 +608,7 @@ func (app *App) Stop() error {
defer finishedShutdown.Done()
startedShutdown.Done()

for i, s := range server.http2listeners {
for i, s := range server.h2listeners {
if err := s.Shutdown(ctx); err != nil {
app.logger.Error("http2 listener shutdown",
zap.Error(err),
Expand Down
6 changes: 6 additions & 0 deletions modules/caddyhttp/http2listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import (
"golang.org/x/net/http2"
)

// http2Listener wraps the listener to solve the following problems:
// 1. server h2 natively without using h2c hack when listener handles tls connection but
// don't return *tls.Conn
// 2. graceful shutdown. the shutdown logic is copied from stdlib http.Server, it's an extra maintenance burden but
// whatever, the shutdown logic maybe extracted to be used with h2c graceful shutdown. http2.Server supports graceful shutdown
// sending GO_AWAY frame to connected clients, but doesn't track connection status. It requires explicit call of http2.ConfigureServer
type http2Listener struct {
cnt uint64
net.Listener
Expand Down
14 changes: 8 additions & 6 deletions modules/caddyhttp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"github.com/quic-go/quic-go"
"io"
"net"
"net/http"
Expand All @@ -34,7 +35,6 @@ import (
"github.com/caddyserver/caddy/v2/modules/caddyevents"
"github.com/caddyserver/caddy/v2/modules/caddytls"
"github.com/caddyserver/certmagic"
"github.com/quic-go/quic-go"
"github.com/quic-go/quic-go/http3"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand Down Expand Up @@ -195,11 +195,11 @@ type Server struct {
errorLogger *zap.Logger
ctx caddy.Context

server *http.Server
h3server *http3.Server
h3listeners []net.PacketConn // TODO: we have to hold these because quic-go won't close listeners it didn't create
http2listeners []*http2Listener
addresses []caddy.NetworkAddress
server *http.Server
h3server *http3.Server
h3listeners []net.PacketConn // TODO: we have to hold these because quic-go won't close listeners it didn't create
h2listeners []*http2Listener
addresses []caddy.NetworkAddress

trustedProxies IPRangeSource

Expand All @@ -214,6 +214,8 @@ type Server struct {

// ServeHTTP is the entry point for all HTTP requests.
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// If there are listener wrappers that process tls connections but don't return a *tls.Conn, this field will be nil.
// Can be removed if https://github.com/golang/go/pull/56110 is ever merged.
if r.TLS == nil {
conn := r.Context().Value(ConnCtxKey).(net.Conn)
if csc, ok := conn.(connectionStateConn); ok {
Expand Down

0 comments on commit 7c8fe21

Please sign in to comment.