-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
storing pid and aquiring fs lock to limit concurrent gr processes (#28)
Co-authored-by: Dawid Ciepiela <[email protected]>
- Loading branch information
Showing
7 changed files
with
87 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ Cristian | |
Debugf | ||
exluded | ||
fatih | ||
fslock | ||
Fullname | ||
Henzel | ||
Hostpath | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package configfile | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
config "github.com/cli/go-gh/v2/pkg/config" | ||
fslock "github.com/juju/fslock" | ||
util "github.com/sarumaj/gh-gr/pkg/util" | ||
) | ||
|
||
const pidFile = "gr.pid" | ||
|
||
var pidFilePath = filepath.Join(config.ConfigDir(), pidFile) | ||
|
||
type ProcessLock struct{ lock *fslock.Lock } | ||
|
||
func (p ProcessLock) Lock(timeout time.Duration) { | ||
if timeout > 0 { | ||
util.FatalIfError(p.lock.LockWithTimeout(timeout)) | ||
return | ||
} | ||
|
||
util.FatalIfError(p.lock.Lock()) | ||
} | ||
|
||
func (p ProcessLock) Unlock() { | ||
util.FatalIfError(p.lock.Unlock()) | ||
_ = os.Remove(pidFilePath) | ||
} | ||
|
||
func NewProcessLock() *ProcessLock { | ||
buffer := bytes.NewBuffer(nil) | ||
util.FatalIfError(binary.Write(buffer, binary.LittleEndian, uint32(os.Getpid()))) | ||
util.FatalIfError(os.WriteFile(pidFilePath, buffer.Bytes(), os.ModePerm)) | ||
|
||
return &ProcessLock{fslock.New(pidFilePath)} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters