lib/connections, lib/model: Track last connection duration (ref #7223) (#7242)

This adds a statistic to track the last connection duration per device.
It isn't used for much in this PR, but it's available for #7223 to use
in deciding how to order device connection attempts (deprioritizing
devices that just dropped our connection the last time).
This commit is contained in:
Jakob Borg
2021-01-05 17:45:07 +01:00
committed by GitHub
parent c48eb4241a
commit b13b15758d
13 changed files with 121 additions and 28 deletions
+1 -1
View File
@@ -87,7 +87,7 @@ func (d *quicDialer) Dial(ctx context.Context, _ protocol.DeviceID, uri *url.URL
return internalConn{}, errors.Wrap(err, "open stream")
}
return internalConn{&quicTlsConn{session, stream, createdConn}, connTypeQUICClient, quicPriority}, nil
return newInternalConn(&quicTlsConn{session, stream, createdConn}, connTypeQUICClient, quicPriority), nil
}
type quicDialerFactory struct {
+1 -1
View File
@@ -150,7 +150,7 @@ func (t *quicListener) serve(ctx context.Context) error {
continue
}
t.conns <- internalConn{&quicTlsConn{session, stream, nil}, connTypeQUICServer, quicPriority}
t.conns <- newInternalConn(&quicTlsConn{session, stream, nil}, connTypeQUICServer, quicPriority)
}
}
+1 -1
View File
@@ -63,7 +63,7 @@ func (d *relayDialer) Dial(ctx context.Context, id protocol.DeviceID, uri *url.U
return internalConn{}, err
}
return internalConn{tc, connTypeRelayClient, relayPriority}, nil
return newInternalConn(tc, connTypeRelayClient, relayPriority), nil
}
type relayDialerFactory struct{}
+1 -1
View File
@@ -105,7 +105,7 @@ func (t *relayListener) serve(ctx context.Context) error {
continue
}
t.conns <- internalConn{tc, connTypeRelayServer, relayPriority}
t.conns <- newInternalConn(tc, connTypeRelayServer, relayPriority)
// Poor mans notifier that informs the connection service that the
// relay URI has changed. This can only happen when we connect to a
+16 -2
View File
@@ -35,8 +35,9 @@ type tlsConn interface {
// came from (type, priority).
type internalConn struct {
tlsConn
connType connType
priority int
connType connType
priority int
establishedAt time.Time
}
type connType int
@@ -82,6 +83,15 @@ func (t connType) Transport() string {
}
}
func newInternalConn(tc tlsConn, connType connType, priority int) internalConn {
return internalConn{
tlsConn: tc,
connType: connType,
priority: priority,
establishedAt: time.Now(),
}
}
func (c internalConn) Close() error {
// *tls.Conn.Close() does more than it says on the tin. Specifically, it
// sends a TLS alert message, which might block forever if the
@@ -119,6 +129,10 @@ func (c internalConn) Transport() string {
return transport + "6"
}
func (c internalConn) EstablishedAt() time.Time {
return c.establishedAt
}
func (c internalConn) String() string {
return fmt.Sprintf("%s-%s/%s/%s", c.LocalAddr(), c.RemoteAddr(), c.Type(), c.Crypto())
}
+1 -1
View File
@@ -57,7 +57,7 @@ func (d *tcpDialer) Dial(ctx context.Context, _ protocol.DeviceID, uri *url.URL)
return internalConn{}, err
}
return internalConn{tc, connTypeTCPClient, tcpPriority}, nil
return newInternalConn(tc, connTypeTCPClient, tcpPriority), nil
}
type tcpDialerFactory struct{}
+1 -1
View File
@@ -137,7 +137,7 @@ func (t *tcpListener) serve(ctx context.Context) error {
continue
}
t.conns <- internalConn{tc, connTypeTCPServer, tcpPriority}
t.conns <- newInternalConn(tc, connTypeTCPServer, tcpPriority)
}
}