Merge pull request #8 from syncthing/latency

Connected clients should know their own latency
This commit is contained in:
Audrius Butkevicius 2015-09-20 12:47:37 +01:00
commit 3533429563

View File

@ -32,6 +32,7 @@ type ProtocolClient struct {
mut sync.RWMutex mut sync.RWMutex
connected bool connected bool
latency time.Duration
} }
func NewProtocolClient(uri *url.URL, certs []tls.Certificate, invitations chan protocol.SessionInvitation) *ProtocolClient { func NewProtocolClient(uri *url.URL, certs []tls.Certificate, invitations chan protocol.SessionInvitation) *ProtocolClient {
@ -168,6 +169,13 @@ func (c *ProtocolClient) StatusOK() bool {
return con return con
} }
func (c *ProtocolClient) Latency() time.Duration {
c.mut.RLock()
lat := c.latency
c.mut.RUnlock()
return lat
}
func (c *ProtocolClient) String() string { func (c *ProtocolClient) String() string {
return fmt.Sprintf("ProtocolClient@%p", c) return fmt.Sprintf("ProtocolClient@%p", c)
} }
@ -177,11 +185,21 @@ func (c *ProtocolClient) connect() error {
return fmt.Errorf("Unsupported relay schema:", c.URI.Scheme) return fmt.Errorf("Unsupported relay schema:", c.URI.Scheme)
} }
conn, err := tls.Dial("tcp", c.URI.Host, c.config) t0 := time.Now()
tcpConn, err := net.Dial("tcp", c.URI.Host)
if err != nil { if err != nil {
return err return err
} }
c.mut.Lock()
c.latency = time.Since(t0)
c.mut.Unlock()
conn := tls.Client(tcpConn, c.config)
if err = conn.Handshake(); err != nil {
return err
}
if err := conn.SetDeadline(time.Now().Add(10 * time.Second)); err != nil { if err := conn.SetDeadline(time.Now().Add(10 * time.Second)); err != nil {
conn.Close() conn.Close()
return err return err