status-go/vendor/github.com/lucas-clemente/quic-go/closed_conn.go

113 lines
2.9 KiB
Go
Raw Normal View History

2022-04-01 16:16:46 +00:00
package quic
import (
"sync"
"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/utils"
)
// A closedLocalConn is a connection that we closed locally.
// When receiving packets for such a connection, we need to retransmit the packet containing the CONNECTION_CLOSE frame,
2022-04-01 16:16:46 +00:00
// with an exponential backoff.
type closedLocalConn struct {
2022-04-01 16:16:46 +00:00
conn sendConn
connClosePacket []byte
closeOnce sync.Once
closeChan chan struct{} // is closed when the connection is closed or destroyed
2022-04-01 16:16:46 +00:00
receivedPackets chan *receivedPacket
counter uint64 // number of packets received
perspective protocol.Perspective
logger utils.Logger
}
var _ packetHandler = &closedLocalConn{}
2022-04-01 16:16:46 +00:00
// newClosedLocalConn creates a new closedLocalConn and runs it.
func newClosedLocalConn(
2022-04-01 16:16:46 +00:00
conn sendConn,
connClosePacket []byte,
perspective protocol.Perspective,
logger utils.Logger,
) packetHandler {
s := &closedLocalConn{
2022-04-01 16:16:46 +00:00
conn: conn,
connClosePacket: connClosePacket,
perspective: perspective,
logger: logger,
closeChan: make(chan struct{}),
receivedPackets: make(chan *receivedPacket, 64),
}
go s.run()
return s
}
func (s *closedLocalConn) run() {
2022-04-01 16:16:46 +00:00
for {
select {
case p := <-s.receivedPackets:
s.handlePacketImpl(p)
case <-s.closeChan:
return
}
}
}
func (s *closedLocalConn) handlePacket(p *receivedPacket) {
2022-04-01 16:16:46 +00:00
select {
case s.receivedPackets <- p:
default:
}
}
func (s *closedLocalConn) handlePacketImpl(_ *receivedPacket) {
2022-04-01 16:16:46 +00:00
s.counter++
// exponential backoff
// only send a CONNECTION_CLOSE for the 1st, 2nd, 4th, 8th, 16th, ... packet arriving
for n := s.counter; n > 1; n = n / 2 {
if n%2 != 0 {
return
}
}
s.logger.Debugf("Received %d packets after sending CONNECTION_CLOSE. Retransmitting.", s.counter)
if err := s.conn.Write(s.connClosePacket); err != nil {
s.logger.Debugf("Error retransmitting CONNECTION_CLOSE: %s", err)
}
}
func (s *closedLocalConn) shutdown() {
2022-04-01 16:16:46 +00:00
s.destroy(nil)
}
func (s *closedLocalConn) destroy(error) {
2022-04-01 16:16:46 +00:00
s.closeOnce.Do(func() {
close(s.closeChan)
})
}
func (s *closedLocalConn) getPerspective() protocol.Perspective {
2022-04-01 16:16:46 +00:00
return s.perspective
}
// A closedRemoteConn is a connection that was closed remotely.
// For such a connection, we might receive reordered packets that were sent before the CONNECTION_CLOSE.
2022-04-01 16:16:46 +00:00
// We can just ignore those packets.
type closedRemoteConn struct {
2022-04-01 16:16:46 +00:00
perspective protocol.Perspective
}
var _ packetHandler = &closedRemoteConn{}
2022-04-01 16:16:46 +00:00
func newClosedRemoteConn(pers protocol.Perspective) packetHandler {
return &closedRemoteConn{perspective: pers}
2022-04-01 16:16:46 +00:00
}
func (s *closedRemoteConn) handlePacket(*receivedPacket) {}
func (s *closedRemoteConn) shutdown() {}
func (s *closedRemoteConn) destroy(error) {}
func (s *closedRemoteConn) getPerspective() protocol.Perspective { return s.perspective }