Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit92abeec

Browse files
committed
remove latency, use quartz
1 parentf3b75c9 commit92abeec

File tree

5 files changed

+160
-201
lines changed

5 files changed

+160
-201
lines changed

‎vpn/client.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ import (
55
"net/http"
66
"net/netip"
77
"net/url"
8-
"time"
98

109
"golang.org/x/xerrors"
1110
"nhooyr.io/websocket"
12-
"tailscale.com/ipn/ipnstate"
1311
"tailscale.com/net/dns"
1412
"tailscale.com/wgengine/router"
1513

@@ -27,7 +25,6 @@ import (
2725
typeConninterface {
2826
CurrentWorkspaceState() (tailnet.WorkspaceUpdate,error)
2927
GetPeerDiagnostics(peerID uuid.UUID) tailnet.PeerDiagnostics
30-
Ping(ctx context.Context,ip netip.Addr) (time.Duration,bool,*ipnstate.PingResult,error)
3128
Close()error
3229
}
3330

‎vpn/tunnel.go

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717

1818
"golang.org/x/exp/maps"
1919
"golang.org/x/xerrors"
20-
"google.golang.org/protobuf/types/known/durationpb"
2120
"google.golang.org/protobuf/types/known/timestamppb"
2221
"tailscale.com/net/dns"
2322
"tailscale.com/util/dnsname"
@@ -28,10 +27,12 @@ import (
2827
"cdr.dev/slog"
2928
"github.com/coder/coder/v2/coderd/util/ptr"
3029
"github.com/coder/coder/v2/tailnet"
30+
"github.com/coder/quartz"
3131
)
3232

33-
// The interval at which the tunnel sends network status updates to the manager.
34-
constnetStatusInterval=30*time.Second
33+
// netStatusInterval is the interval at which the tunnel sends network status updates to the manager.
34+
// This is currently only used to keep `last_handshake` up to date.
35+
constnetStatusInterval=10*time.Second
3536

3637
typeTunnelstruct {
3738
speaker[*TunnelMessage,*ManagerMessage,ManagerMessage]
@@ -58,6 +59,8 @@ type Tunnel struct {
5859
// router and dnsConfigurator may be nil
5960
router router.Router
6061
dnsConfigurator dns.OSConfigurator
62+
63+
clock quartz.Clock
6164
}
6265

6366
typeTunnelOptionfunc(t*Tunnel)
@@ -85,6 +88,7 @@ func NewTunnel(
8588
netLoopDone:make(chanstruct{}),
8689
client:client,
8790
agents:make(map[uuid.UUID]*tailnet.Agent),
91+
clock:quartz.NewReal(),
8892
}
8993

9094
for_,opt:=rangeopts {
@@ -122,7 +126,7 @@ func (t *Tunnel) requestLoop() {
122126
}
123127

124128
func (t*Tunnel)netStatusLoop() {
125-
ticker:=time.NewTicker(netStatusInterval)
129+
ticker:=t.clock.NewTicker(netStatusInterval)
126130
deferticker.Stop()
127131
deferclose(t.netLoopDone)
128132
for {
@@ -213,6 +217,12 @@ func UseAsDNSConfig() TunnelOption {
213217
}
214218
}
215219

220+
funcWithClock(clock quartz.Clock)TunnelOption {
221+
returnfunc(r*Tunnel) {
222+
r.clock=clock
223+
}
224+
}
225+
216226
// ApplyNetworkSettings sends a request to the manager to apply the given network settings
217227
func (t*Tunnel)ApplyNetworkSettings(ctx context.Context,ns*NetworkSettingsRequest)error {
218228
msg,err:=t.speaker.unaryRPC(ctx,&TunnelMessage{
@@ -377,22 +387,18 @@ func (t *Tunnel) createPeerUpdate(update tailnet.WorkspaceUpdate) (*PeerUpdate,
377387
Fqdn:fqdn,
378388
IpAddrs:hostsToIPStrings(agent.Hosts),
379389
LastHandshake:nil,
380-
Latency:nil,
381390
}
382391
}
383392
returnout,nil
384393
}
385394

386-
// Given a list ofagents, populate their network info, andreturn themas proto agents.
395+
// Given a list of`tailnet.Agent`, populate their network info, andconver themto proto agents.
387396
func (t*Tunnel)populateAgents(agents []*tailnet.Agent) ([]*Agent,error) {
388397
ift.conn==nil {
389398
returnnil,xerrors.New("no active connection")
390399
}
391400

392401
out:=make([]*Agent,0,len(agents))
393-
varwg sync.WaitGroup
394-
pingCtx,cancelFunc:=context.WithTimeout(context.Background(),5*time.Second)
395-
defercancelFunc()
396402

397403
for_,agent:=rangeagents {
398404
fqdn:=make([]string,0,len(agent.Hosts))
@@ -406,22 +412,10 @@ func (t *Tunnel) populateAgents(agents []*tailnet.Agent) ([]*Agent, error) {
406412
Fqdn:fqdn,
407413
IpAddrs:hostsToIPStrings(agent.Hosts),
408414
}
409-
agentIP:=tailnet.CoderServicePrefix.AddrFromUUID(agent.ID)
410-
wg.Add(1)
411-
gofunc() {
412-
deferwg.Done()
413-
duration,_,_,err:=t.conn.Ping(pingCtx,agentIP)
414-
iferr!=nil {
415-
return
416-
}
417-
protoAgent.Latency=durationpb.New(duration)
418-
}()
419415
diags:=t.conn.GetPeerDiagnostics(agent.ID)
420-
//nolint:revive // outdated rule
421416
protoAgent.LastHandshake=timestamppb.New(diags.LastWireguardHandshake)
422417
out=append(out,protoAgent)
423418
}
424-
wg.Wait()
425419

426420
returnout,nil
427421
}

‎vpn/tunnel_internal_test.go

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ import (
1313

1414
"github.com/google/uuid"
1515
"github.com/stretchr/testify/require"
16-
"google.golang.org/protobuf/types/known/durationpb"
1716
"google.golang.org/protobuf/types/known/timestamppb"
18-
"tailscale.com/ipn/ipnstate"
1917
"tailscale.com/util/dnsname"
2018

2119
"github.com/coder/coder/v2/tailnet"
2220
"github.com/coder/coder/v2/tailnet/proto"
2321
"github.com/coder/coder/v2/testutil"
22+
"github.com/coder/quartz"
2423
)
2524

2625
funcnewFakeClient(ctx context.Context,t*testing.T)*fakeClient {
@@ -69,10 +68,6 @@ func (f *fakeConn) CurrentWorkspaceState() (tailnet.WorkspaceUpdate, error) {
6968
returnf.state,nil
7069
}
7170

72-
func (*fakeConn)Ping(context.Context, netip.Addr) (time.Duration,bool,*ipnstate.PingResult,error) {
73-
returntime.Millisecond*100,true,&ipnstate.PingResult{},nil
74-
}
75-
7671
func (f*fakeConn)GetPeerDiagnostics(uuid.UUID) tailnet.PeerDiagnostics {
7772
return tailnet.PeerDiagnostics{
7873
LastWireguardHandshake:f.hsTime,
@@ -93,7 +88,7 @@ func TestTunnel_StartStop(t *testing.T) {
9388
client:=newFakeClient(ctx,t)
9489
conn:=newFakeConn(tailnet.WorkspaceUpdate{}, time.Time{})
9590

96-
_,mgr:=setupTunnel(t,ctx,client)
91+
_,mgr:=setupTunnel(t,ctx,client,quartz.NewMock(t))
9792

9893
errCh:=make(chanerror,1)
9994
varresp*TunnelMessage
@@ -159,7 +154,7 @@ func TestTunnel_PeerUpdate(t *testing.T) {
159154
},
160155
}, time.Time{})
161156

162-
tun,mgr:=setupTunnel(t,ctx,client)
157+
tun,mgr:=setupTunnel(t,ctx,client,quartz.NewMock(t))
163158

164159
errCh:=make(chanerror,1)
165160
varresp*TunnelMessage
@@ -224,7 +219,7 @@ func TestTunnel_NetworkSettings(t *testing.T) {
224219
client:=newFakeClient(ctx,t)
225220
conn:=newFakeConn(tailnet.WorkspaceUpdate{}, time.Time{})
226221

227-
tun,mgr:=setupTunnel(t,ctx,client)
222+
tun,mgr:=setupTunnel(t,ctx,client,quartz.NewMock(t))
228223

229224
errCh:=make(chanerror,1)
230225
varresp*TunnelMessage
@@ -287,7 +282,7 @@ func TestTunnel_createPeerUpdate(t *testing.T) {
287282

288283
client:=newFakeClient(ctx,t)
289284

290-
tun,_:=setupTunnel(t,ctx,client)
285+
tun,_:=setupTunnel(t,ctx,client,quartz.NewMock(t))
291286
hsTime:=time.Now().Add(-time.Minute).UTC()
292287
tun.conn=newFakeConn(tailnet.WorkspaceUpdate{},hsTime)
293288

@@ -337,7 +332,6 @@ func TestTunnel_createPeerUpdate(t *testing.T) {
337332
Fqdn: []string{"w1.coder.","w1a1.w1.me.coder.","w1a1.w1.testy.coder."},
338333
IpAddrs: []string{w1a1IP.String()},
339334
LastHandshake:timestamppb.New(hsTime),
340-
Latency:durationpb.New(100*time.Millisecond),
341335
},
342336
},
343337
DeletedWorkspaces: []*Workspace{
@@ -348,7 +342,6 @@ func TestTunnel_createPeerUpdate(t *testing.T) {
348342
Id:w2a1ID[:],Name:"w2a1",WorkspaceId:w2ID[:],
349343
Fqdn: []string{"w2.coder.","w2a1.w2.me.coder.","w2a1.w2.testy.coder."},
350344
IpAddrs: []string{w2a1IP.String()},
351-
Latency:nil,
352345
LastHandshake:nil,
353346
},
354347
},
@@ -360,6 +353,8 @@ func TestTunnel_sendAgentUpdate(t *testing.T) {
360353

361354
ctx:=testutil.Context(t,testutil.WaitShort)
362355

356+
mClock:=quartz.NewMock(t)
357+
363358
wID1:= uuid.UUID{1}
364359
aID1:= uuid.UUID{2}
365360
aID2:= uuid.UUID{3}
@@ -368,8 +363,7 @@ func TestTunnel_sendAgentUpdate(t *testing.T) {
368363
client:=newFakeClient(ctx,t)
369364
conn:=newFakeConn(tailnet.WorkspaceUpdate{},hsTime)
370365

371-
tun,mgr:=setupTunnel(t,ctx,client)
372-
366+
tun,mgr:=setupTunnel(t,ctx,client,mClock)
373367
errCh:=make(chanerror,1)
374368
varresp*TunnelMessage
375369
gofunc() {
@@ -391,9 +385,6 @@ func TestTunnel_sendAgentUpdate(t *testing.T) {
391385
_,ok:=resp.Msg.(*TunnelMessage_Start)
392386
require.True(t,ok)
393387

394-
// `sendAgentUpdate` is a no-op if there's no agents
395-
tun.sendAgentUpdate()
396-
397388
// Inform the tunnel of the initial state
398389
err=tun.Update(tailnet.WorkspaceUpdate{
399390
UpsertedWorkspaces: []*tailnet.Workspace{
@@ -422,8 +413,7 @@ func TestTunnel_sendAgentUpdate(t *testing.T) {
422413
// `sendAgentUpdate` produces the same PeerUpdate message until an agent
423414
// update is received
424415
forrange2 {
425-
// When: we send a (normally scheduled) agent update
426-
tun.sendAgentUpdate()
416+
mClock.AdvanceNext()
427417
// Then: the tunnel sends a PeerUpdate message of agent upserts,
428418
// with the last handshake and latency set
429419
req=testutil.RequireRecvCtx(ctx,t,mgr.requests)
@@ -432,7 +422,6 @@ func TestTunnel_sendAgentUpdate(t *testing.T) {
432422
require.Len(t,req.msg.GetPeerUpdate().UpsertedAgents,1)
433423
require.Equal(t,aID1[:],req.msg.GetPeerUpdate().UpsertedAgents[0].Id)
434424
require.Equal(t,hsTime,req.msg.GetPeerUpdate().UpsertedAgents[0].LastHandshake.AsTime())
435-
require.Equal(t,100*time.Millisecond,req.msg.GetPeerUpdate().UpsertedAgents[0].Latency.AsDuration())
436425
}
437426

438427
// Upsert a new agent
@@ -453,7 +442,7 @@ func TestTunnel_sendAgentUpdate(t *testing.T) {
453442
testutil.RequireRecvCtx(ctx,t,mgr.requests)
454443

455444
// The new update includes the new agent
456-
tun.sendAgentUpdate()
445+
mClock.AdvanceNext()
457446
req=testutil.RequireRecvCtx(ctx,t,mgr.requests)
458447
require.Nil(t,req.msg.Rpc)
459448
require.NotNil(t,req.msg.GetPeerUpdate())
@@ -464,10 +453,8 @@ func TestTunnel_sendAgentUpdate(t *testing.T) {
464453

465454
require.Equal(t,aID1[:],req.msg.GetPeerUpdate().UpsertedAgents[0].Id)
466455
require.Equal(t,hsTime,req.msg.GetPeerUpdate().UpsertedAgents[0].LastHandshake.AsTime())
467-
require.Equal(t,100*time.Millisecond,req.msg.GetPeerUpdate().UpsertedAgents[0].Latency.AsDuration())
468456
require.Equal(t,aID2[:],req.msg.GetPeerUpdate().UpsertedAgents[1].Id)
469457
require.Equal(t,hsTime,req.msg.GetPeerUpdate().UpsertedAgents[1].LastHandshake.AsTime())
470-
require.Equal(t,100*time.Millisecond,req.msg.GetPeerUpdate().UpsertedAgents[1].Latency.AsDuration())
471458

472459
// Delete an agent
473460
err=tun.Update(tailnet.WorkspaceUpdate{
@@ -486,18 +473,17 @@ func TestTunnel_sendAgentUpdate(t *testing.T) {
486473
testutil.RequireRecvCtx(ctx,t,mgr.requests)
487474

488475
// The new update doesn't include the deleted agent
489-
tun.sendAgentUpdate()
476+
mClock.AdvanceNext()
490477
req=testutil.RequireRecvCtx(ctx,t,mgr.requests)
491478
require.Nil(t,req.msg.Rpc)
492479
require.NotNil(t,req.msg.GetPeerUpdate())
493480
require.Len(t,req.msg.GetPeerUpdate().UpsertedAgents,1)
494481
require.Equal(t,aID2[:],req.msg.GetPeerUpdate().UpsertedAgents[0].Id)
495482
require.Equal(t,hsTime,req.msg.GetPeerUpdate().UpsertedAgents[0].LastHandshake.AsTime())
496-
require.Equal(t,100*time.Millisecond,req.msg.GetPeerUpdate().UpsertedAgents[0].Latency.AsDuration())
497483
}
498484

499485
//nolint:revive // t takes precedence
500-
funcsetupTunnel(t*testing.T,ctx context.Context,client*fakeClient) (*Tunnel,*speaker[*ManagerMessage,*TunnelMessage,TunnelMessage]) {
486+
funcsetupTunnel(t*testing.T,ctx context.Context,client*fakeClient,mClock quartz.Clock) (*Tunnel,*speaker[*ManagerMessage,*TunnelMessage,TunnelMessage]) {
501487
mp,tp:=net.Pipe()
502488
t.Cleanup(func() {_=mp.Close() })
503489
t.Cleanup(func() {_=tp.Close() })
@@ -507,7 +493,7 @@ func setupTunnel(t *testing.T, ctx context.Context, client *fakeClient) (*Tunnel
507493
varmgr*speaker[*ManagerMessage,*TunnelMessage,TunnelMessage]
508494
errCh:=make(chanerror,2)
509495
gofunc() {
510-
tunnel,err:=NewTunnel(ctx,logger.Named("tunnel"),tp,client)
496+
tunnel,err:=NewTunnel(ctx,logger.Named("tunnel"),tp,client,WithClock(mClock))
511497
tun=tunnel
512498
errCh<-err
513499
}()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp