diff --git a/agent.go b/agent.go index 1a8c897..6dfe4f6 100644 --- a/agent.go +++ b/agent.go @@ -69,6 +69,7 @@ type Agent struct { srflxAcceptanceMinWait time.Duration prflxAcceptanceMinWait time.Duration relayAcceptanceMinWait time.Duration + stunGatherTimeout time.Duration tcpPriorityOffset uint16 disableActiveTCP bool diff --git a/agent_config.go b/agent_config.go index 6877313..73ebf8d 100644 --- a/agent_config.go +++ b/agent_config.go @@ -38,6 +38,9 @@ const ( // defaultRelayAcceptanceMinWait is the wait time before nominating a relay candidate defaultRelayAcceptanceMinWait = 2000 * time.Millisecond + // defaultStunGatherTimeout is the wait time for STUN responses + defaultStunGatherTimeout = 5 * time.Second + // defaultMaxBindingRequests is the maximum number of binding requests before considering a pair failed defaultMaxBindingRequests = 7 @@ -136,6 +139,8 @@ type AgentConfig struct { PrflxAcceptanceMinWait *time.Duration // HostAcceptanceMinWait specify a minimum wait time before selecting relay candidates RelayAcceptanceMinWait *time.Duration + // StunGatherTimeout specify a minimum wait time for STUN responses + StunGatherTimeout *time.Duration // Net is the our abstracted network interface for internal development purpose only // (see https://github.com/pion/transport) @@ -222,6 +227,12 @@ func (config *AgentConfig) initWithDefaults(a *Agent) { a.relayAcceptanceMinWait = *config.RelayAcceptanceMinWait } + if config.StunGatherTimeout == nil { + a.stunGatherTimeout = defaultStunGatherTimeout + } else { + a.stunGatherTimeout = *config.StunGatherTimeout + } + if config.TCPPriorityOffset == nil { a.tcpPriorityOffset = defaultTCPPriorityOffset } else { diff --git a/gather.go b/gather.go index fe56cc7..e97fe43 100644 --- a/gather.go +++ b/gather.go @@ -12,7 +12,6 @@ import ( "net/netip" "reflect" "sync" - "time" "github.com/pion/dtls/v2" "github.com/pion/ice/v3/internal/fakenet" @@ -22,10 +21,6 @@ import ( "github.com/pion/turn/v3" ) -const ( - stunGatherTimeout = time.Second * 5 -) - // Close a net.Conn and log if we have a failure func closeConnAndLog(c io.Closer, log logging.LeveledLogger, msg string, args ...interface{}) { if c == nil || (reflect.ValueOf(c).Kind() == reflect.Ptr && reflect.ValueOf(c).IsNil()) { @@ -479,7 +474,7 @@ func (a *Agent) gatherCandidatesSrflxUDPMux(ctx context.Context, urls []*stun.UR return } - xorAddr, err := a.udpMuxSrflx.GetXORMappedAddr(serverAddr, stunGatherTimeout) + xorAddr, err := a.udpMuxSrflx.GetXORMappedAddr(serverAddr, a.stunGatherTimeout) if err != nil { a.log.Warnf("Failed get server reflexive address %s %s: %v", network, url, err) return @@ -564,7 +559,7 @@ func (a *Agent) gatherCandidatesSrflx(ctx context.Context, urls []*stun.URI, net } }() - xorAddr, err := stunx.GetXORMappedAddr(conn, serverAddr, stunGatherTimeout) + xorAddr, err := stunx.GetXORMappedAddr(conn, serverAddr, a.stunGatherTimeout) if err != nil { closeConnAndLog(conn, a.log, "failed to get server reflexive address %s %s: %v", network, url, err) return