packer: reduce number of calls to time.Now() when packing packets (#3815)

This commit is contained in:
Marten Seemann 2023-06-03 09:58:41 +03:00 committed by GitHub
parent 56432a8b79
commit 591ab1ab5e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 177 additions and 170 deletions

View file

@ -1811,7 +1811,7 @@ func (s *connection) sendPackets() error {
s.windowUpdateQueue.QueueAll()
if !s.handshakeConfirmed {
packet, err := s.packer.PackCoalescedPacket(false, s.mtuDiscoverer.CurrentSize(), s.version)
packet, err := s.packer.PackCoalescedPacket(false, s.mtuDiscoverer.CurrentSize(), now, s.version)
if err != nil || packet == nil {
return err
}
@ -1928,8 +1928,9 @@ func (s *connection) resetPacingDeadline() {
}
func (s *connection) maybeSendAckOnlyPacket() error {
now := time.Now()
if !s.handshakeConfirmed {
packet, err := s.packer.PackCoalescedPacket(true, s.mtuDiscoverer.CurrentSize(), s.version)
packet, err := s.packer.PackCoalescedPacket(true, s.mtuDiscoverer.CurrentSize(), now, s.version)
if err != nil {
return err
}
@ -1940,8 +1941,7 @@ func (s *connection) maybeSendAckOnlyPacket() error {
return nil
}
now := time.Now()
p, buf, err := s.packer.PackAckOnlyPacket(s.mtuDiscoverer.CurrentSize(), s.version)
p, buf, err := s.packer.PackAckOnlyPacket(s.mtuDiscoverer.CurrentSize(), now, s.version)
if err != nil {
if err == errNothingToPack {
return nil
@ -1955,6 +1955,7 @@ func (s *connection) maybeSendAckOnlyPacket() error {
}
func (s *connection) sendProbePacket(encLevel protocol.EncryptionLevel) error {
now := time.Now()
// Queue probe packets until we actually send out a packet,
// or until there are no more packets to queue.
var packet *coalescedPacket
@ -1963,7 +1964,7 @@ func (s *connection) sendProbePacket(encLevel protocol.EncryptionLevel) error {
break
}
var err error
packet, err = s.packer.MaybePackProbePacket(encLevel, s.mtuDiscoverer.CurrentSize(), s.version)
packet, err = s.packer.MaybePackProbePacket(encLevel, s.mtuDiscoverer.CurrentSize(), now, s.version)
if err != nil {
return err
}
@ -1984,7 +1985,7 @@ func (s *connection) sendProbePacket(encLevel protocol.EncryptionLevel) error {
panic("unexpected encryption level")
}
var err error
packet, err = s.packer.MaybePackProbePacket(encLevel, s.mtuDiscoverer.CurrentSize(), s.version)
packet, err = s.packer.MaybePackProbePacket(encLevel, s.mtuDiscoverer.CurrentSize(), now, s.version)
if err != nil {
return err
}
@ -1992,7 +1993,7 @@ func (s *connection) sendProbePacket(encLevel protocol.EncryptionLevel) error {
if packet == nil || (len(packet.longHdrPackets) == 0 && packet.shortHdrPacket == nil) {
return fmt.Errorf("connection BUG: couldn't pack %s probe packet", encLevel)
}
s.sendPackedCoalescedPacket(packet, time.Now())
s.sendPackedCoalescedPacket(packet, now)
return nil
}
@ -2000,7 +2001,7 @@ func (s *connection) sendProbePacket(encLevel protocol.EncryptionLevel) error {
// If there was nothing to pack, the returned size is 0.
func (s *connection) appendPacket(buf *packetBuffer, maxSize protocol.ByteCount, now time.Time) (protocol.ByteCount, error) {
startLen := buf.Len()
p, err := s.packer.AppendPacket(buf, maxSize, s.version)
p, err := s.packer.AppendPacket(buf, maxSize, now, s.version)
if err != nil {
return 0, err
}
@ -2043,14 +2044,14 @@ func (s *connection) sendConnectionClose(e error) ([]byte, error) {
var transportErr *qerr.TransportError
var applicationErr *qerr.ApplicationError
if errors.As(e, &transportErr) {
packet, err = s.packer.PackConnectionClose(transportErr, s.mtuDiscoverer.CurrentSize(), s.version)
packet, err = s.packer.PackConnectionClose(transportErr, s.mtuDiscoverer.CurrentSize(), time.Now(), s.version)
} else if errors.As(e, &applicationErr) {
packet, err = s.packer.PackApplicationClose(applicationErr, s.mtuDiscoverer.CurrentSize(), s.version)
packet, err = s.packer.PackApplicationClose(applicationErr, s.mtuDiscoverer.CurrentSize(), time.Now(), s.version)
} else {
packet, err = s.packer.PackConnectionClose(&qerr.TransportError{
ErrorCode: qerr.InternalError,
ErrorMessage: fmt.Sprintf("connection BUG: unspecified error type (msg: %s)", e.Error()),
}, s.mtuDiscoverer.CurrentSize(), s.version)
}, s.mtuDiscoverer.CurrentSize(), time.Now(), s.version)
}
if err != nil {
return nil, err