observer.Serve makes its 1024-row onUsage queue when the gateway starts,
for every user, with or without a module that has onUsage. Its slots were
whole usage.Records (~930 bytes each), so the empty queue held ~1 MB for
the life of the process: 1.08 MB of the 18.9 MB idle heap with real
accounts at 1c3056a2e. A slot is now a pointer (8 KB for the queue); a row
is copied only when a module has onUsage, as before (Ledger returns first
otherwise).
Run on mote at this head: go test ./internal/middleware ok; gofmt clean;
GOOS=windows go vet and GOOS=linux go vet -tags nogui
./internal/middleware ok.
Broken by hand: with the old points.go, TestOnUsageQueueIsSmall fails
("the queue's slots take 950272 bytes, want at most 16 KB").
Siblings: no other buffered channel of a value type is larger than 16
slots (grep make(chan in internal).
22 lines
547 B
Go
22 lines
547 B
Go
package middleware
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
// The onUsage queue is made when the gateway starts, for every user, with
|
|
// or without a module that has onUsage: its slots stay small (a 1024-row
|
|
// queue of whole Records held ~1 MB from the start).
|
|
func TestOnUsageQueueIsSmall(t *testing.T) {
|
|
o := Points().(*observer)
|
|
stop := o.Serve()
|
|
defer stop()
|
|
o.mu.Lock()
|
|
q := o.queue
|
|
o.mu.Unlock()
|
|
if n := int(reflect.TypeOf(q).Elem().Size()) * cap(q); n > 16<<10 {
|
|
t.Fatalf("the queue's slots take %d bytes, want at most 16 KB", n)
|
|
}
|
|
}
|