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.

Update last connection time on tunnel connections #401

Merged
merged 2 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions coder-sdk/agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package coder

import (
"context"
"fmt"
"net/http"
)

// UpdateLastConnectionAt updates the last connection at attribute of a workspace.
func (c *DefaultClient) UpdateLastConnectionAt(ctx context.Context, workspaceID string) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That only does so on connection, this will do so continuously for SSH connections and such

reqURL := fmt.Sprintf("/api/private/envagent/%s/update-last-connection-at", workspaceID)
resp, err := c.request(ctx, http.MethodPost, reqURL, nil)
if err != nil {
return err
}

if resp.StatusCode != http.StatusOK {
return NewHTTPError(resp)
}

return nil
}
3 changes: 3 additions & 0 deletions coder-sdk/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,7 @@ type Client interface {

// DeleteSatelliteByID deletes a satellite entity from the Coder control plane.
DeleteSatelliteByID(ctx context.Context, id string) error

// UpdateLastConnectionAt updates the last connection at attribute of a workspace.
UpdateLastConnectionAt(ctx context.Context, workspaceID string) error
}
22 changes: 22 additions & 0 deletions internal/cmd/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/url"
"os"
"strconv"
"time"

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

sdk, err := newClient(ctx, false)
if err != nil {
return xerrors.Errorf("getting coder client: %w", err)
}

// regularly update the last connection at
go func() {
ticker := time.NewTicker(time.Minute)
defer ticker.Stop()

for {
select {
case <-ctx.Done():
return
case <-ticker.C:
// silently ignore failures so we don't spam the console
_ = sdk.UpdateLastConnectionAt(ctx, c.workspaceID)
}
}
}()

// proxy via stdio
if c.stdio {
go func() {
Expand Down