Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 6eb1887

Browse files
authored
Update last connection time on tunnel connections (#401)
* Update last connection time on tunnnel connections * fmt
1 parent e641ac5 commit 6eb1887

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

coder-sdk/agent.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package coder
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
)
8+
9+
// UpdateLastConnectionAt updates the last connection at attribute of a workspace.
10+
func (c *DefaultClient) UpdateLastConnectionAt(ctx context.Context, workspaceID string) error {
11+
reqURL := fmt.Sprintf("/api/private/envagent/%s/update-last-connection-at", workspaceID)
12+
resp, err := c.request(ctx, http.MethodPost, reqURL, nil)
13+
if err != nil {
14+
return err
15+
}
16+
17+
if resp.StatusCode != http.StatusOK {
18+
return NewHTTPError(resp)
19+
}
20+
21+
return nil
22+
}

coder-sdk/interface.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,7 @@ type Client interface {
250250

251251
// DeleteSatelliteByID deletes a satellite entity from the Coder control plane.
252252
DeleteSatelliteByID(ctx context.Context, id string) error
253+
254+
// UpdateLastConnectionAt updates the last connection at attribute of a workspace.
255+
UpdateLastConnectionAt(ctx context.Context, workspaceID string) error
253256
}

internal/cmd/tunnel.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/url"
99
"os"
1010
"strconv"
11+
"time"
1112

1213
"cdr.dev/slog"
1314
"cdr.dev/slog/sloggers/sloghuman"
@@ -130,6 +131,27 @@ func (c *tunnneler) start(ctx context.Context) error {
130131
}
131132
c.log.Debug(ctx, "Connected to workspace!")
132133

134+
sdk, err := newClient(ctx, false)
135+
if err != nil {
136+
return xerrors.Errorf("getting coder client: %w", err)
137+
}
138+
139+
// regularly update the last connection at
140+
go func() {
141+
ticker := time.NewTicker(time.Minute)
142+
defer ticker.Stop()
143+
144+
for {
145+
select {
146+
case <-ctx.Done():
147+
return
148+
case <-ticker.C:
149+
// silently ignore failures so we don't spam the console
150+
_ = sdk.UpdateLastConnectionAt(ctx, c.workspaceID)
151+
}
152+
}
153+
}()
154+
133155
// proxy via stdio
134156
if c.stdio {
135157
go func() {

0 commit comments

Comments
 (0)