mirror of
https://github.com/octoleo/syncthing.git
synced 2025-01-03 07:12:27 +00:00
lib/relay/client: Stricter typing and remove unused code (#7819)
This commit is contained in:
parent
eb6cad7f93
commit
dc38e6ae88
@ -11,58 +11,43 @@ import (
|
||||
|
||||
"github.com/syncthing/syncthing/lib/relay/protocol"
|
||||
"github.com/syncthing/syncthing/lib/svcutil"
|
||||
"github.com/syncthing/syncthing/lib/sync"
|
||||
|
||||
"github.com/thejerf/suture/v4"
|
||||
)
|
||||
|
||||
type relayClientFactory func(uri *url.URL, certs []tls.Certificate, invitations chan protocol.SessionInvitation, timeout time.Duration) RelayClient
|
||||
|
||||
var (
|
||||
supportedSchemes = map[string]relayClientFactory{
|
||||
"relay": newStaticClient,
|
||||
"dynamic+http": newDynamicClient,
|
||||
"dynamic+https": newDynamicClient,
|
||||
}
|
||||
)
|
||||
|
||||
type RelayClient interface {
|
||||
suture.Service
|
||||
Error() error
|
||||
Latency() time.Duration
|
||||
String() string
|
||||
Invitations() chan protocol.SessionInvitation
|
||||
Invitations() <-chan protocol.SessionInvitation
|
||||
URI() *url.URL
|
||||
}
|
||||
|
||||
func NewClient(uri *url.URL, certs []tls.Certificate, timeout time.Duration) (RelayClient, error) {
|
||||
factory, ok := supportedSchemes[uri.Scheme]
|
||||
if !ok {
|
||||
invitations := make(chan protocol.SessionInvitation)
|
||||
|
||||
switch uri.Scheme {
|
||||
case "relay":
|
||||
return newStaticClient(uri, certs, invitations, timeout), nil
|
||||
case "dynamic+http", "dynamic+https":
|
||||
return newDynamicClient(uri, certs, invitations, timeout), nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported scheme: %s", uri.Scheme)
|
||||
}
|
||||
|
||||
invitations := make(chan protocol.SessionInvitation)
|
||||
return factory(uri, certs, invitations, timeout), nil
|
||||
}
|
||||
|
||||
type commonClient struct {
|
||||
svcutil.ServiceWithError
|
||||
|
||||
invitations chan protocol.SessionInvitation
|
||||
mut sync.RWMutex
|
||||
}
|
||||
|
||||
func newCommonClient(invitations chan protocol.SessionInvitation, serve func(context.Context) error, creator string) commonClient {
|
||||
c := commonClient{
|
||||
invitations: invitations,
|
||||
mut: sync.NewRWMutex(),
|
||||
return commonClient{
|
||||
ServiceWithError: svcutil.AsService(serve, creator),
|
||||
invitations: invitations,
|
||||
}
|
||||
c.ServiceWithError = svcutil.AsService(serve, creator)
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *commonClient) Invitations() chan protocol.SessionInvitation {
|
||||
c.mut.RLock()
|
||||
defer c.mut.RUnlock()
|
||||
func (c *commonClient) Invitations() <-chan protocol.SessionInvitation {
|
||||
return c.invitations
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/syncthing/syncthing/lib/osutil"
|
||||
@ -25,10 +26,11 @@ type dynamicClient struct {
|
||||
certs []tls.Certificate
|
||||
timeout time.Duration
|
||||
|
||||
client RelayClient
|
||||
mut sync.RWMutex // Protects client.
|
||||
client *staticClient
|
||||
}
|
||||
|
||||
func newDynamicClient(uri *url.URL, certs []tls.Certificate, invitations chan protocol.SessionInvitation, timeout time.Duration) RelayClient {
|
||||
func newDynamicClient(uri *url.URL, certs []tls.Certificate, invitations chan protocol.SessionInvitation, timeout time.Duration) *dynamicClient {
|
||||
c := &dynamicClient{
|
||||
pooladdr: uri,
|
||||
certs: certs,
|
||||
@ -114,15 +116,6 @@ func (c *dynamicClient) Error() error {
|
||||
return c.client.Error()
|
||||
}
|
||||
|
||||
func (c *dynamicClient) Latency() time.Duration {
|
||||
c.mut.RLock()
|
||||
defer c.mut.RUnlock()
|
||||
if c.client == nil {
|
||||
return time.Hour
|
||||
}
|
||||
return c.client.Latency()
|
||||
}
|
||||
|
||||
func (c *dynamicClient) String() string {
|
||||
return fmt.Sprintf("DynamicClient:%p:%s@%s", c, c.URI(), c.pooladdr)
|
||||
}
|
||||
|
@ -28,12 +28,9 @@ type staticClient struct {
|
||||
connectTimeout time.Duration
|
||||
|
||||
conn *tls.Conn
|
||||
|
||||
connected bool
|
||||
latency time.Duration
|
||||
}
|
||||
|
||||
func newStaticClient(uri *url.URL, certs []tls.Certificate, invitations chan protocol.SessionInvitation, timeout time.Duration) RelayClient {
|
||||
func newStaticClient(uri *url.URL, certs []tls.Certificate, invitations chan protocol.SessionInvitation, timeout time.Duration) *staticClient {
|
||||
c := &staticClient{
|
||||
uri: uri,
|
||||
|
||||
@ -67,10 +64,6 @@ func (c *staticClient) serve(ctx context.Context) error {
|
||||
|
||||
l.Infof("Joined relay %s://%s", c.uri.Scheme, c.uri.Host)
|
||||
|
||||
c.mut.Lock()
|
||||
c.connected = true
|
||||
c.mut.Unlock()
|
||||
|
||||
messages := make(chan interface{})
|
||||
errorsc := make(chan error, 1)
|
||||
|
||||
@ -128,20 +121,6 @@ func (c *staticClient) serve(ctx context.Context) error {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *staticClient) StatusOK() bool {
|
||||
c.mut.RLock()
|
||||
con := c.connected
|
||||
c.mut.RUnlock()
|
||||
return con
|
||||
}
|
||||
|
||||
func (c *staticClient) Latency() time.Duration {
|
||||
c.mut.RLock()
|
||||
lat := c.latency
|
||||
c.mut.RUnlock()
|
||||
return lat
|
||||
}
|
||||
|
||||
func (c *staticClient) String() string {
|
||||
return fmt.Sprintf("StaticClient:%p@%s", c, c.URI())
|
||||
}
|
||||
@ -155,7 +134,6 @@ func (c *staticClient) connect(ctx context.Context) error {
|
||||
return fmt.Errorf("unsupported relay scheme: %v", c.uri.Scheme)
|
||||
}
|
||||
|
||||
t0 := time.Now()
|
||||
timeoutCtx, cancel := context.WithTimeout(ctx, c.connectTimeout)
|
||||
defer cancel()
|
||||
tcpConn, err := dialer.DialContext(timeoutCtx, "tcp", c.uri.Host)
|
||||
@ -163,10 +141,6 @@ func (c *staticClient) connect(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
c.mut.Lock()
|
||||
c.latency = time.Since(t0)
|
||||
c.mut.Unlock()
|
||||
|
||||
conn := tls.Client(tcpConn, c.config)
|
||||
|
||||
if err := conn.SetDeadline(time.Now().Add(c.connectTimeout)); err != nil {
|
||||
@ -185,10 +159,6 @@ func (c *staticClient) connect(ctx context.Context) error {
|
||||
|
||||
func (c *staticClient) disconnect() {
|
||||
l.Debugln(c, "disconnecting")
|
||||
c.mut.Lock()
|
||||
c.connected = false
|
||||
c.mut.Unlock()
|
||||
|
||||
c.conn.Close()
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user