api/imports: Make loaders available in context
This commit is contained in:
parent
2422df8a4f
commit
9f4e4b5a0b
|
@ -1112,83 +1112,87 @@ func fetchSubsByTrackerIDUnsafe(ctx context.Context) func(ids []int) ([]*model.T
|
||||||
|
|
||||||
func Middleware(next http.Handler) http.Handler {
|
func Middleware(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx := context.WithValue(r.Context(), loadersCtxKey, &Loaders{
|
ctx := Context(r.Context())
|
||||||
UsersByID: UsersByIDLoader{
|
|
||||||
maxBatch: 100,
|
|
||||||
wait: 1 * time.Millisecond,
|
|
||||||
fetch: fetchUsersByID(r.Context()),
|
|
||||||
},
|
|
||||||
UsersByName: UsersByNameLoader{
|
|
||||||
maxBatch: 100,
|
|
||||||
wait: 1 * time.Millisecond,
|
|
||||||
fetch: fetchUsersByName(r.Context()),
|
|
||||||
},
|
|
||||||
TrackersByID: TrackersByIDLoader{
|
|
||||||
maxBatch: 100,
|
|
||||||
wait: 1 * time.Millisecond,
|
|
||||||
fetch: fetchTrackersByID(r.Context()),
|
|
||||||
},
|
|
||||||
TrackersByName: TrackersByNameLoader{
|
|
||||||
maxBatch: 100,
|
|
||||||
wait: 1 * time.Millisecond,
|
|
||||||
fetch: fetchTrackersByName(r.Context()),
|
|
||||||
},
|
|
||||||
TrackersByOwnerName: TrackersByOwnerNameLoader{
|
|
||||||
maxBatch: 100,
|
|
||||||
wait: 1 * time.Millisecond,
|
|
||||||
fetch: fetchTrackersByOwnerName(r.Context()),
|
|
||||||
},
|
|
||||||
TicketsByID: TicketsByIDLoader{
|
|
||||||
maxBatch: 100,
|
|
||||||
wait: 1 * time.Millisecond,
|
|
||||||
fetch: fetchTicketsByID(r.Context()),
|
|
||||||
},
|
|
||||||
TicketsByTrackerID: TicketsByTrackerIDLoader{
|
|
||||||
maxBatch: 100,
|
|
||||||
wait: 1 * time.Millisecond,
|
|
||||||
fetch: fetchTicketsByTrackerID(r.Context()),
|
|
||||||
},
|
|
||||||
CommentsByIDUnsafe: CommentsByIDLoader{
|
|
||||||
maxBatch: 100,
|
|
||||||
wait: 1 * time.Millisecond,
|
|
||||||
fetch: fetchCommentsByIDUnsafe(r.Context()),
|
|
||||||
},
|
|
||||||
EntitiesByParticipantID: EntitiesByParticipantIDLoader{
|
|
||||||
maxBatch: 100,
|
|
||||||
wait: 1 * time.Millisecond,
|
|
||||||
fetch: fetchEntitiesByParticipantID(r.Context()),
|
|
||||||
},
|
|
||||||
LabelsByID: LabelsByIDLoader{
|
|
||||||
maxBatch: 100,
|
|
||||||
wait: 1 * time.Millisecond,
|
|
||||||
fetch: fetchLabelsByID(r.Context()),
|
|
||||||
},
|
|
||||||
ParticipantsByUserID: ParticipantsByUserIDLoader{
|
|
||||||
maxBatch: 100,
|
|
||||||
wait: 1 * time.Millisecond,
|
|
||||||
fetch: fetchParticipantsByUserID(r.Context()),
|
|
||||||
},
|
|
||||||
ParticipantsByUsername: ParticipantsByUsernameLoader{
|
|
||||||
maxBatch: 100,
|
|
||||||
wait: 1 * time.Millisecond,
|
|
||||||
fetch: fetchParticipantsByUsername(r.Context()),
|
|
||||||
},
|
|
||||||
SubsByTicketIDUnsafe: SubsByTicketIDLoader{
|
|
||||||
maxBatch: 100,
|
|
||||||
wait: 1 * time.Millisecond,
|
|
||||||
fetch: fetchSubsByTicketIDUnsafe(r.Context()),
|
|
||||||
},
|
|
||||||
SubsByTrackerIDUnsafe: SubsByTrackerIDLoader{
|
|
||||||
maxBatch: 100,
|
|
||||||
wait: 1 * time.Millisecond,
|
|
||||||
fetch: fetchSubsByTrackerIDUnsafe(r.Context()),
|
|
||||||
},
|
|
||||||
})
|
|
||||||
r = r.WithContext(ctx)
|
r = r.WithContext(ctx)
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Context(ctx context.Context) context.Context {
|
||||||
|
return context.WithValue(ctx, loadersCtxKey, &Loaders{
|
||||||
|
UsersByID: UsersByIDLoader{
|
||||||
|
maxBatch: 100,
|
||||||
|
wait: 1 * time.Millisecond,
|
||||||
|
fetch: fetchUsersByID(ctx),
|
||||||
|
},
|
||||||
|
UsersByName: UsersByNameLoader{
|
||||||
|
maxBatch: 100,
|
||||||
|
wait: 1 * time.Millisecond,
|
||||||
|
fetch: fetchUsersByName(ctx),
|
||||||
|
},
|
||||||
|
TrackersByID: TrackersByIDLoader{
|
||||||
|
maxBatch: 100,
|
||||||
|
wait: 1 * time.Millisecond,
|
||||||
|
fetch: fetchTrackersByID(ctx),
|
||||||
|
},
|
||||||
|
TrackersByName: TrackersByNameLoader{
|
||||||
|
maxBatch: 100,
|
||||||
|
wait: 1 * time.Millisecond,
|
||||||
|
fetch: fetchTrackersByName(ctx),
|
||||||
|
},
|
||||||
|
TrackersByOwnerName: TrackersByOwnerNameLoader{
|
||||||
|
maxBatch: 100,
|
||||||
|
wait: 1 * time.Millisecond,
|
||||||
|
fetch: fetchTrackersByOwnerName(ctx),
|
||||||
|
},
|
||||||
|
TicketsByID: TicketsByIDLoader{
|
||||||
|
maxBatch: 100,
|
||||||
|
wait: 1 * time.Millisecond,
|
||||||
|
fetch: fetchTicketsByID(ctx),
|
||||||
|
},
|
||||||
|
TicketsByTrackerID: TicketsByTrackerIDLoader{
|
||||||
|
maxBatch: 100,
|
||||||
|
wait: 1 * time.Millisecond,
|
||||||
|
fetch: fetchTicketsByTrackerID(ctx),
|
||||||
|
},
|
||||||
|
CommentsByIDUnsafe: CommentsByIDLoader{
|
||||||
|
maxBatch: 100,
|
||||||
|
wait: 1 * time.Millisecond,
|
||||||
|
fetch: fetchCommentsByIDUnsafe(ctx),
|
||||||
|
},
|
||||||
|
EntitiesByParticipantID: EntitiesByParticipantIDLoader{
|
||||||
|
maxBatch: 100,
|
||||||
|
wait: 1 * time.Millisecond,
|
||||||
|
fetch: fetchEntitiesByParticipantID(ctx),
|
||||||
|
},
|
||||||
|
LabelsByID: LabelsByIDLoader{
|
||||||
|
maxBatch: 100,
|
||||||
|
wait: 1 * time.Millisecond,
|
||||||
|
fetch: fetchLabelsByID(ctx),
|
||||||
|
},
|
||||||
|
ParticipantsByUserID: ParticipantsByUserIDLoader{
|
||||||
|
maxBatch: 100,
|
||||||
|
wait: 1 * time.Millisecond,
|
||||||
|
fetch: fetchParticipantsByUserID(ctx),
|
||||||
|
},
|
||||||
|
ParticipantsByUsername: ParticipantsByUsernameLoader{
|
||||||
|
maxBatch: 100,
|
||||||
|
wait: 1 * time.Millisecond,
|
||||||
|
fetch: fetchParticipantsByUsername(ctx),
|
||||||
|
},
|
||||||
|
SubsByTicketIDUnsafe: SubsByTicketIDLoader{
|
||||||
|
maxBatch: 100,
|
||||||
|
wait: 1 * time.Millisecond,
|
||||||
|
fetch: fetchSubsByTicketIDUnsafe(ctx),
|
||||||
|
},
|
||||||
|
SubsByTrackerIDUnsafe: SubsByTrackerIDLoader{
|
||||||
|
maxBatch: 100,
|
||||||
|
wait: 1 * time.Millisecond,
|
||||||
|
fetch: fetchSubsByTrackerIDUnsafe(ctx),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func ForContext(ctx context.Context) *Loaders {
|
func ForContext(ctx context.Context) *Loaders {
|
||||||
raw, ok := ctx.Value(loadersCtxKey).(*Loaders)
|
raw, ok := ctx.Value(loadersCtxKey).(*Loaders)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
|
|
||||||
"git.sr.ht/~sircmpwn/core-go/config"
|
"git.sr.ht/~sircmpwn/core-go/config"
|
||||||
work "git.sr.ht/~sircmpwn/dowork"
|
work "git.sr.ht/~sircmpwn/dowork"
|
||||||
|
"git.sr.ht/~sircmpwn/todo.sr.ht/api/loaders"
|
||||||
)
|
)
|
||||||
|
|
||||||
type contextKey struct {
|
type contextKey struct {
|
||||||
|
@ -38,6 +39,7 @@ func ImportTrackerDump(ctx context.Context, trackerID int, dump io.Reader) {
|
||||||
task := work.NewTask(func(ctx context.Context) error {
|
task := work.NewTask(func(ctx context.Context) error {
|
||||||
importCtx, cancel := context.WithTimeout(ctx, 10*time.Minute)
|
importCtx, cancel := context.WithTimeout(ctx, 10*time.Minute)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
importCtx = loaders.Context(importCtx)
|
||||||
err := importTrackerDump(importCtx, trackerID, dump, ourUpstream)
|
err := importTrackerDump(importCtx, trackerID, dump, ourUpstream)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
Loading…
Reference in New Issue