Skip to content

Commit e830baf

Browse files
committed
use gofrs/flock instead to handle file locking
1 parent 55a1de2 commit e830baf

File tree

3 files changed

+13
-25
lines changed

3 files changed

+13
-25
lines changed

cli/ssh.go

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cli
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
76
"io"
87
"os"
@@ -11,6 +10,7 @@ import (
1110
"time"
1211

1312
"github.com/gen2brain/beeep"
13+
"github.com/gofrs/flock"
1414
"github.com/google/uuid"
1515
"github.com/mattn/go-isatty"
1616
"github.com/spf13/cobra"
@@ -197,35 +197,20 @@ func ssh() *cobra.Command {
197197
// avoid spamming the user with notifications in case of multiple instances
198198
// of the CLI running simultaneously.
199199
func tryPollWorkspaceAutostop(ctx context.Context, client *codersdk.Client, workspace codersdk.Workspace) (stop func()) {
200-
lockPath := filepath.Join(os.TempDir(), "coder-autostop-notify-"+workspace.ID.String())
201-
lockStat, err := os.Stat(lockPath)
202-
if err == nil {
203-
// Lock file already exists for this workspace. How old is it?
204-
lockAge := time.Now().Sub(lockStat.ModTime())
205-
if lockAge < 3*autostopPollInterval {
206-
// Lock file exists and is still "fresh". Do nothing.
207-
return func() {}
208-
}
209-
}
210-
if !errors.Is(err, os.ErrNotExist) {
211-
// No permission to write to temp? Not much we can do.
212-
return func() {}
213-
}
214-
lockFile, err := os.Create(lockPath)
215-
if err != nil {
216-
// Someone got there already?
217-
return func() {}
218-
}
219-
220-
condition := notifyCondition(ctx, client, workspace.ID, lockFile)
200+
lock := flock.New(filepath.Join(os.TempDir(), "coder-autostop-notify-"+workspace.ID.String()))
201+
condition := notifyCondition(ctx, client, workspace.ID, lock)
221202
return notify.Notify(condition, autostopPollInterval, autostopNotifyCountdown...)
222203
}
223204

224205
// Notify the user if the workspace is due to shutdown.
225-
func notifyCondition(ctx context.Context, client *codersdk.Client, workspaceID uuid.UUID, lockFile *os.File) notify.Condition {
206+
func notifyCondition(ctx context.Context, client *codersdk.Client, workspaceID uuid.UUID, lock *flock.Flock) notify.Condition {
226207
return func(now time.Time) (deadline time.Time, callback func()) {
227-
// update lockFile (best effort)
228-
_ = os.Chtimes(lockFile.Name(), now, now)
208+
// Keep trying to regain the lock.
209+
locked, err := lock.TryLockContext(ctx, autostopPollInterval)
210+
if err != nil || !locked {
211+
return time.Time{}, nil
212+
}
213+
229214
ws, err := client.Workspace(ctx, workspaceID)
230215
if err != nil {
231216
return time.Time{}, nil

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ require (
6464
github.com/go-chi/httprate v0.5.3
6565
github.com/go-chi/render v1.0.1
6666
github.com/go-playground/validator/v10 v10.11.0
67+
github.com/gofrs/flock v0.8.1
6768
github.com/gohugoio/hugo v0.98.0
6869
github.com/golang-jwt/jwt v3.2.2+incompatible
6970
github.com/golang-migrate/migrate/v4 v4.15.2

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,8 @@ github.com/godbus/dbus/v5 v5.0.6/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5x
754754
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
755755
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
756756
github.com/gofiber/fiber/v2 v2.11.0/go.mod h1:oZTLWqYnqpMMuF922SjGbsYZsdpE1MCfh416HNdweIM=
757+
github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw=
758+
github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
757759
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
758760
github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
759761
github.com/gogo/googleapis v1.2.0/go.mod h1:Njal3psf3qN6dwBtQfUmBZh2ybovJ0tlu3o/AC7HYjU=

0 commit comments

Comments
 (0)