Skip to content

Commit

Permalink
chore: add more lock
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Jul 26, 2024
1 parent 13b6cb3 commit 4aaddd9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 2 additions & 0 deletions pkg/runtime/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"runtime"
"sync"

"kcl-lang.io/kcl-go/pkg/logger"
"kcl-lang.io/kcl-go/pkg/spec/gpyrpc"
)

Expand Down Expand Up @@ -41,6 +42,7 @@ func initRuntime(maxProc int) {

rpcRuntime = NewRuntime(int(maxProc), "kclvm_cli", "server")
rpcRuntime.Start()
logger.GetLogger().Errorf("start rpc runtime...")

client := &BuiltinServiceClient{
Runtime: rpcRuntime,
Expand Down
13 changes: 12 additions & 1 deletion pkg/runtime/kclvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
package runtime

import (
"context"
_ "embed"
"errors"
"os"
"os/exec"
"path/filepath"
"runtime"
"time"

"github.com/gofrs/flock"
"kcl-lang.io/kcl-go/pkg/env"
Expand All @@ -34,21 +36,30 @@ func installKclArtifact() {
// Acquire a file lock for process synchronization
lockPath := filepath.Join(path, "init.lock")
fileLock := flock.New(lockPath)
err = fileLock.Lock()
lockCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
logger.GetLogger().Errorf("locking...")
locked, err := fileLock.TryLockContext(lockCtx, time.Second)
if err != nil {
logger.GetLogger().Warningf("attempt to acquire file lock failed: %s", err.Error())
return
}
if !locked {
logger.GetLogger().Warning("unable to acquire file lock")
return
}
defer func() {
if err := fileLock.Unlock(); err != nil {
logger.GetLogger().Errorf("failed to unlock file: %s", err.Error())
}
}()
// Install lib
logger.GetLogger().Errorf("installing...")
err = install.InstallKclvm(path)
if err != nil {
logger.GetLogger().Warningf("install kclvm failed: %s", err.Error())
}
logger.GetLogger().Errorf("unlocking...")
}

var (
Expand Down

0 comments on commit 4aaddd9

Please sign in to comment.