Skip to content

Modern Docker Management, Designed for Everyone

perf: share stats sampling and dashboard producers, back off idle job scans (#4486)

Kyle Mendellb31fae3c7543
diff --git a/backend/api/ws/containers.go b/backend/api/ws/containers.go
index b196a7e9e..171286a7c 100644
--- a/backend/api/ws/containers.go
+++ b/backend/api/ws/containers.go
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json/v2"
"log/slog"
+ "strings"
"sync/atomic"
"time"
@@ -13,16 +14,22 @@ import (
"github.com/labstack/echo/v5"
kit "go.getarcane.app/kit/pkg"
+ "github.com/getarcaneapp/arcane/backend/v2/internal/container"
wshub "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/ws"
)
const (
- // containersStatsInterval is the pause between batches and the oldest cached sample a batch reuses.
+ // containersStatsInterval is the wall-clock period every subscription samples on.
containersStatsInterval = 2 * time.Second
// containersStatsReadLimit fits a subscription of 500 full container IDs, the most one batch samples.
containersStatsReadLimit = 64 << 10
)
+type containersStatsResult struct {
+ samples map[string]*containertypes.ResourceSample
+ err error
+}
+
// ContainersStats streams resource samples for a client-chosen set of containers over one WebSocket.
// The client sends a ResourceSampleSubscription whenever its set changes; the server answers with a
// ResourceSampleBatch every couple of seconds.
@@ -45,8 +52,8 @@ func (h *WebSocketHandler) ContainersStats(c *echo.Context) error {
defer cancel()
var ids atomic.Pointer[[]string]
- wake := make(chan struct{}, 1)
- go readContainersStatsPump(ctx, cancel, conn, &ids, wake)
+ subscribed := make(chan struct{}, 1)
+ go readContainersStatsPump(ctx, cancel, conn, &ids, subscribed)
// The pong is serviced by readContainersStatsPump.
go wshub.KeepAlive(ctx, cancel, conn)
@@ -64,37 +71,54 @@ func (h *WebSocketHandler) ContainersStats(c *echo.Context) error {
select {
case <-ctx.Done():
return nil
- case <-wake:
+ case <-subscribed:
}
- for {
- samples, err := h.containerService.SampleResources(ctx, kit.FromPtr(ids.Load()), containersStatsInterval)
- switch {
- case ctx.Err() != nil:
- return nil
- case err != nil:
- slog.DebugContext(ctx, "Failed to sample container stats", "envId", c.Param("id"), "error", err)
- if send(containertypes.ResourceSampleBatch{Error: "Failed to sample container stats: " + err.Error(), Timestamp: time.Now()}) != nil {
- return nil
+ for ctx.Err() == nil {
+ want := kit.FromPtr(ids.Load())
+ want = want[:min(len(want), container.ResourceSampleLimit)]
+ subCtx, cancelSub := context.WithCancel(ctx)
+ go func() {
+ defer cancelSub()
+ select {
+ case <-subscribed:
+ case <-subCtx.Done():
}
- default:
- if send(containertypes.ResourceSampleBatch{Samples: samples, Timestamp: time.Now()}) != nil {
- return nil
- }
- }
-
- select {
- case <-ctx.Done():
+ }()
+ h.containersStatsHub.Subscribe(subCtx, strings.Join(want, ","),
+ func(runCtx context.Context, publish func(containersStatsResult)) {
+ for {
+ samples, err := h.containerService.SampleResources(runCtx, want, containersStatsInterval/4)
+ if runCtx.Err() != nil {
+ return
+ }
+ if err != nil {
+ slog.DebugContext(runCtx, "Failed to sample container stats", "envId", c.Param("id"), "error", err)
+ }
+ publish(containersStatsResult{samples: samples, err: err})
+ select {
+ case <-runCtx.Done():
+ return
+ case <-time.After(time.Until(time.Now().Truncate(containersStatsInterval).Add(containersStatsInterval))):
+ }
+ }
+ },
+ func(result containersStatsResult) bool {
+ if result.err != nil {
+ return send(containertypes.ResourceSampleBatch{Error: "Failed to sample container stats: " + result.err.Error(), Timestamp: time.Now()}) == nil
+ }
+ return send(containertypes.ResourceSampleBatch{Samples: result.samples, Timestamp: time.Now()}) == nil
+ })
+ if subCtx.Err() == nil {
return nil
- case <-wake:
- case <-time.After(containersStatsInterval):
}
}
+ return nil
}
// readContainersStatsPump is the single reader for the ContainersStats websocket. It stores each
// subscription and wakes the sampler; malformed frames are ignored. Do not add other readers.
-func readContainersStatsPump(ctx context.Context, cancel context.CancelFunc, conn *websocket.Conn, ids *atomic.Pointer[[]string], wake chan<- struct{}) {
+func readContainersStatsPump(ctx context.Context, cancel context.CancelFunc, conn *websocket.Conn, ids *atomic.Pointer[[]string], subscribed chan<- struct{}) {
defer cancel()
for {
_, data, err := conn.Read(ctx)
@@ -107,7 +131,7 @@ func readContainersStatsPump(ctx context.Context, cancel context.CancelFunc, con
}
ids.Store(new(subscription.ContainerIDs))
select {
- case wake <- struct{}{}:
+ case subscribed <- struct{}{}:
default:
}
}
diff --git a/backend/api/ws/handler.go b/backend/api/ws/handler.go
index 8e29180d8..f755cfc4c 100644
--- a/backend/api/ws/handler.go
+++ b/backend/api/ws/handler.go
@@ -12,6 +12,7 @@ import (
systemtypes "github.com/getarcaneapp/arcane/types/v2/system"
"github.com/labstack/echo/v5"
"github.com/samber/hot"
+ "go.getarcane.app/streams/agg"
"go.getarcane.app/sys/cgroup"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
@@ -72,6 +73,7 @@ type WebSocketHandler struct {
running bool
}
containerStatsHubs sync.Map
+ containersStatsHub *agg.Hub[containersStatsResult]
cgroupCache *cgroup.Cache
gpuMonitor *systemlib.GPUMonitor
@@ -102,6 +104,7 @@ func NewWebSocketHandler(
systemService: systemService,
diagnosticsService: diagnosticsService,
logStreams: make(map[string]*wsLogStream),
+ containersStatsHub: agg.NewHub[containersStatsResult](),
cgroupCache: cgroup.NewCache(cgroupCacheTTL),
gpuMonitor: systemlib.NewGPUMonitor(cfg.GPUMonitoringEnabled, cfg.GPUType),
diskUsagePathCache: hot.NewHotCache[struct{}, string](hot.LRU, 1).
diff --git a/backend/internal/container/children/stats/service.go b/backend/internal/container/children/stats/service.go
index 1c349cb02..da20c26a2 100644
--- a/backend/internal/container/children/stats/service.go
+++ b/backend/internal/container/children/stats/service.go
@@ -130,6 +130,9 @@ func (s *Service) Stream(ctx context.Context, containerID string, statsChan chan
CurrentHistorySample: currentSample,
StatsHistory: s.history.Record(containerID, currentSample, !historySent, kit.Ternary(statsData.Read.IsZero(), time.Now(), statsData.Read)),
}
+ if historySent && statsData.PreCPUStats.SystemUsage != 0 {
+ s.cache.Set(s.dockerHost()+"\x00"+containerID, cachedStats{stats: statsData, fetchedAt: time.Now()})
+ }
historySent = true
select {
diff --git a/backend/internal/container/service.go b/backend/internal/container/service.go
index e2e245c89..e0d7bd455 100644
--- a/backend/internal/container/service.go
+++ b/backend/internal/container/service.go
@@ -1477,13 +1477,13 @@ func (s *ContainerService) StreamStats(ctx context.Context, containerID string,
return s.stats.Stream(ctx, containerID, statsChan)
}
-// containerResourceSampleLimit caps how many requested containers one SampleResources call reads.
-const containerResourceSampleLimit = 500
+// ResourceSampleLimit caps how many requested containers one SampleResources call reads.
+const ResourceSampleLimit = 500
-// SampleResources samples the running containers among the first containerResourceSampleLimit ids,
+// SampleResources samples the running containers among the first ResourceSampleLimit ids,
// refetching samples older than maxAge. Blank, stopped and unknown IDs are omitted.
func (s *ContainerService) SampleResources(ctx context.Context, ids []string, maxAge time.Duration) (map[string]*containertypes.ResourceSample, error) {
- requested := slices.DeleteFunc(slices.Clone(ids[:min(len(ids), containerResourceSampleLimit)]), func(id string) bool {
+ requested := slices.DeleteFunc(slices.Clone(ids[:min(len(ids), ResourceSampleLimit)]), func(id string) bool {
return strings.TrimSpace(id) == ""
})
if len(requested) == 0 {
diff --git a/backend/internal/container/service_test.go b/backend/internal/container/service_test.go
index e1bd2367e..84f2f60ae 100644
--- a/backend/internal/container/service_test.go
+++ b/backend/internal/container/service_test.go
@@ -1391,7 +1391,7 @@ func TestResourceSortCancelledContextFailsRequest(t *testing.T) {
}
func TestSampleResourcesFiltersRequestedIDs(t *testing.T) {
- capped := make([]string, containerResourceSampleLimit+1)
+ capped := make([]string, ResourceSampleLimit+1)
for i := range capped {
capped[i] = fmt.Sprintf("c%03d", i)
}
@@ -1419,7 +1419,7 @@ func TestSampleResourcesFiltersRequestedIDs(t *testing.T) {
name: "ids past the limit are ignored",
running: capped,
request: capped,
- want: capped[:containerResourceSampleLimit],
+ want: capped[:ResourceSampleLimit],
},
{
name: "no ids samples nothing",
diff --git a/backend/internal/dashboard/handler.go b/backend/internal/dashboard/handler.go
index 64de7c45d..cd7efa142 100644
--- a/backend/internal/dashboard/handler.go
+++ b/backend/internal/dashboard/handler.go
@@ -15,6 +15,7 @@ import (
"github.com/getarcaneapp/arcane/types/v2/image"
"github.com/getarcaneapp/arcane/types/v2/version"
"github.com/getarcaneapp/arcane/types/v2/volume"
+ kit "go.getarcane.app/kit/pkg"
"go.getarcane.app/streams/agg"
"github.com/getarcaneapp/arcane/backend/v2/internal/common"
@@ -31,6 +32,7 @@ type DashboardHandler struct {
// remoteStreamHub shares one poller per remote environment across every
// connected stream client instead of polling per client × environment.
remoteStreamHub *agg.Hub[dashboard.StreamEvent]
+ localStreamHub *agg.Hub[dashboard.StreamEvent]
}
type GetDashboardInput struct {
@@ -55,6 +57,7 @@ func NewHandler(dashboardService *DashboardService, environmentService *environm
dashboardService: dashboardService,
environmentService: environmentService,
remoteStreamHub: agg.NewHub[dashboard.StreamEvent](),
+ localStreamHub: agg.NewHub[dashboard.StreamEvent](),
}
}
@@ -96,56 +99,62 @@ func trimDashboardStreamSnapshotInternal(snapshot *dashboard.Snapshot) *dashboar
}
func (h *DashboardHandler) RunLocalStreamProducer(ctx context.Context, debugAllGood bool, events chan<- dashboard.StreamEvent) {
- lastError := ""
-
- poll := func() {
- snapshot, err := h.dashboardService.GetSnapshot(ctx, DashboardActionItemsOptions{
- DebugAllGood: debugAllGood,
- }, false)
- if err == nil && snapshot == nil {
- err = common.Classify(common.ErrUnavailable, errors.New("dashboard snapshot not available"))
- }
- if err != nil {
- if ctx.Err() != nil {
- return
- }
- // A failing snapshot must not end the stream; surface the error
- // once per distinct message and keep polling.
- if msg := err.Error(); msg != lastError {
- lastError = msg
- agg.Send(ctx, events, dashboard.StreamEvent{
- Type: "error",
+ h.localStreamHub.Subscribe(ctx, kit.Ternary(debugAllGood, "local:debugAllGood", "local"),
+ func(runCtx context.Context, publish func(dashboard.StreamEvent)) {
+ lastError := ""
+
+ poll := func() {
+ snapshot, err := h.dashboardService.GetSnapshot(runCtx, DashboardActionItemsOptions{
+ DebugAllGood: debugAllGood,
+ }, false)
+ if err == nil && snapshot == nil {
+ err = common.Classify(common.ErrUnavailable, errors.New("dashboard snapshot not available"))
+ }
+ if err != nil {
+ if runCtx.Err() != nil {
+ return
+ }
+ // A failing snapshot must not end the stream; surface the error
+ // once per distinct message and keep polling.
+ if msg := err.Error(); msg != lastError {
+ lastError = msg
+ publish(dashboard.StreamEvent{
+ Type: "error",
+ EnvironmentID: "0",
+ Error: msg,
+ Timestamp: time.Now(),
+ })
+ }
+ return
+ }
+ lastError = ""
+ // Already built without tables; shared with other subscribers, so it
+ // must not be trimmed (mutated) here.
+ publish(dashboard.StreamEvent{
+ Type: "snapshot",
EnvironmentID: "0",
- Error: msg,
+ Snapshot: snapshot,
Timestamp: time.Now(),
})
}
- return
- }
- lastError = ""
- // Already built without tables; shared with other subscribers, so it
- // must not be trimmed (mutated) here.
- agg.Send(ctx, events, dashboard.StreamEvent{
- Type: "snapshot",
- EnvironmentID: "0",
- Snapshot: snapshot,
- Timestamp: time.Now(),
- })
- }
- poll()
+ poll()
- ticker := time.NewTicker(dashboardStreamLocalPollInterval)
- defer ticker.Stop()
+ ticker := time.NewTicker(dashboardStreamLocalPollInterval)
+ defer ticker.Stop()
- for {
- select {
- case <-ctx.Done():
- return
- case <-ticker.C:
- poll()
- }
- }
+ for {
+ select {
+ case <-runCtx.Done():
+ return
+ case <-ticker.C:
+ poll()
+ }
+ }
+ },
+ func(event dashboard.StreamEvent) bool {
+ return agg.Send(ctx, events, event)
+ })
}
// RunRemoteStreamPollers keeps one poller goroutine per
diff --git a/backend/internal/version/service.go b/backend/internal/version/service.go
index 7e74ebcc7..740b39da8 100644
--- a/backend/internal/version/service.go
+++ b/backend/internal/version/service.go
@@ -44,6 +44,13 @@ type latestRelease struct {
PublishedAt string
}
+type currentImageInfo struct {
+ Tag string
+ Digest string
+ ImageRef string
+ ImageID string
+}
+
type VersionService struct {
httpClient *http.Client
cache *hot.HotCache[struct{}, latestRelease]
@@ -55,6 +62,7 @@ type VersionService struct {
imageUpdateService *imageupdate.ImageUpdateService
settingsService *settings.SettingsService
streamHub *agg.Hub[version.StreamEvent]
+ currentImage *hot.HotCache[struct{}, currentImageInfo]
}
func NewVersionService(
@@ -79,6 +87,7 @@ func NewVersionService(
imageUpdateService: imageUpdateService,
settingsService: settingsService,
streamHub: agg.NewHub[version.StreamEvent](),
+ currentImage: hot.NewHotCache[struct{}, currentImageInfo](hot.LRU, 1).Build(),
}
loader := func(_ []struct{}) (map[struct{}]latestRelease, error) {
ctx, cancel := context.WithTimeout(context.Background(), defaultRequestTimeout) //nolint:forbidigo // Cache revalidation runs independently of request cancellation.
@@ -288,7 +297,6 @@ func (s *VersionService) GetAppVersionInfo(ctx context.Context) *version.Info {
// Semver versions (including prereleases like 2.4.0-next.1) display as v<version>.
displayVersion := strings.TrimPrefix(strings.TrimSpace(s.version), "v")
- // Always detect current image info
currentTag, currentDigest, currentImageRef, currentImageID := s.detectCurrentImageInfo(ctx)
// Update fields stay empty when update checks are disabled.
@@ -390,6 +398,26 @@ func (s *VersionService) storedOrDigestBasedUpdate(ctx context.Context, currentI
// detectCurrentImageInfo attempts to detect the current container's image tag and digest
func (s *VersionService) detectCurrentImageInfo(ctx context.Context) (tag, digest, imageRef, imageID string) {
+ if cached, found, _ := s.currentImage.Get(struct{}{}); found {
+ return cached.Tag, cached.Digest, cached.ImageRef, cached.ImageID
+ }
+ defer func() {
+ if ctx.Err() != nil {
+ return
+ }
+ detected := currentImageInfo{
+ Tag: tag,
+ Digest: digest,
+ ImageRef: imageRef,
+ ImageID: imageID,
+ }
+ if digest != "" {
+ s.currentImage.Set(struct{}{}, detected)
+ } else {
+ s.currentImage.SetWithTTL(struct{}{}, detected, time.Minute)
+ }
+ }()
+
if s.dockerService == nil {
slog.DebugContext(ctx, "detectCurrentImageInfo: dockerService is nil")
return "", "", "", ""
diff --git a/backend/pkg/scheduler/runs/actors.go b/backend/pkg/scheduler/runs/actors.go
index 1201c95a7..0f1560899 100644
--- a/backend/pkg/scheduler/runs/actors.go
+++ b/backend/pkg/scheduler/runs/actors.go
@@ -28,6 +28,7 @@ const (
healthExecutorType = "job-health-executor"
orphanedDispatchAge = 15 * time.Minute
outcomePersistGrace = 30 * time.Second
+ coordinatorIdleWake = 2 * time.Minute
)
type coordinatorActor struct {
@@ -342,25 +343,32 @@ func (a *coordinatorActor) Alarm(ctx context.Context, name string, data actor.En
if setStateErr := a.q.service.SetState(ctx, coordinatorType, a.id, state, nil); setStateErr != nil {
return setStateErr
}
- return a.flush(ctx, &state)
+ if flushErr := a.flush(ctx, &state); flushErr != nil {
+ return flushErr
+ }
+ a.q.signal()
+ return nil
}
// repair flushes coordinators so schedule alarms and dispatches are restored.
// Unless all is set, it skips records without unfinished runs so idle coordinators can deactivate.
-func (q *Coordinator) repair(ctx context.Context, all bool) error {
+func (q *Coordinator) repair(ctx context.Context, all bool) (busy bool, err error) {
records, err := q.Records(ctx)
if err != nil {
- return err
+ return false, err
+ }
+ unfinished := func(record st.QueueRecord) bool {
+ return slices.ContainsFunc(record.Runs, func(run st.Run) bool { return !run.Status.Terminal() && run.Status != st.NeedsAttention })
}
for _, record := range records {
- if !all && !slices.ContainsFunc(record.Runs, func(run st.Run) bool { return !run.Status.Terminal() && run.Status != st.NeedsAttention }) {
+ if !all && !unfinished(record) {
continue
}
if _, invokeErr := q.service.Invoke(ctx, coordinatorType, kit.SHA256Hex(record.EnvironmentID+"\x00"+record.JobID), "repair", nil); invokeErr != nil {
- return invokeErr
+ return false, invokeErr
}
}
- return nil
+ return slices.ContainsFunc(records, unfinished), nil
}
func (q *Coordinator) importLegacy(ctx context.Context) error {
diff --git a/backend/pkg/scheduler/runs/actors_test.go b/backend/pkg/scheduler/runs/actors_test.go
index 44513a751..978b6d7f3 100644
--- a/backend/pkg/scheduler/runs/actors_test.go
+++ b/backend/pkg/scheduler/runs/actors_test.go
@@ -268,10 +268,10 @@ func TestReconciliationRepairsMissingAlarm(t *testing.T) {
record, err := q.ScheduleState(t.Context(), "repair")
require.NoError(t, err)
id := kit.SHA256Hex("0\x00repair")
- err = q.repair(t.Context(), true)
+ _, err = q.repair(t.Context(), true)
require.NoError(t, err)
require.NoError(t, q.service.DeleteAlarm(t.Context(), coordinatorType, id, fmt.Sprintf("schedule-%d-%d", record.Generation, record.Sequence)))
- err = q.repair(t.Context(), true)
+ _, err = q.repair(t.Context(), true)
require.NoError(t, err)
require.NoError(t, q.service.DeleteAlarm(t.Context(), coordinatorType, id, fmt.Sprintf("schedule-%d-%d", record.Generation, record.Sequence)))
after, err := q.ScheduleState(t.Context(), "repair")
@@ -358,7 +358,7 @@ func TestLostDispatchAcknowledgementRetainsOneExecution(t *testing.T) {
state.Dispatches[key] = intent
state.Revision++
require.NoError(t, q.service.SetState(t.Context(), coordinatorType, id, state, nil))
- err = q.repair(t.Context(), false)
+ _, err = q.repair(t.Context(), false)
require.NoError(t, err)
require.NoError(t, q.service.GetState(t.Context(), coordinatorType, id, &state))
require.Equal(t, original, state.Dispatches[key].JobID)
diff --git a/backend/pkg/scheduler/runs/coordinator.go b/backend/pkg/scheduler/runs/coordinator.go
index a97c668f4..d86dc9e7d 100644
--- a/backend/pkg/scheduler/runs/coordinator.go
+++ b/backend/pkg/scheduler/runs/coordinator.go
@@ -303,9 +303,11 @@ func (q *Coordinator) dispatch(ctx context.Context) {
}
nextRetention = time.Now().Add(time.Hour)
}
- nextWake := time.Now().Add(15 * time.Second)
enabled := q.enabled.Load()
- if err := q.repair(ctx, fullRepair); err != nil {
+ busy, err := q.repair(ctx, fullRepair)
+ pendingActivity := q.hasPendingActivitySync(ctx)
+ nextWake := time.Now().Add(kit.Ternary(busy || pendingActivity, 15*time.Second, coordinatorIdleWake))
+ if err != nil {
if ctx.Err() == nil {
slog.ErrorContext(ctx, "Job queue dispatch failed", "error", err)
}
@@ -316,7 +318,7 @@ func (q *Coordinator) dispatch(ctx context.Context) {
if nextRetention.Before(nextWake) {
nextWake = nextRetention
}
- if q.hasPendingActivitySync(ctx) {
+ if pendingActivity {
activityRetry := time.Now().Add(5 * time.Second)
if activityRetry.Before(nextWake) {
nextWake = activityRetry
diff --git a/backend/pkg/utils/francis/runtime.go b/backend/pkg/utils/francis/runtime.go
index 68dbb498b..3f0d795f0 100644
--- a/backend/pkg/utils/francis/runtime.go
+++ b/backend/pkg/utils/francis/runtime.go
@@ -80,7 +80,7 @@ func New(databaseURL, encryptionKey, instanceID, port string, options ...local.H
options: []local.HostOption{
local.WithAddress(address), providerOption(storeURL),
local.WithMaxHosts(1), local.WithHostHealthCheckDeadline(150 * time.Second),
- local.WithShutdownGracePeriod(10 * time.Second), local.WithAlarmsPollInterval(time.Second),
+ local.WithShutdownGracePeriod(10 * time.Second), local.WithAlarmsPollInterval(10 * time.Second),
local.WithAlarmsFetchAheadInterval(30 * time.Second), local.WithAlarmsLeaseDuration(180 * time.Second),
},
}
diff --git a/frontend/src/lib/components/arcane-table/arcane-table-pagination.svelte b/frontend/src/lib/components/arcane-table/arcane-table-pagination.svelte
index f33215c87..062a4b08e 100644
--- a/frontend/src/lib/components/arcane-table/arcane-table-pagination.svelte
+++ b/frontend/src/lib/components/arcane-table/arcane-table-pagination.svelte
@@ -28,6 +28,7 @@
setPageSize: (limit: number) => void;
} = $props();
+ const uid = $props.id();
const isAllSelected = $derived(pageSize === TABLE_PAGE_SIZE_ALL);
const displayValue = $derived(isAllSelected ? m.common_all() : String(pageSize));
const selectValue = $derived(isAllSelected ? 'all' : String(pageSize));
@@ -47,9 +48,9 @@
</div>
<div class="order-1 flex flex-col gap-4 sm:order-2 sm:flex-row sm:items-center sm:space-x-6 lg:space-x-8">
<div class="flex items-center justify-between space-x-2 sm:justify-start">
- <p class="text-sm font-medium">{m.common_rows_per_page()}</p>
+ <p id="{uid}-rows-per-page" class="text-sm font-medium">{m.common_rows_per_page()}</p>
<Select.Root allowDeselect={false} type="single" bind:value={() => selectValue, handlePageSizeChange}>
- <Select.Trigger aria-label={m.common_rows_per_page()} class="h-11 w-17.5 sm:h-8">
+ <Select.Trigger id="{uid}-page-size" aria-labelledby="{uid}-rows-per-page {uid}-page-size" class="h-11 w-17.5 sm:h-8">
{displayValue}
</Select.Trigger>
<Select.Content side="top">
diff --git a/frontend/src/lib/utils/ws.ts b/frontend/src/lib/utils/ws.ts
index 57d6006dc..d92aa3e54 100644
--- a/frontend/src/lib/utils/ws.ts
+++ b/frontend/src/lib/utils/ws.ts
@@ -23,6 +23,7 @@ export interface ReconnectWSOptions<T> {
maxBackoff?: number;
autoConnect?: boolean;
shouldReconnect?: () => boolean;
+ pauseWhenHidden?: boolean;
}
export class ReconnectingWebSocket<T = unknown> {
@@ -34,6 +35,8 @@ export class ReconnectingWebSocket<T = unknown> {
private connecting = false;
private generation = 0;
private reconnectTimer: ReturnType<typeof setTimeout> | null = null;
+ private paused = false;
+ private listeningVisibility = false;
constructor(opts: ReconnectWSOptions<T>) {
this.opts = opts;
@@ -44,11 +47,19 @@ export class ReconnectingWebSocket<T = unknown> {
async connect() {
this.close();
this.closed = false;
+ if (this.opts.pauseWhenHidden && typeof document !== 'undefined' && !this.listeningVisibility) {
+ document.addEventListener('visibilitychange', this.onVisibility);
+ this.listeningVisibility = true;
+ }
await this.connectOnce();
}
async connectOnce() {
if (this.closed || this.connecting) return;
+ if (this.shouldPause()) {
+ this.paused = true;
+ return;
+ }
if (this.ws) {
try {
@@ -124,6 +135,10 @@ export class ReconnectingWebSocket<T = unknown> {
if (currentSessionSignal().aborted || (this.opts.shouldReconnect && !this.opts.shouldReconnect())) {
return;
}
+ if (this.shouldPause()) {
+ this.paused = true;
+ return;
+ }
if (this.reconnectTimer) {
clearTimeout(this.reconnectTimer);
@@ -140,6 +155,40 @@ export class ReconnectingWebSocket<T = unknown> {
}, backoff);
}
+ private shouldPause() {
+ return this.opts.pauseWhenHidden === true && (this.paused || (typeof document !== 'undefined' && document.hidden));
+ }
+
+ private readonly onVisibility = () => {
+ if (this.closed) return;
+ if (document.hidden) {
+ this.paused = true;
+ this.generation++;
+ if (this.reconnectTimer) {
+ clearTimeout(this.reconnectTimer);
+ this.reconnectTimer = null;
+ }
+ const socket = this.ws;
+ this.ws = null;
+ this.connecting = false;
+ try {
+ socket?.close();
+ } catch {}
+ return;
+ }
+ if (!this.paused) return;
+ this.paused = false;
+ this.attempt = 0;
+ void this.connectOnce();
+ };
+
+ private removeVisibilityListener() {
+ this.paused = false;
+ if (!this.listeningVisibility) return;
+ document.removeEventListener('visibilitychange', this.onVisibility);
+ this.listeningVisibility = false;
+ }
+
send(data: string): boolean {
if (this.ws?.readyState !== WebSocket.OPEN) return false;
this.ws.send(data);
@@ -150,6 +199,7 @@ export class ReconnectingWebSocket<T = unknown> {
this.closed = true;
this.attempt = 0;
this.generation++;
+ this.removeVisibilityListener();
if (this.reconnectTimer) {
clearTimeout(this.reconnectTimer);
@@ -166,6 +216,7 @@ export class ReconnectingWebSocket<T = unknown> {
this.closed = true;
this.attempt = 0;
this.generation++;
+ this.removeVisibilityListener();
if (this.reconnectTimer) {
clearTimeout(this.reconnectTimer);
@@ -227,7 +278,8 @@ export function createStatsWebSocket(opts: {
onOpen: opts.onOpen,
onClose: opts.onClose,
onError: opts.onError,
- maxBackoff: opts.maxBackoff
+ maxBackoff: opts.maxBackoff,
+ pauseWhenHidden: true
});
}
@@ -256,7 +308,8 @@ export function createContainerStatsWebSocket(opts: {
onError: opts.onError,
maxBackoff: opts.maxBackoff,
autoConnect: false,
- shouldReconnect: opts.shouldReconnect
+ shouldReconnect: opts.shouldReconnect,
+ pauseWhenHidden: true
});
}
@@ -279,7 +332,8 @@ export function createContainersStatsWebSocket(opts: {
onMessage: opts.onMessage,
onOpen: opts.onOpen,
onError: opts.onError,
- shouldReconnect: opts.shouldReconnect
+ shouldReconnect: opts.shouldReconnect,
+ pauseWhenHidden: true
});
}