make EarlyListener a struct, not an interface

This commit is contained in:
Marten Seemann 2023-03-25 09:19:23 +11:00
parent 1b64b13750
commit 86a1234c87
13 changed files with 174 additions and 164 deletions

View file

@ -0,0 +1,80 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: github.com/quic-go/quic-go/http3 (interfaces: QUICEarlyListener)
// Package http3 is a generated GoMock package.
package http3
import (
context "context"
net "net"
reflect "reflect"
gomock "github.com/golang/mock/gomock"
quic "github.com/quic-go/quic-go"
)
// MockQUICEarlyListener is a mock of QUICEarlyListener interface.
type MockQUICEarlyListener struct {
ctrl *gomock.Controller
recorder *MockQUICEarlyListenerMockRecorder
}
// MockQUICEarlyListenerMockRecorder is the mock recorder for MockQUICEarlyListener.
type MockQUICEarlyListenerMockRecorder struct {
mock *MockQUICEarlyListener
}
// NewMockQUICEarlyListener creates a new mock instance.
func NewMockQUICEarlyListener(ctrl *gomock.Controller) *MockQUICEarlyListener {
mock := &MockQUICEarlyListener{ctrl: ctrl}
mock.recorder = &MockQUICEarlyListenerMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockQUICEarlyListener) EXPECT() *MockQUICEarlyListenerMockRecorder {
return m.recorder
}
// Accept mocks base method.
func (m *MockQUICEarlyListener) Accept(arg0 context.Context) (quic.EarlyConnection, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Accept", arg0)
ret0, _ := ret[0].(quic.EarlyConnection)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// Accept indicates an expected call of Accept.
func (mr *MockQUICEarlyListenerMockRecorder) Accept(arg0 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Accept", reflect.TypeOf((*MockQUICEarlyListener)(nil).Accept), arg0)
}
// Addr mocks base method.
func (m *MockQUICEarlyListener) Addr() net.Addr {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Addr")
ret0, _ := ret[0].(net.Addr)
return ret0
}
// Addr indicates an expected call of Addr.
func (mr *MockQUICEarlyListenerMockRecorder) Addr() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Addr", reflect.TypeOf((*MockQUICEarlyListener)(nil).Addr))
}
// Close mocks base method.
func (m *MockQUICEarlyListener) Close() error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Close")
ret0, _ := ret[0].(error)
return ret0
}
// Close indicates an expected call of Close.
func (mr *MockQUICEarlyListenerMockRecorder) Close() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Close", reflect.TypeOf((*MockQUICEarlyListener)(nil).Close))
}

View file

@ -4,3 +4,5 @@ package http3
//go:generate sh -c "go run github.com/golang/mock/mockgen -build_flags=\"-tags=gomock\" -package http3 -destination mock_roundtripcloser_test.go github.com/quic-go/quic-go/http3 RoundTripCloser"
type RoundTripCloser = roundTripCloser
//go:generate sh -c "go run github.com/golang/mock/mockgen -package http3 -destination mock_quic_early_listener_test.go github.com/quic-go/quic-go/http3 QUICEarlyListener"

View file

