Skip to content

Commit

Permalink
fix(enginenetx): refine the happy-eyeballs algorithm (ooni#1296)
Browse files Browse the repository at this point in the history
We want to pack attempts in parallel, which we also did before when the
interval between attempts was linear.

We need to take into account possible congestion, so we should push back
exponentially, even though the common case for us is probably censorship
(but it is better to do the right thing anyway).

So, let's scale exponentially until we reach 30s. After that, it's fine
to keep attempts evenly spaces, because 30s is quite definitely a huge
interval if we're reasoning in internet time.

Also, change the base value used for TLS handshaking to be 900ms rather
than 300ms, because a TLS handshake is ~3 round trips.

Part of ooni/probe#2531
  • Loading branch information
bassosimone authored and Murphy-OrangeMud committed Feb 13, 2024
1 parent 1a179ab commit 2492145
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
30 changes: 30 additions & 0 deletions internal/enginenetx/happyeyeballs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package enginenetx

import "time"

// happyEyeballsDelay implements an happy-eyeballs like algorithm with the
// given base delay and with the given index. The index is the attempt number
// and the first attempt should have zero as its index.
//
// The algorithm should emit 0 as the first delay, the baseDelay as the
// second delay, and then it should double the base delay at each attempt,
// until we reach the 30 seconds, after which the delay is constant.
//
// By doubling the base delay, we account for the case where there are
// actual issues inside the network. By using this algorithm, we are still
// able to overlap and pack more dialing attempts overall.
func happyEyeballsDelay(baseDelay time.Duration, idx int) time.Duration {
const cutoff = 30 * time.Second
switch {
case idx <= 0:
return 0
case idx == 1:
return baseDelay
default:
delay := baseDelay << (idx - 1)
if delay > cutoff {
delay = cutoff
}
return delay
}
}
41 changes: 41 additions & 0 deletions internal/enginenetx/happyeyeballs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package enginenetx

import (
"fmt"
"testing"
"time"
)

func TestHappyEyeballsDelay(t *testing.T) {
type testcase struct {
idx int
expect time.Duration
}

const delay = 900 * time.Millisecond

cases := []testcase{
{-1, 0}, // make sure we gracefully handle negative numbers (i.e., we don't crash)
{0, 0},
{1, delay},
{2, delay * 2},
{3, delay * 4},
{4, delay * 8},
{5, delay * 16},
{6, delay * 32},
{7, 30 * time.Second},
{8, 30 * time.Second},
{9, 30 * time.Second},
{10, 30 * time.Second},
}

for _, tc := range cases {
t.Run(fmt.Sprintf("delay=%v tc.idx=%v", delay, tc.idx), func(t *testing.T) {
got := happyEyeballsDelay(delay, tc.idx)
if got != tc.expect {
t.Fatalf("with delay=%v tc.idx=%v we got %v but expected %v", delay, tc.idx, got, tc.expect)
}
t.Logf("with delay=%v tc.idx=%v: got %v", delay, tc.idx, got)
})
}
}
9 changes: 7 additions & 2 deletions internal/enginenetx/httpsdialernull.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ type HTTPSDialerNullPolicy struct{}

var _ HTTPSDialerPolicy = &HTTPSDialerNullPolicy{}

// httpsDialerHappyEyeballsDelay is the delay after which we should start a new TCP
// connect and TLS handshake using another tactic. The standard Go library uses a 300ms
// delay for connecting. Because a TCP connect is one round trip and the TLS handshake
// is two round trips (roughly), we multiply this value by three.
const httpsDialerHappyEyeballsDelay = 900 * time.Millisecond

// LookupTactics implements HTTPSDialerPolicy.
func (*HTTPSDialerNullPolicy) LookupTactics(
ctx context.Context, domain, port string, reso model.Resolver) ([]*HTTPSDialerTactic, error) {
Expand All @@ -30,12 +36,11 @@ func (*HTTPSDialerNullPolicy) LookupTactics(
return nil, err
}

const delay = 300 * time.Millisecond
var tactics []*HTTPSDialerTactic
for idx, addr := range addrs {
tactics = append(tactics, &HTTPSDialerTactic{
Endpoint: net.JoinHostPort(addr, port),
InitialDelay: time.Duration(idx) * delay, // zero for the first dial
InitialDelay: happyEyeballsDelay(httpsDialerHappyEyeballsDelay, idx),
SNI: domain,
VerifyHostname: domain,
})
Expand Down

0 comments on commit 2492145

Please sign in to comment.