use a package-level variable to set the key update frequency

This commit is contained in:
Marten Seemann
2020-09-08 11:11:16 +07:00
parent 72d81f6951
commit a063500d73
4 changed files with 8 additions and 54 deletions

View File

@@ -5,9 +5,10 @@ import (
"fmt"
"io/ioutil"
"net"
"os"
quic "github.com/lucas-clemente/quic-go"
"github.com/lucas-clemente/quic-go/internal/handshake"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
@@ -35,7 +36,7 @@ var _ = Describe("Key Update tests", func() {
BeforeEach(func() {
// update keys as frequently as possible
os.Setenv("QUIC_GO_KEY_UPDATE_INTERVAL", "1")
handshake.KeyUpdateInterval = 1
runServer()
})

View File

@@ -6,8 +6,6 @@ import (
"crypto/tls"
"encoding/binary"
"fmt"
"os"
"strconv"
"time"
"github.com/lucas-clemente/quic-go/internal/protocol"
@@ -17,30 +15,9 @@ import (
"github.com/lucas-clemente/quic-go/logging"
)
// By setting this environment variable, the key update interval can be adjusted.
// This is not needed in production, but useful for integration and interop testing.
// Note that no matter what value is set, a key update is only initiated once it is
// permitted (i.e. once an ACK for a packet sent at the current key phase has been received).
const keyUpdateEnv = "QUIC_GO_KEY_UPDATE_INTERVAL"
var keyUpdateInterval uint64
func init() {
setKeyUpdateInterval()
}
func setKeyUpdateInterval() {
env := os.Getenv(keyUpdateEnv)
if env == "" {
keyUpdateInterval = protocol.KeyUpdateInterval
return
}
interval, err := strconv.ParseUint(env, 10, 64)
if err != nil {
panic(fmt.Sprintf("Cannot parse %s: %s", keyUpdateEnv, err))
}
keyUpdateInterval = interval
}
// KeyUpdateInterval is the maximum number of packets we send or receive before initiating a key update.
// It's a package-level variable to allow modifying it for testing purposes.
var KeyUpdateInterval uint64 = protocol.KeyUpdateInterval
type updatableAEAD struct {
suite *qtls.CipherSuiteTLS13
@@ -92,7 +69,7 @@ func newUpdatableAEAD(rttStats *utils.RTTStats, tracer logging.ConnectionTracer,
largestAcked: protocol.InvalidPacketNumber,
firstRcvdWithCurrentKey: protocol.InvalidPacketNumber,
firstSentWithCurrentKey: protocol.InvalidPacketNumber,
keyUpdateInterval: keyUpdateInterval,
keyUpdateInterval: KeyUpdateInterval,
rttStats: rttStats,
tracer: tracer,
logger: logger,

View File

@@ -4,7 +4,6 @@ import (
"crypto/rand"
"crypto/tls"
"fmt"
"os"
"time"
"github.com/golang/mock/gomock"
@@ -452,29 +451,6 @@ var _ = Describe("Updatable AEAD", func() {
Expect(err).ToNot(HaveOccurred())
})
})
Context("reading the key update env", func() {
AfterEach(func() {
os.Setenv(keyUpdateEnv, "")
setKeyUpdateInterval()
})
It("uses the default value if the env is not set", func() {
setKeyUpdateInterval()
Expect(keyUpdateInterval).To(BeEquivalentTo(protocol.KeyUpdateInterval))
})
It("uses the env", func() {
os.Setenv(keyUpdateEnv, "1337")
setKeyUpdateInterval()
Expect(keyUpdateInterval).To(BeEquivalentTo(1337))
})
It("panics when it can't parse the env", func() {
os.Setenv(keyUpdateEnv, "foobar")
Expect(setKeyUpdateInterval).To(Panic())
})
})
})
})
})

View File

@@ -160,7 +160,7 @@ const MaxAckDelay = 25 * time.Millisecond
// This is the value that should be advertised to the peer.
const MaxAckDelayInclGranularity = MaxAckDelay + TimerGranularity
// KeyUpdateInterval is the maximum number of packets we send or receive before initiating a key udpate.
// KeyUpdateInterval is the maximum number of packets we send or receive before initiating a key update.
const KeyUpdateInterval = 100 * 1000
// Max0RTTQueueingDuration is the maximum time that we store 0-RTT packets in order to wait for the corresponding Initial to be received.