@ -23,8 +23,12 @@ import (
// allows mocking of quic.Listen and quic.ListenAddr
var (
quicListen = quic.ListenEarly
quicListenAddr = quic.ListenAddrEarly
quicListen = func(conn net.PacketConn, tlsConf *tls.Config, config *quic.Config) (QUICEarlyListener, error) {
return quic.ListenEarly(conn, tlsConf, config)
}
quicListenAddr = func(addr string, tlsConf *tls.Config, config *quic.Config) (QUICEarlyListener, error) {
return quic.ListenAddrEarly(addr, tlsConf, config)
}
)
const (
@ -44,6 +48,15 @@ const (
streamTypeQPACKDecoderStream = 3
)
// A QUICEarlyListener listens for incoming QUIC connections.
type QUICEarlyListener interface {
Accept(context.Context) (quic.EarlyConnection, error)
Addr() net.Addr
io.Closer
}
var _ QUICEarlyListener = &quic.EarlyListener{}
func versionToALPN(v protocol.VersionNumber) string {
//nolint:exhaustive // These are all the versions we care about.
switch v {
@ -193,7 +206,7 @@ type Server struct {
UniStreamHijacker func(StreamType, quic.Connection, quic.ReceiveStream, error) (hijacked bool)
mutex sync.RWMutex
listeners map[*quic.EarlyListener]listenerInfo
listeners map[*QUICEarlyListener]listenerInfo
closed bool
@ -249,7 +262,7 @@ func (s *Server) ServeQUICConn(conn quic.Connection) error {
// Make sure you use http3.ConfigureTLSConfig to configure a tls.Config
// and use it to construct a http3-friendly QUIC listener.
// Closing the server does close the listener.
func (s *Server) ServeListener(ln quic.EarlyListener) error {
func (s *Server) ServeListener(ln QUICEarlyListener) error {
if err := s.addListener(&ln); err != nil {
return err
}
@ -283,7 +296,7 @@ func (s *Server) serveConn(tlsConf *tls.Config, conn net.PacketConn) error {
quicConf.EnableDatagrams = true
}
var ln quic.EarlyListener
var ln QUICEarlyListener
var err error
if conn == nil {
addr := s.Addr
@ -305,7 +318,7 @@ func (s *Server) serveConn(tlsConf *tls.Config, conn net.PacketConn) error {
return err
}
func (s *Server) serveListener(ln quic.EarlyListener) error {
func (s *Server) serveListener(ln QUICEarlyListener) error {
for {
conn, err := ln.Accept(context.Background())
if err != nil {
@ -391,7 +404,7 @@ func (s *Server) generateAltSvcHeader() {
// We store a pointer to interface in the map set. This is safe because we only
// call trackListener via Serve and can track+defer untrack the same pointer to
// local variable there. We never need to compare a Listener from another caller.
func (s *Server) addListener(l *quic.EarlyListener) error {
func (s *Server) addListener(l *QUICEarlyListener) error {
s.mutex.Lock()
defer s.mutex.Unlock()
@ -402,25 +415,24 @@ func (s *Server) addListener(l *quic.EarlyListener) error {
s.logger = utils.DefaultLogger.WithPrefix("server")
}
if s.listeners == nil {
s.listeners = make(map[*quic.EarlyListener]listenerInfo)
s.listeners = make(map[*QUICEarlyListener]listenerInfo)
}
if port, err := extractPort((*l).Addr().String()); err == nil {
s.listeners[l] = listenerInfo{port}
} else {
s.logger.Errorf(
"Unable to extract port from listener %+v, will not be announced using SetQuicHeaders: %s", err)
s.logger.Errorf("Unable to extract port from listener %+v, will not be announced using SetQuicHeaders: %s", err)
s.listeners[l] = listenerInfo{}
}
s.generateAltSvcHeader()
return nil
}
func (s *Server) removeListener(l *quic.EarlyListener) {
func (s *Server) removeListener(l *QUICEarlyListener) {
s.mutex.Lock()
defer s.mutex.Unlock()
delete(s.listeners, l)
s.generateAltSvcHeader()
s.mutex.Unlock()
}
func (s *Server) handleConn(conn quic.Connection) error {

View file

@ -28,34 +28,25 @@ import (
gmtypes "github.com/onsi/gomega/types"
)
type mockAddr struct {
addr string
}
type mockAddr struct{ addr string }
func (ma *mockAddr) Network() string {
return "udp"
}
func (ma *mockAddr) String() string {
return ma.addr
}
func (ma *mockAddr) Network() string { return "udp" }
func (ma *mockAddr) String() string { return ma.addr }
type mockAddrListener struct {
*mockquic.MockEarlyListener
*MockQUICEarlyListener
addr *mockAddr
}
func (m *mockAddrListener) Addr() net.Addr {
_ = m.MockEarlyListener.Addr()
_ = m.MockQUICEarlyListener.Addr()
return m.addr
}
func newMockAddrListener(addr string) *mockAddrListener {
return &mockAddrListener{
MockEarlyListener: mockquic.NewMockEarlyListener(mockCtrl),
addr: &mockAddr{
addr: addr,
},
MockQUICEarlyListener: NewMockQUICEarlyListener(mockCtrl),
addr: &mockAddr{addr: addr},
}
}
@ -791,20 +782,20 @@ var _ = Describe("Server", func() {
s.QuicConfig = &quic.Config{Versions: []protocol.VersionNumber{protocol.VersionDraft29}}
})
var ln1 quic.EarlyListener
var ln2 quic.EarlyListener
var ln1 QUICEarlyListener
var ln2 QUICEarlyListener
expected := http.Header{
"Alt-Svc": {`h3-29=":443"; ma=2592000`},
}
addListener := func(addr string, ln *quic.EarlyListener) {
addListener := func(addr string, ln *QUICEarlyListener) {
mln := newMockAddrListener(addr)
mln.EXPECT().Addr()
*ln = mln
s.addListener(ln)
}
removeListener := func(ln *quic.EarlyListener) {
removeListener := func(ln *QUICEarlyListener) {
s.removeListener(ln)
}
@ -951,7 +942,7 @@ var _ = Describe("Server", func() {
It("sets the GetConfigForClient callback if no tls.Config is given", func() {
var receivedConf *tls.Config
quicListenAddr = func(addr string, tlsConf *tls.Config, _ *quic.Config) (quic.EarlyListener, error) {
quicListenAddr = func(addr string, tlsConf *tls.Config, _ *quic.Config) (QUICEarlyListener, error) {
receivedConf = tlsConf
return nil, errors.New("listen err")
}
@ -1021,7 +1012,7 @@ var _ = Describe("Server", func() {
It("serves a packet conn", func() {
ln := newMockAddrListener(":443")
conn := &net.UDPConn{}
quicListen = func(c net.PacketConn, tlsConf *tls.Config, config *quic.Config) (quic.EarlyListener, error) {
quicListen = func(c net.PacketConn, tlsConf *tls.Config, config *quic.Config) (QUICEarlyListener, error) {
Expect(c).To(Equal(conn))
return ln, nil
}
@ -1052,12 +1043,12 @@ var _ = Describe("Server", func() {
It("serves two packet conns", func() {
ln1 := newMockAddrListener(":443")
ln2 := newMockAddrListener(":8443")
lns := make(chan quic.EarlyListener, 2)
lns := make(chan QUICEarlyListener, 2)
lns <- ln1
lns <- ln2
conn1 := &net.UDPConn{}
conn2 := &net.UDPConn{}
quicListen = func(c net.PacketConn, tlsConf *tls.Config, config *quic.Config) (quic.EarlyListener, error) {
quicListen = func(c net.PacketConn, tlsConf *tls.Config, config *quic.Config) (QUICEarlyListener, error) {
return <-lns, nil
}
@ -1111,7 +1102,7 @@ var _ = Describe("Server", func() {
It("serves a listener", func() {
var called int32
ln := newMockAddrListener(":443")
quicListen = func(conn net.PacketConn, tlsConf *tls.Config, config *quic.Config) (quic.EarlyListener, error) {
quicListen = func(conn net.PacketConn, tlsConf *tls.Config, config *quic.Config) (QUICEarlyListener, error) {
atomic.StoreInt32(&called, 1)
return ln, nil
}
@ -1142,10 +1133,10 @@ var _ = Describe("Server", func() {
var called int32
ln1 := newMockAddrListener(":443")
ln2 := newMockAddrListener(":8443")
lns := make(chan quic.EarlyListener, 2)
lns := make(chan QUICEarlyListener, 2)
lns <- ln1
lns <- ln2
quicListen = func(c net.PacketConn, tlsConf *tls.Config, config *quic.Config) (quic.EarlyListener, error) {
quicListen = func(c net.PacketConn, tlsConf *tls.Config, config *quic.Config) (QUICEarlyListener, error) {
atomic.StoreInt32(&called, 1)
return <-lns, nil
}
@ -1225,7 +1216,7 @@ var _ = Describe("Server", func() {
It("uses the quic.Config to start the QUIC server", func() {
conf := &quic.Config{HandshakeIdleTimeout: time.Nanosecond}
var receivedConf *quic.Config
quicListenAddr = func(addr string, _ *tls.Config, config *quic.Config) (quic.EarlyListener, error) {
quicListenAddr = func(addr string, _ *tls.Config, config *quic.Config) (QUICEarlyListener, error) {
receivedConf = config
return nil, errors.New("listen err")
}
@ -1241,7 +1232,7 @@ var _ = Describe("Server", func() {
It("errors when listening fails", func() {
testErr := errors.New("listen error")
quicListenAddr = func(addr string, tlsConf *tls.Config, config *quic.Config) (quic.EarlyListener, error) {
quicListenAddr = func(addr string, tlsConf *tls.Config, config *quic.Config) (QUICEarlyListener, error) {
return nil, testErr
}
fullpem, privkey := testdata.GetCertificatePaths()
@ -1251,7 +1242,7 @@ var _ = Describe("Server", func() {
It("supports H3_DATAGRAM", func() {
s.EnableDatagrams = true
var receivedConf *quic.Config
quicListenAddr = func(addr string, _ *tls.Config, config *quic.Config) (quic.EarlyListener, error) {
quicListenAddr = func(addr string, _ *tls.Config, config *quic.Config) (QUICEarlyListener, error) {
receivedConf = config
return nil, errors.New("listen err")
}