1
0
Fork 0

Set process priority to idle on Windows.

develop
Icedream 2024-01-22 00:46:35 +01:00
parent d704db400b
commit 537be95850
Signed by: icedream
GPG Key ID: 468BBEEBB9EC6AEA
1 changed files with 23 additions and 0 deletions

View File

@ -18,6 +18,7 @@ import (
"github.com/delthas/go-libnp" "github.com/delthas/go-libnp"
"github.com/dhowden/tag" "github.com/dhowden/tag"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"golang.org/x/sys/windows"
"gopkg.in/alecthomas/kingpin.v3-unstable" "gopkg.in/alecthomas/kingpin.v3-unstable"
"github.com/icedream/livestream-tools/icedreammusic/metacollector" "github.com/icedream/livestream-tools/icedreammusic/metacollector"
@ -61,9 +62,31 @@ func generateIDFromMetadata(metadata libnp.Info) [64]byte {
return sha512.Sum512([]byte(strings.Join(metadata.Artists, "|") + "||" + metadata.Title)) return sha512.Sum512([]byte(strings.Join(metadata.Artists, "|") + "||" + metadata.Title))
} }
// https://learn.microsoft.com/en-us/windows/win32/procthread/process-security-and-access-rights
const PROCESS_ALL_ACCESS = windows.STANDARD_RIGHTS_REQUIRED | windows.SYNCHRONIZE | 0xffff
func SetPriorityWindows(pid int, priority uint32) error {
handle, err := windows.OpenProcess(PROCESS_ALL_ACCESS, false, uint32(pid))
if err != nil {
return err
}
defer windows.CloseHandle(handle) // Technically this can fail, but we ignore it if it does
err = windows.SetPriorityClass(handle, priority)
if err != nil {
return err
}
}
func main() { func main() {
ctx := context.Background() ctx := context.Background()
// Set priority to idle to not use CPU that is required for audio playback
err := SetPriorityWindows(os.Getpid(), windows.IDLE_PRIORITY_CLASS)
if err != nil {
log.Println("WARNING: Failed to set priority class: ", err)
}
ctx, cancel := signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM) ctx, cancel := signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM)
defer cancel() defer cancel()