Skip to content

Commit

Permalink
only use filemutex for windows (#960)
Browse files Browse the repository at this point in the history
  • Loading branch information
sawka authored Oct 4, 2024
1 parent 3763f94 commit e795f75
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cmd/server/main-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func main() {
return
}
defer func() {
err = waveLock.Unlock()
err = waveLock.Close()
if err != nil {
log.Printf("error releasing wave lock: %v\n", err)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
github.com/spf13/cobra v1.8.1
github.com/wavetermdev/htmltoken v0.1.0
golang.org/x/crypto v0.28.0
golang.org/x/sys v0.26.0
golang.org/x/term v0.25.0
)

Expand All @@ -41,7 +42,6 @@ require (
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.uber.org/atomic v1.7.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.26.0 // indirect
)

replace github.com/kevinburke/ssh_config => github.com/wavetermdev/ssh_config v0.0.0-20240306041034-17e2087ebde2
Expand Down
30 changes: 30 additions & 0 deletions pkg/wavebase/wavebase-posix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2024, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0

//go:build !windows

package wavebase

import (
"log"
"os"
"path/filepath"

"golang.org/x/sys/unix"
)

func AcquireWaveLock() (FDLock, error) {
homeDir := GetWaveHomeDir()
lockFileName := filepath.Join(homeDir, WaveLockFile)
log.Printf("[base] acquiring lock on %s\n", lockFileName)
fd, err := os.OpenFile(lockFileName, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
return nil, err
}
err = unix.Flock(int(fd.Fd()), unix.LOCK_EX|unix.LOCK_NB)
if err != nil {
fd.Close()
return nil, err
}
return fd, nil
}
29 changes: 29 additions & 0 deletions pkg/wavebase/wavebase-win.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2024, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0

//go:build windows

package wavebase

import (
"fmt"
"log"
"path/filepath"

"github.com/alexflint/go-filemutex"
)

func AcquireWaveLock() (FDLock, error) {
homeDir := GetWaveHomeDir()
lockFileName := filepath.Join(homeDir, WaveLockFile)
log.Printf("[base] acquiring lock on %s\n", lockFileName)
m, err := filemutex.New(lockFileName)
if err != nil {
return nil, fmt.Errorf("filemutex new error: %w", err)
}
err = m.TryLock()
if err != nil {
return nil, fmt.Errorf("filemutex trylock error: %w", err)
}
return m, nil
}
19 changes: 4 additions & 15 deletions pkg/wavebase/wavebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import (
"strings"
"sync"
"time"

"github.com/alexflint/go-filemutex"
)

// set by main-server.go
Expand All @@ -41,6 +39,10 @@ const AppPathBinDir = "bin"
var baseLock = &sync.Mutex{}
var ensureDirCache = map[string]bool{}

type FDLock interface {
Close() error
}

func IsDevMode() bool {
pdev := os.Getenv(WaveDevVarName)
return pdev != ""
Expand Down Expand Up @@ -200,19 +202,6 @@ func DetermineLocale() string {
return strings.Replace(truncated, "_", "-", -1)
}

func AcquireWaveLock() (*filemutex.FileMutex, error) {
homeDir := GetWaveHomeDir()
lockFileName := filepath.Join(homeDir, WaveLockFile)
log.Printf("[base] acquiring lock on %s\n", lockFileName)
m, err := filemutex.New(lockFileName)
if err != nil {
return nil, err
}

err = m.TryLock()
return m, err
}

func ClientArch() string {
return fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)
}
Expand Down

0 comments on commit e795f75

Please sign in to comment.