@@ -72,7 +72,7 @@ type Options struct {
72
72
type Client interface {
73
73
Metadata (ctx context.Context ) (agentsdk.Metadata , error )
74
74
Listen (ctx context.Context ) (net.Conn , error )
75
- ReportStats (ctx context.Context , log slog.Logger , stats func () * agentsdk.Stats ) (io.Closer , error )
75
+ ReportStats (ctx context.Context , log slog.Logger , statsChan <- chan * agentsdk.Stats , setInterval func (time. Duration ) ) (io.Closer , error )
76
76
PostLifecycle (ctx context.Context , state agentsdk.PostLifecycleRequest ) error
77
77
PostAppHealth (ctx context.Context , req agentsdk.PostAppHealthsRequest ) error
78
78
PostStartup (ctx context.Context , req agentsdk.PostStartupRequest ) error
@@ -112,6 +112,7 @@ func New(options Options) io.Closer {
112
112
logDir : options .LogDir ,
113
113
tempDir : options .TempDir ,
114
114
lifecycleUpdate : make (chan struct {}, 1 ),
115
+ connStatsChan : make (chan * agentsdk.Stats , 1 ),
115
116
}
116
117
a .init (ctx )
117
118
return a
@@ -143,7 +144,8 @@ type agent struct {
143
144
lifecycleMu sync.Mutex // Protects following.
144
145
lifecycleState codersdk.WorkspaceAgentLifecycle
145
146
146
- network * tailnet.Conn
147
+ network * tailnet.Conn
148
+ connStatsChan chan * agentsdk.Stats
147
149
}
148
150
149
151
// runLoop attempts to start the agent in a retry loop.
@@ -351,11 +353,20 @@ func (a *agent) run(ctx context.Context) error {
351
353
return xerrors .New ("agent is closed" )
352
354
}
353
355
356
+ setStatInterval := func (d time.Duration ) {
357
+ network .SetConnStatsCallback (d , 2048 ,
358
+ func (_ , _ time.Time , virtual , _ map [netlogtype.Connection ]netlogtype.Counts ) {
359
+ select {
360
+ case a .connStatsChan <- convertAgentStats (virtual ):
361
+ default :
362
+ a .logger .Warn (ctx , "network stat dropped" )
363
+ }
364
+ },
365
+ )
366
+ }
367
+
354
368
// Report statistics from the created network.
355
- cl , err := a .client .ReportStats (ctx , a .logger , func () * agentsdk.Stats {
356
- stats := network .ExtractTrafficStats ()
357
- return convertAgentStats (stats )
358
- })
369
+ cl , err := a .client .ReportStats (ctx , a .logger , a .connStatsChan , setStatInterval )
359
370
if err != nil {
360
371
a .logger .Error (ctx , "report stats" , slog .Error (err ))
361
372
} else {
@@ -399,10 +410,9 @@ func (a *agent) trackConnGoroutine(fn func()) error {
399
410
400
411
func (a * agent ) createTailnet (ctx context.Context , derpMap * tailcfg.DERPMap ) (_ * tailnet.Conn , err error ) {
401
412
network , err := tailnet .NewConn (& tailnet.Options {
402
- Addresses : []netip.Prefix {netip .PrefixFrom (codersdk .WorkspaceAgentIP , 128 )},
403
- DERPMap : derpMap ,
404
- Logger : a .logger .Named ("tailnet" ),
405
- EnableTrafficStats : true ,
413
+ Addresses : []netip.Prefix {netip .PrefixFrom (codersdk .WorkspaceAgentIP , 128 )},
414
+ DERPMap : derpMap ,
415
+ Logger : a .logger .Named ("tailnet" ),
406
416
})
407
417
if err != nil {
408
418
return nil , xerrors .Errorf ("create tailnet: %w" , err )
0 commit comments