Files
nfs/internal/nfs4server/grace.go
T
petrbalvin a9b8039ef7
Test / test (push) Successful in 2m4s
Release / gates (push) Successful in 2m5s
Release / build (amd64, freebsd) (push) Successful in 1m27s
Release / build (amd64, linux) (push) Successful in 1m22s
Release / build (amd64, netbsd) (push) Successful in 1m19s
Release / build (amd64, openbsd) (push) Successful in 1m20s
Release / build (arm64, darwin) (push) Successful in 1m21s
Release / build (arm64, freebsd) (push) Successful in 1m26s
Release / build (arm64, linux) (push) Successful in 1m25s
Release / build (arm64, netbsd) (push) Successful in 1m31s
Release / build (arm64, openbsd) (push) Successful in 1m27s
Release / build (loong64, linux) (push) Successful in 1m37s
Release / build (riscv64, linux) (push) Successful in 1m21s
Release / release (push) Successful in 40s
feat: full NFSv4.2 server and client in pure Go
Assisted-by: GLM 5.3 Flash
2026-09-21 18:51:17 +02:00

61 lines
1.6 KiB
Go

// Copyright (c) 2026 Petr Balvín <opensource@petrbalvin.org> (https://petrbalvin.org)
// SPDX-License-Identifier: MIT
package nfs4server
import (
"sync"
"time"
"sourcedock.dev/petrbalvin/nfs/internal/nfs4"
)
// A grace is the reclaim window of one handler: it opens when the handler
// starts and closes after the configured period. The server recovers no
// state across restarts, so a fresh window means every reclaim attempt
// inside it is answered, and every attempt after it is NFS4ERR_NO_GRACE.
type grace struct {
mu sync.Mutex
end time.Time
done map[uint64]bool
}
func newGrace(period time.Duration) *grace {
return &grace{end: time.Now().Add(period), done: make(map[uint64]bool)}
}
// active reports whether the reclaim window still runs.
func (g *grace) active(now time.Time) bool {
g.mu.Lock()
defer g.mu.Unlock()
return now.Before(g.end)
}
// reclaimOKFor reports whether the client may reclaim: inside the
// window and without an earlier RECLAIM_COMPLETE. The status names the
// refusal: NO_GRACE for a closed window, COMPLETE_ALREADY for a client
// that announced it was done.
func (g *grace) reclaimOKFor(clientid uint64, now time.Time) (bool, uint32) {
g.mu.Lock()
defer g.mu.Unlock()
if !now.Before(g.end) {
return false, nfs4.ErrNoGrace
}
if g.done[clientid] {
return false, nfs4.ErrCompleteAlready
}
return true, 0
}
// complete records the client's RECLAIM_COMPLETE and reports whether this
// was the first announcement.
func (g *grace) complete(clientid uint64, now time.Time) bool {
g.mu.Lock()
defer g.mu.Unlock()
if g.done[clientid] {
return false
}
g.done[clientid] = true
return true
}