Skip to content

Commit

Permalink
Rename ConnectionTimeout -> DisconnectTimeout
Browse files Browse the repository at this point in the history
ICE has two distinct states `Disconnected` and `Failed`. We need to
split out these arguments so that we can control (and test) both.

Relates to pion#190
  • Loading branch information
Sean-Der committed Jun 21, 2020
1 parent 6582907 commit 0c308ea
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
37 changes: 24 additions & 13 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ const (
// keepaliveInterval used to keep candidates alive
defaultKeepaliveInterval = 10 * time.Second

// defaultConnectionTimeout used to declare a connection dead
defaultConnectionTimeout = 30 * time.Second
// defaultDisconnectTimeout is the default time till an Agent transitions disconnected
defaultDisconnectTimeout = 5 * time.Second

// defaultFailureTimeout is the default time till an Agent transitions to failed
defaultFailureTimeout = 25 * time.Second //nolint

// timeout for candidate selection, after this time, the best candidate is used
defaultCandidateSelectionTimeout = 10 * time.Second
Expand Down Expand Up @@ -112,9 +115,13 @@ type Agent struct {

candidateTypes []CandidateType

// How long should a pair stay quiet before we declare it dead?
// 0 means never timeout
connectionTimeout time.Duration
// How long connectivity checks can fail before the ICE Agent
// goes to disconnected
disconnectTimeout time.Duration

// How long connectivity checks can fail before the ICE Agent
// goes to failed
failedTimeout time.Duration //nolint

// How often should we send keepalive packets?
// 0 means never
Expand Down Expand Up @@ -230,9 +237,14 @@ type AgentConfig struct {
// MulticastDNSHostName controls the hostname for this agent. If none is specified a random one will be generated
MulticastDNSHostName string

// ConnectionTimeout defaults to 30 seconds when this property is nil.
// If the duration is 0, we will never timeout this connection.
ConnectionTimeout *time.Duration
// DisconnectTimeout defaults to 5 seconds when this property is nil.
// If the duration is 0, the ICE Agent will never go to disconnected
DisconnectTimeout *time.Duration

// FailedTimeout defaults to 25 seconds when this property is nil.
// If the duration is 0, we will never go to failed.
FailedTimeout *time.Duration

// KeepaliveInterval determines how often should we send ICE
// keepalives (should be less then connectiontimeout above)
// when this is nil, it defaults to 10 seconds.
Expand Down Expand Up @@ -489,11 +501,10 @@ func (a *Agent) initWithDefaults(config *AgentConfig) {
a.relayAcceptanceMinWait = *config.RelayAcceptanceMinWait
}

// connectionTimeout used to declare a connection dead
if config.ConnectionTimeout == nil {
a.connectionTimeout = defaultConnectionTimeout
if config.DisconnectTimeout == nil {
a.disconnectTimeout = defaultDisconnectTimeout
} else {
a.connectionTimeout = *config.ConnectionTimeout
a.disconnectTimeout = *config.DisconnectTimeout
}

if config.KeepaliveInterval == nil {
Expand Down Expand Up @@ -753,7 +764,7 @@ func (a *Agent) validateSelectedPair() bool {
return false
}

if (a.connectionTimeout != 0) && (time.Since(selectedPair.remote.LastReceived()) > a.connectionTimeout) {
if (a.disconnectTimeout != 0) && (time.Since(selectedPair.remote.LastReceived()) > a.disconnectTimeout) {
a.updateConnectionState(ConnectionStateDisconnected)
} else {
a.updateConnectionState(ConnectionStateConnected)
Expand Down
4 changes: 2 additions & 2 deletions agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,13 +702,13 @@ func TestConnectionStateCallback(t *testing.T) {
var wg sync.WaitGroup
wg.Add(2)

timeoutDuration := time.Second
disconnectDuration := time.Second
KeepaliveInterval := time.Duration(0)
cfg := &AgentConfig{
Urls: []*URL{},
Trickle: true,
NetworkTypes: supportedNetworkTypes,
ConnectionTimeout: &timeoutDuration,
DisconnectTimeout: &disconnectDuration,
KeepaliveInterval: &KeepaliveInterval,
taskLoopInterval: 500 * time.Millisecond,
}
Expand Down
6 changes: 3 additions & 3 deletions connectivity_vnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,15 +512,15 @@ func TestDisconnectedToConnected(t *testing.T) {

assert.NoError(t, wan.Start())

connectionTimeout := time.Second
disconnectTimeout := time.Second
keepaliveInterval := time.Millisecond * 20

// Create two agents and connect them
controllingAgent, err := NewAgent(&AgentConfig{
NetworkTypes: supportedNetworkTypes,
MulticastDNSMode: MulticastDNSModeDisabled,
Net: net0,
ConnectionTimeout: &connectionTimeout,
DisconnectTimeout: &disconnectTimeout,
KeepaliveInterval: &keepaliveInterval,
taskLoopInterval: keepaliveInterval,
})
Expand All @@ -530,7 +530,7 @@ func TestDisconnectedToConnected(t *testing.T) {
NetworkTypes: supportedNetworkTypes,
MulticastDNSMode: MulticastDNSModeDisabled,
Net: net1,
ConnectionTimeout: &connectionTimeout,
DisconnectTimeout: &disconnectTimeout,
KeepaliveInterval: &keepaliveInterval,
taskLoopInterval: keepaliveInterval,
})
Expand Down
6 changes: 3 additions & 3 deletions transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestTimeout(t *testing.T) {
panic(err)
}

testTimeout(t, ca, defaultConnectionTimeout)
testTimeout(t, ca, defaultDisconnectTimeout)

ca, cb = pipeWithTimeout(5*time.Second, 3*time.Second)
err = cb.Close()
Expand Down Expand Up @@ -261,7 +261,7 @@ func pipe() (*Conn, *Conn) {
return aConn, bConn
}

func pipeWithTimeout(iceTimeout time.Duration, iceKeepalive time.Duration) (*Conn, *Conn) {
func pipeWithTimeout(disconnectTimeout time.Duration, iceKeepalive time.Duration) (*Conn, *Conn) {
var urls []*URL

aNotifier, aConnected := onConnected()
Expand All @@ -273,7 +273,7 @@ func pipeWithTimeout(iceTimeout time.Duration, iceKeepalive time.Duration) (*Con
cfg := &AgentConfig{
Urls: urls,
Trickle: true,
ConnectionTimeout: &iceTimeout,
DisconnectTimeout: &disconnectTimeout,
KeepaliveInterval: &iceKeepalive,
NetworkTypes: supportedNetworkTypes,
}
Expand Down

0 comments on commit 0c308ea

Please sign in to comment.