Also replace question options on update so placement grading stays consistent with admin edits, and tune the Postgres pool defaults. Co-authored-by: Cursor <cursoragent@cursor.com>
90 lines
2.0 KiB
Go
90 lines
2.0 KiB
Go
package lmsprogress
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
)
|
|
|
|
type progressMemoKey struct {
|
|
kind string
|
|
userID int64
|
|
id int64
|
|
}
|
|
|
|
type progressMemoVal struct {
|
|
fraction float64
|
|
done bool
|
|
completed int32
|
|
total int32
|
|
err error
|
|
}
|
|
|
|
type accessMemoVal struct {
|
|
ok bool
|
|
reason string
|
|
err error
|
|
}
|
|
|
|
// requestMemo caches LMS/exam-prep access and progress calculations within a single request.
|
|
type requestMemo struct {
|
|
mu sync.Mutex
|
|
access map[progressMemoKey]accessMemoVal
|
|
progress map[progressMemoKey]progressMemoVal
|
|
}
|
|
|
|
type requestMemoCtxKey struct{}
|
|
|
|
// WithRequestMemo attaches a per-request memoization cache used by access/progress helpers.
|
|
func WithRequestMemo(ctx context.Context) context.Context {
|
|
if memoFrom(ctx) != nil {
|
|
return ctx
|
|
}
|
|
return context.WithValue(ctx, requestMemoCtxKey{}, &requestMemo{
|
|
access: make(map[progressMemoKey]accessMemoVal),
|
|
progress: make(map[progressMemoKey]progressMemoVal),
|
|
})
|
|
}
|
|
|
|
func memoFrom(ctx context.Context) *requestMemo {
|
|
v, _ := ctx.Value(requestMemoCtxKey{}).(*requestMemo)
|
|
return v
|
|
}
|
|
|
|
func (m *requestMemo) getAccess(kind string, userID, id int64) (accessMemoVal, bool) {
|
|
if m == nil {
|
|
return accessMemoVal{}, false
|
|
}
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
v, ok := m.access[progressMemoKey{kind: kind, userID: userID, id: id}]
|
|
return v, ok
|
|
}
|
|
|
|
func (m *requestMemo) setAccess(kind string, userID, id int64, v accessMemoVal) {
|
|
if m == nil {
|
|
return
|
|
}
|
|
m.mu.Lock()
|
|
m.access[progressMemoKey{kind: kind, userID: userID, id: id}] = v
|
|
m.mu.Unlock()
|
|
}
|
|
|
|
func (m *requestMemo) getProgress(kind string, userID, id int64) (progressMemoVal, bool) {
|
|
if m == nil {
|
|
return progressMemoVal{}, false
|
|
}
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
v, ok := m.progress[progressMemoKey{kind: kind, userID: userID, id: id}]
|
|
return v, ok
|
|
}
|
|
|
|
func (m *requestMemo) setProgress(kind string, userID, id int64, v progressMemoVal) {
|
|
if m == nil {
|
|
return
|
|
}
|
|
m.mu.Lock()
|
|
m.progress[progressMemoKey{kind: kind, userID: userID, id: id}] = v
|
|
m.mu.Unlock()
|
|
}
|