mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
chore: update tailscale (#6091)
This commit is contained in:
@@ -368,44 +368,55 @@ func (c *Client) AuthAzureInstanceIdentity(ctx context.Context) (AuthenticateRes
|
||||
|
||||
// ReportStats begins a stat streaming connection with the Coder server.
|
||||
// It is resilient to network failures and intermittent coderd issues.
|
||||
func (c *Client) ReportStats(
|
||||
ctx context.Context,
|
||||
log slog.Logger,
|
||||
getStats func() *Stats,
|
||||
) (io.Closer, error) {
|
||||
func (c *Client) ReportStats(ctx context.Context, log slog.Logger, statsChan <-chan *Stats, setInterval func(time.Duration)) (io.Closer, error) {
|
||||
var interval time.Duration
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
exited := make(chan struct{})
|
||||
|
||||
postStat := func(stat *Stats) {
|
||||
var nextInterval time.Duration
|
||||
for r := retry.New(100*time.Millisecond, time.Minute); r.Wait(ctx); {
|
||||
resp, err := c.PostStats(ctx, stat)
|
||||
if err != nil {
|
||||
if !xerrors.Is(err, context.Canceled) {
|
||||
log.Error(ctx, "report stats", slog.Error(err))
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
nextInterval = resp.ReportInterval
|
||||
break
|
||||
}
|
||||
|
||||
if nextInterval != 0 && interval != nextInterval {
|
||||
setInterval(nextInterval)
|
||||
}
|
||||
interval = nextInterval
|
||||
}
|
||||
|
||||
// Send an empty stat to get the interval.
|
||||
postStat(&Stats{ConnsByProto: map[string]int64{}})
|
||||
|
||||
go func() {
|
||||
// Immediately trigger a stats push to get the correct interval.
|
||||
timer := time.NewTimer(time.Nanosecond)
|
||||
defer timer.Stop()
|
||||
defer close(exited)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-timer.C:
|
||||
}
|
||||
|
||||
var nextInterval time.Duration
|
||||
for r := retry.New(100*time.Millisecond, time.Minute); r.Wait(ctx); {
|
||||
resp, err := c.PostStats(ctx, getStats())
|
||||
if err != nil {
|
||||
if !xerrors.Is(err, context.Canceled) {
|
||||
log.Error(ctx, "report stats", slog.Error(err))
|
||||
}
|
||||
continue
|
||||
case stat, ok := <-statsChan:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
nextInterval = resp.ReportInterval
|
||||
break
|
||||
postStat(stat)
|
||||
}
|
||||
timer.Reset(nextInterval)
|
||||
}
|
||||
}()
|
||||
|
||||
return closeFunc(func() error {
|
||||
cancel()
|
||||
<-exited
|
||||
return nil
|
||||
}), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user