providers: a StepFun sign-in is read once a request, not once a row
/api/providers asks StepFunSignedIn for each StepFun provider on every poll, and each ask opened stepfun-auth.json under stepfunMu, most often a file that isn't there. In a profile of 200 lists against a copy of the owner's real config (2 StepFun providers) it was 0.28s of 4.07s. StepFunSignedIn now reads the file through filememo, which parses it once until it changes and remembers it missing while a request holds it. A file with a site's session from before #2384 ("ai", "com") still goes through loadStepFun under the lock, which moves it. saveStepFun calls filememo.Forget, so a sign-in saved during a request is seen by the next ask in it. Run (on mote unless named): - go test -count=1 -timeout 45m ./internal/provider ok (115s); -run TestStepFun -count=3 ok; GOOS=linux and windows go vet ./internal/provider ok; gofmt clean (here). - New TestStepFunSignedInReadOnce: 40 asks in one held request parse once, and a save inside it is seen. Broken by hand: each ask reading the file -> "parsed 0 times, want 1"; no Forget in saveStepFun -> "a sign-in saved in the request wasn't seen". - Here, 200 GET /api/providers on the sandbox with the real config copy (offline, proxy :9): StepFunSignedIn 0.28s -> gone from the profile, syscalls 1.54s -> 1.08s; total CPU 4.49s -> 4.53s, within this machine's noise. Not run: the full ./... suite; GUI tests (no page change); a real StepFun sign-in (the save path is unchanged apart from Forget).
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/filememo"
|
||||
)
|
||||
|
||||
// TestStepFunSignedInReadOnce: a provider list asked each StepFun
|
||||
// provider's sign-in, and each ask read stepfun-auth.json; a request
|
||||
// reads it once, and a sign-in saved in it is seen at once.
|
||||
func TestStepFunSignedInReadOnce(t *testing.T) {
|
||||
stepfunHome(t, stepfunOn("stepfun", "StepFun", "ai"), stepfunOn("stepfun-cn", "StepFun (China)", "com"))
|
||||
defer func(d time.Duration) { heldFor = d }(heldFor)
|
||||
heldFor = time.Hour // a loaded machine's pause isn't the bound
|
||||
if StepFunSignedIn("stepfun") {
|
||||
t.Fatal("signed in with no file")
|
||||
}
|
||||
acc, ref := stepfunTokens("4161", time.Now().Add(time.Hour), true)
|
||||
if err := saveStepFun(map[string]stepfunSession{"stepfun": {Access: acc, Refresh: ref, Account: "4161"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// written a while ago, so a request keeps what it read without a look
|
||||
ago := time.Now().Add(-time.Hour)
|
||||
if err := os.Chtimes(stepfunPath(), ago, ago); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer Hold()()
|
||||
defer filememo.Hold()() // as /api/ holds both
|
||||
parses := stepfunParses.Load()
|
||||
for range 20 {
|
||||
if !StepFunSignedIn("stepfun") || StepFunSignedIn("stepfun-cn") {
|
||||
t.Fatal("signed in: want stepfun only")
|
||||
}
|
||||
}
|
||||
if n := stepfunParses.Load() - parses; n != 1 {
|
||||
t.Fatalf("40 asks in one request parsed %d times, want 1", n)
|
||||
}
|
||||
if err := saveStepFun(map[string]stepfunSession{"stepfun-cn": {Access: acc, Refresh: ref, Account: "4161"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if StepFunSignedIn("stepfun") || !StepFunSignedIn("stepfun-cn") {
|
||||
t.Fatal("a sign-in saved in the request wasn't seen")
|
||||
}
|
||||
}
|
||||
@@ -37,8 +37,10 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/filememo"
|
||||
"github.com/yetone/magpie/internal/lastgood"
|
||||
)
|
||||
|
||||
@@ -230,6 +232,7 @@ func renameStepFun(from, to string) error {
|
||||
}
|
||||
|
||||
func saveStepFun(all map[string]stepfunSession) error {
|
||||
defer filememo.Forget() // read again, where a request holds it
|
||||
b, err := json.MarshalIndent(all, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -276,12 +279,30 @@ func (c stepfunClaims) account() string {
|
||||
}
|
||||
|
||||
// StepFunSignedIn reports whether magpie has a sign-in for the provider.
|
||||
// StepFunSignedIn reads the file once until it changes: a provider list
|
||||
// asks it for each StepFun provider on every poll, and each read was a
|
||||
// file open, most often of a file that isn't there. One with a site's
|
||||
// session from before #2384 is moved first, under the lock.
|
||||
func StepFunSignedIn(id string) bool {
|
||||
stepfunMu.Lock()
|
||||
defer stepfunMu.Unlock()
|
||||
return loadStepFun()[id].Access != ""
|
||||
all, err := filememo.Read("stepfun-auth", stepfunPath(), func(b []byte) (map[string]stepfunSession, error) {
|
||||
stepfunParses.Add(1)
|
||||
m := map[string]stepfunSession{}
|
||||
return m, json.Unmarshal(b, &m)
|
||||
})
|
||||
if err != nil {
|
||||
return false // no file, or one that doesn't parse: no session, as loadStepFun
|
||||
}
|
||||
if slices.ContainsFunc(stepfunOldSites, func(s string) bool { _, ok := all[s]; return ok }) {
|
||||
stepfunMu.Lock()
|
||||
defer stepfunMu.Unlock()
|
||||
return loadStepFun()[id].Access != ""
|
||||
}
|
||||
return all[id].Access != ""
|
||||
}
|
||||
|
||||
// stepfunParses counts StepFunSignedIn's parses, for a test.
|
||||
var stepfunParses atomic.Int64
|
||||
|
||||
// SaveStepFunSession keeps the session the sign-in page ended in, once
|
||||
// the platform has taken it: a page that isn't signed in yet has an
|
||||
// anonymous one, which is refused here. It is the provider id's alone; site
|
||||
|
||||
Reference in New Issue
Block a user