pii: a model's find, cache and download as a backend of its own

No change in behaviour: Privacy Filter's turn, waiting count, answer
cache, on-disk found file and download state move into a backend and
a dlState, so a second model can have its own of each.

Run: go test -race -count=3 ./internal/pii at the pushed head.
This commit is contained in:
yetone
2026-10-27 02:01:16 +08:00
parent 6e9820a3a4
commit 27b4f41264
5 changed files with 106 additions and 58 deletions
+16 -7
View File
@@ -32,7 +32,9 @@ func cachePath() string { return filepath.Join(dir(), "found-"+modelSHA[:12]+".b
// for a burst of requests.
var saveAfter = 2 * time.Second
var disk struct {
// diskState is a backend's cache file: read once, written saveAfter
// after an answer.
type diskState struct {
mu sync.Mutex
loaded string // the file read into cache, once
timer *time.Timer
@@ -42,11 +44,14 @@ var disk struct {
saves sync.WaitGroup
}
var disk diskState
// loadCache reads the file into cache, the first time Find is asked.
func loadCache() {
func (bk *backend) loadCache() {
disk, cache := bk.disk, bk.cache
disk.mu.Lock()
defer disk.mu.Unlock()
p := cachePath()
p := bk.path()
if disk.loaded == p {
return
}
@@ -69,7 +74,8 @@ func loadCache() {
}
// saveSoon writes the file saveAfter from now, unless a write is due.
func saveSoon() {
func (bk *backend) saveSoon() {
disk := bk.disk
disk.mu.Lock()
defer disk.mu.Unlock()
if disk.timer == nil {
@@ -79,20 +85,23 @@ func saveSoon() {
disk.mu.Lock()
disk.timer = nil
disk.mu.Unlock()
saveCache()
bk.saveCache()
})
}
}
// saveCache writes what cache holds, oldest first, to a file beside the
// old one and renames it over it: a reader never sees half of it.
func saveCache() error {
func saveCache() error { return pf.saveCache() }
func (bk *backend) saveCache() error {
disk, cache := bk.disk, bk.cache
disk.saving.Lock()
defer disk.saving.Unlock()
cache.mu.Lock()
b := encodeCache(cache)
cache.mu.Unlock()
p := cachePath()
p := bk.path()
f, err := os.CreateTemp(filepath.Dir(p), "found-*.tmp")
if err != nil {
return err
+36 -17
View File
@@ -19,8 +19,8 @@ import (
"github.com/yetone/magpie/internal/source"
)
// dl is the download under way, one at a time.
var dl struct {
// dlState is a model's download under way, one at a time.
type dlState struct {
mu sync.Mutex
cancel context.CancelFunc // nil when none is under way
err string // the last one's, "" when it worked
@@ -28,6 +28,9 @@ var dl struct {
done atomic.Int64
}
// dl is Privacy Filter's.
var dl dlState
// client has no timeout: the model takes minutes on a slow line. A
// download that stops getting bytes is ended by stall instead, and one
// whose server doesn't answer by the header timeout.
@@ -49,25 +52,31 @@ func Download(done func()) error {
if !Supported() {
return fmt.Errorf("the local masking model doesn't run on %s yet", target())
}
dl.mu.Lock()
defer dl.mu.Unlock()
if dl.cancel != nil {
return dl.run(modelSize, install, done)
}
// run runs install in the background, unless a download is under way,
// and calls done once it worked.
func (d *dlState) run(total int64, install func(context.Context) error, done func()) error {
d.mu.Lock()
defer d.mu.Unlock()
if d.cancel != nil {
return nil
}
ctx, cancel := context.WithCancel(context.Background())
dl.cancel, dl.err, dl.total = cancel, "", modelSize
dl.done.Store(0)
d.cancel, d.err, d.total = cancel, "", total
d.done.Store(0)
go func() {
err := install(ctx)
dl.mu.Lock()
dl.cancel = nil
d.mu.Lock()
d.cancel = nil
if err != nil {
dl.err = err.Error()
d.err = err.Error()
if ctx.Err() != nil {
dl.err = ""
d.err = ""
}
}
dl.mu.Unlock()
d.mu.Unlock()
cancel()
if err == nil && done != nil {
done()
@@ -77,12 +86,22 @@ func Download(done func()) error {
}
// Cancel stops the download under way, keeping what it fetched.
func Cancel() {
dl.mu.Lock()
if dl.cancel != nil {
dl.cancel()
func Cancel() { dl.stop() }
func (d *dlState) stop() {
d.mu.Lock()
if d.cancel != nil {
d.cancel()
}
dl.mu.Unlock()
d.mu.Unlock()
}
// fill puts d's state in s.
func (d *dlState) fill(s *Status) {
d.mu.Lock()
s.Downloading, s.Error = d.cancel != nil, d.err
s.Done, s.Total = d.done.Load(), d.total
d.mu.Unlock()
}
func install(ctx context.Context) error {
+52 -29
View File
@@ -8,6 +8,7 @@ import (
"sort"
"strings"
"sync"
"sync/atomic"
"unicode/utf8"
"github.com/yetone/magpie/internal/redact"
@@ -34,18 +35,40 @@ func Find(texts []string) ([][]redact.Found, error) {
// after it are left. Between chunks the turn is given up and taken again,
// so a short request waiting for it goes before the rest of a long one.
func FindContext(ctx context.Context, texts []string) ([][]redact.Found, error) {
loadCache()
out, miss := known(texts, nil)
return pf.find(ctx, texts)
}
// backend is one model's side of FindContext: its turn and the requests
// waiting for it, what it found (cache, on disk at path), and how it is
// asked (classify, with turn held) unless it failed a while ago
// (heldBack).
type backend struct {
turn chan struct{}
waiting *atomic.Int32
cache *lru
disk *diskState
path func() string
classify func([]string) ([][]redact.Found, error)
heldBack func() error
}
// pf is Privacy Filter's, run by pf-serve.
var pf = &backend{turn: turn, waiting: &waiting, cache: cache, disk: &disk, path: cachePath,
classify: classify, heldBack: heldBack}
func (b *backend) find(ctx context.Context, texts []string) ([][]redact.Found, error) {
b.loadCache()
out, miss := b.known(texts, nil)
if len(miss) == 0 {
return out, nil
}
if ctx.Err() != nil {
return nil, waitErr(ctx)
}
if err := heldBack(); err != nil {
if err := b.heldBack(); err != nil {
return nil, err
}
if !takeTurn(ctx) {
if !b.takeTurn(ctx) {
return nil, waitErr(ctx)
}
type result struct {
@@ -57,7 +80,7 @@ func FindContext(ctx context.Context, texts []string) ([][]redact.Found, error)
held := true
defer func() {
if held {
<-turn
<-b.turn
}
}()
// what this call's chunks found, by hash, whatever the cache
@@ -65,14 +88,14 @@ func FindContext(ctx context.Context, texts []string) ([][]redact.Found, error)
got := map[[32]byte][]redact.Found{}
// read again: the requests before this one in turn had most of a
// history in common with it, and asked already
out, miss := known(texts, got)
out, miss := b.known(texts, got)
for c := 0; len(miss) > 0; c++ {
if c > 0 {
// give way to a request waiting for the turn: it goes
// before the next chunk
<-turn
<-b.turn
held = false
if !takeTurn(ctx) {
if !b.takeTurn(ctx) {
done <- result{nil, waitErr(ctx)}
return
}
@@ -80,35 +103,35 @@ func FindContext(ctx context.Context, texts []string) ([][]redact.Found, error)
}
// the one ahead may have failed: those queued behind it
// don't each try again, minutes apiece
if err := heldBack(); err != nil {
if err := b.heldBack(); err != nil {
done <- result{nil, err}
return
}
chunks, parts := join(missing(texts, miss, got))
chunks, parts := join(b.missing(texts, miss, got))
if len(chunks) == 0 {
done <- result{nil, fmt.Errorf("the local masking model has no text to read for %d texts", len(miss))}
return
}
read, err := classify(chunks[:1])
read, err := b.classify(chunks[:1])
if err != nil {
done <- result{nil, err}
return
}
cache.mu.Lock()
b.cache.mu.Lock()
for _, p := range parts[0] {
k := sha256.Sum256([]byte(p.s))
got[k] = within(read[0], p.at, p.at+len(p.s))
cache.put(k, got[k])
b.cache.put(k, got[k])
}
cache.mu.Unlock()
saveSoon()
b.cache.mu.Unlock()
b.saveSoon()
// the texts still missing, with this chunk's answers and
// what others' requests found meanwhile
sub := make([]string, len(miss))
for j, i := range miss {
sub[j] = texts[i]
}
o, m := known(sub, got)
o, m := b.known(sub, got)
was := miss
miss = nil
for j, i := range was {
@@ -130,11 +153,11 @@ func FindContext(ctx context.Context, texts []string) ([][]redact.Found, error)
// takeTurn waits for the model's turn while ctx lives, counted as
// waiting; false when ctx ended first.
func takeTurn(ctx context.Context) bool {
waiting.Add(1)
defer waiting.Add(-1)
func (b *backend) takeTurn(ctx context.Context) bool {
b.waiting.Add(1)
defer b.waiting.Add(-1)
select {
case turn <- struct{}{}:
case b.turn <- struct{}{}:
return true
case <-ctx.Done():
return false
@@ -150,16 +173,16 @@ func waitErr(ctx context.Context) error {
// cache, and which texts it isn't. A text kept whole is taken as it is;
// a long one is put together from its pieces, once all of them are known,
// and kept whole then too.
func known(texts []string, got map[[32]byte][]redact.Found) (out [][]redact.Found, miss []int) {
func (b *backend) known(texts []string, got map[[32]byte][]redact.Found) (out [][]redact.Found, miss []int) {
out = make([][]redact.Found, len(texts))
cache.mu.Lock()
defer cache.mu.Unlock()
b.cache.mu.Lock()
defer b.cache.mu.Unlock()
look := func(s string) ([]redact.Found, bool) {
k := sha256.Sum256([]byte(s))
if fs, ok := got[k]; ok {
return fs, true
}
if fs, ok := cache.get(k); ok && fits(fs, s) {
if fs, ok := b.cache.get(k); ok && fits(fs, s) {
return fs, true
}
return nil, false
@@ -191,7 +214,7 @@ func known(texts []string, got map[[32]byte][]redact.Found) (out [][]redact.Foun
continue
}
out[i] = merge(all)
cache.put(sha256.Sum256([]byte(t)), out[i])
b.cache.put(sha256.Sum256([]byte(t)), out[i])
}
return out, miss
}
@@ -299,11 +322,11 @@ func merge(fs []redact.Found) []redact.Found {
// missing are what of texts[miss] neither got nor the cache has, to ask
// the model about: the texts whole, and the long ones' pieces, each once.
func missing(texts []string, miss []int, got map[[32]byte][]redact.Found) []string {
func (b *backend) missing(texts []string, miss []int, got map[[32]byte][]redact.Found) []string {
var out []string
seen := map[[32]byte]bool{}
cache.mu.Lock()
defer cache.mu.Unlock()
b.cache.mu.Lock()
defer b.cache.mu.Unlock()
for _, i := range miss {
t := texts[i]
for _, p := range pieces(t) {
@@ -316,7 +339,7 @@ func missing(texts []string, miss []int, got map[[32]byte][]redact.Found) []stri
if _, ok := got[k]; ok {
continue
}
if fs, ok := cache.get(k); ok && fits(fs, s) {
if fs, ok := b.cache.get(k); ok && fits(fs, s) {
continue
}
out = append(out, s)
+1 -4
View File
@@ -138,10 +138,7 @@ type Status struct {
// Now is the model's status.
func Now() Status {
s := Status{Supported: Supported(), Installed: Installed(), Size: modelSize}
dl.mu.Lock()
s.Downloading, s.Error = dl.cancel != nil, dl.err
s.Done, s.Total = dl.done.Load(), dl.total
dl.mu.Unlock()
dl.fill(&s)
srv.mu.Lock()
if srv.h != nil {
s.Running, s.Device, s.GPU, s.LoadMS, s.GPUError = true, srv.h.hello.Device, srv.h.hello.GPU, srv.h.hello.MS, srv.h.hello.GPUError
+1 -1
View File
@@ -55,7 +55,7 @@ func TestFindJoinsTextsAndMapsBack(t *testing.T) {
func TestJoinSplitsAtChunkSize(t *testing.T) {
long := strings.Repeat("x", chunkSize)
texts := []string{"a", "b", long, "c"}
chunks, parts := join(missing(texts, []int{0, 1, 2, 3}, nil))
chunks, parts := join(pf.missing(texts, []int{0, 1, 2, 3}, nil))
if len(chunks) != 3 || chunks[0] != "a"+sep+"b" || chunks[1] != long || chunks[2] != "c" {
t.Fatalf("chunks = %d: %.20q", len(chunks), chunks)
}