acquireRoutingFileLock (util/routing_lock_windows.go)

tokless · internal/util/routing_lock_windows.go · go · MIT
infra, tag:knn-classified

acquireRoutingFileLock obtains an exclusive file lock on Windows using LockFileEx.

// Source: https://github.com/HoangP8/tokless | License: MIT | Copyright 2026 HoangP8
// Provided via justclaw.it.com; keep this notice with the code.
//go:build windows

package util

import (
	"fmt"
	"os"
	"syscall"
	"unsafe"
)

var (
	routingKernel32     = syscall.NewLazyDLL("kernel32.dll")
	routingLockFileEx   = routingKernel32.NewProc("LockFileEx")
	routingUnlockFileEx = routingKernel32.NewProc("UnlockFileEx")
)

func acquireRoutingFileLock(path string) (func(), error) {
	file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o600)
	if err != nil {
		return nil, err
	}
	var overlapped syscall.Overlapped
	const lockfileFailImmediately = 0x1
	const lockfileExclusiveLock = 0x2
	r, _, callErr := routingLockFileEx.Call(uintptr(file.Fd()), lockfileFailImmediately|lockfileExclusiveLock, 0, 1

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key