API: Rig up query { paste }

This commit is contained in:
Drew DeVault 2021-09-21 10:25:19 +02:00
parent 62ec9ea04a
commit fb6d90961a
3 changed files with 283 additions and 1 deletions

View File

@ -85,7 +85,7 @@ func (r *queryResolver) Pastes(ctx context.Context, cursor *coremodel.Cursor) (*
}
func (r *queryResolver) Paste(ctx context.Context, id string) (*model.Paste, error) {
panic(fmt.Errorf("not implemented"))
return loaders.ForContext(ctx).PastesBySHA.Load(id)
}
func (r *userResolver) Pastes(ctx context.Context, obj *model.User, cursor *coremodel.Cursor) (*model.PasteCursor, error) {

View File

@ -2,6 +2,7 @@ package loaders
//go:generate ./gen UsersByIDLoader int api/graph/model.User
//go:generate ./gen UsersByNameLoader string api/graph/model.User
//go:generate ./gen PastesBySHALoader string api/graph/model.Paste
import (
"context"
@ -13,6 +14,7 @@ import (
sq "github.com/Masterminds/squirrel"
"github.com/lib/pq"
"git.sr.ht/~sircmpwn/core-go/auth"
"git.sr.ht/~sircmpwn/core-go/database"
"git.sr.ht/~sircmpwn/paste.sr.ht/api/graph/model"
)
@ -24,10 +26,61 @@ type contextKey struct {
}
type Loaders struct {
PastesBySHA PastesBySHALoader
UsersByID UsersByIDLoader
UsersByName UsersByNameLoader
}
func fetchPastesBySHA(ctx context.Context) func(shas []string) ([]*model.Paste, []error) {
return func(shas []string) ([]*model.Paste, []error) {
pastes := make([]*model.Paste, len(shas))
if err := database.WithTx(ctx, &sql.TxOptions{
Isolation: 0,
ReadOnly: true,
}, func (tx *sql.Tx) error {
var (
err error
rows *sql.Rows
)
user := auth.ForContext(ctx)
query := database.
Select(ctx, (&model.Paste{}).As(`paste`)).
From(`paste`).
Where(sq.And{
sq.Expr(`paste.sha = ANY(?)`, pq.Array(shas)),
sq.Or{
sq.Expr(`paste.user_id = ?`, user.UserID),
sq.Expr(`paste.visibility != 'private'`),
},
})
if rows, err = query.RunWith(tx).QueryContext(ctx); err != nil {
panic(err)
}
defer rows.Close()
pastesBySHA := map[string]*model.Paste{}
for rows.Next() {
var paste model.Paste
if err := rows.Scan(database.Scan(ctx, &paste)...); err != nil {
panic(err)
}
pastesBySHA[paste.ID] = &paste
}
if err = rows.Err(); err != nil {
panic(err)
}
for i, sha := range shas {
pastes[i] = pastesBySHA[sha]
}
return nil
}); err != nil {
panic(err)
}
return pastes, nil
}
}
func fetchUsersByID(ctx context.Context) func(ids []int) ([]*model.User, []error) {
return func(ids []int) ([]*model.User, []error) {
users := make([]*model.User, len(ids))
@ -117,6 +170,11 @@ func fetchUsersByName(ctx context.Context) func(names []string) ([]*model.User,
func Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), loadersCtxKey, &Loaders{
PastesBySHA: PastesBySHALoader{
maxBatch: 100,
wait: 1 * time.Millisecond,
fetch: fetchPastesBySHA(r.Context()),
},
UsersByName: UsersByNameLoader{
maxBatch: 100,
wait: 1 * time.Millisecond,

View File

@ -0,0 +1,224 @@
// Code generated by github.com/vektah/dataloaden, DO NOT EDIT.
package loaders
import (
"sync"
"time"
"git.sr.ht/~sircmpwn/paste.sr.ht/api/graph/model"
)
// PastesBySHALoaderConfig captures the config to create a new PastesBySHALoader
type PastesBySHALoaderConfig struct {
// Fetch is a method that provides the data for the loader
Fetch func(keys []string) ([]*model.Paste, []error)
// Wait is how long wait before sending a batch
Wait time.Duration
// MaxBatch will limit the maximum number of keys to send in one batch, 0 = not limit
MaxBatch int
}
// NewPastesBySHALoader creates a new PastesBySHALoader given a fetch, wait, and maxBatch
func NewPastesBySHALoader(config PastesBySHALoaderConfig) *PastesBySHALoader {
return &PastesBySHALoader{
fetch: config.Fetch,
wait: config.Wait,
maxBatch: config.MaxBatch,
}
}
// PastesBySHALoader batches and caches requests
type PastesBySHALoader struct {
// this method provides the data for the loader
fetch func(keys []string) ([]*model.Paste, []error)
// how long to done before sending a batch
wait time.Duration
// this will limit the maximum number of keys to send in one batch, 0 = no limit
maxBatch int
// INTERNAL
// lazily created cache
cache map[string]*model.Paste
// the current batch. keys will continue to be collected until timeout is hit,
// then everything will be sent to the fetch method and out to the listeners
batch *pastesBySHALoaderBatch
// mutex to prevent races
mu sync.Mutex
}
type pastesBySHALoaderBatch struct {
keys []string
data []*model.Paste
error []error
closing bool
done chan struct{}
}
// Load a Paste by key, batching and caching will be applied automatically
func (l *PastesBySHALoader) Load(key string) (*model.Paste, error) {
return l.LoadThunk(key)()
}
// LoadThunk returns a function that when called will block waiting for a Paste.
// This method should be used if you want one goroutine to make requests to many
// different data loaders without blocking until the thunk is called.
func (l *PastesBySHALoader) LoadThunk(key string) func() (*model.Paste, error) {
l.mu.Lock()
if it, ok := l.cache[key]; ok {
l.mu.Unlock()
return func() (*model.Paste, error) {
return it, nil
}
}
if l.batch == nil {
l.batch = &pastesBySHALoaderBatch{done: make(chan struct{})}
}
batch := l.batch
pos := batch.keyIndex(l, key)
l.mu.Unlock()
return func() (*model.Paste, error) {
<-batch.done
var data *model.Paste
if pos < len(batch.data) {
data = batch.data[pos]
}
var err error
// its convenient to be able to return a single error for everything
if len(batch.error) == 1 {
err = batch.error[0]
} else if batch.error != nil {
err = batch.error[pos]
}
if err == nil {
l.mu.Lock()
l.unsafeSet(key, data)
l.mu.Unlock()
}
return data, err
}
}
// LoadAll fetches many keys at once. It will be broken into appropriate sized
// sub batches depending on how the loader is configured
func (l *PastesBySHALoader) LoadAll(keys []string) ([]*model.Paste, []error) {
results := make([]func() (*model.Paste, error), len(keys))
for i, key := range keys {
results[i] = l.LoadThunk(key)
}
pastes := make([]*model.Paste, len(keys))
errors := make([]error, len(keys))
for i, thunk := range results {
pastes[i], errors[i] = thunk()
}
return pastes, errors
}
// LoadAllThunk returns a function that when called will block waiting for a Pastes.
// This method should be used if you want one goroutine to make requests to many
// different data loaders without blocking until the thunk is called.
func (l *PastesBySHALoader) LoadAllThunk(keys []string) func() ([]*model.Paste, []error) {
results := make([]func() (*model.Paste, error), len(keys))
for i, key := range keys {
results[i] = l.LoadThunk(key)
}
return func() ([]*model.Paste, []error) {
pastes := make([]*model.Paste, len(keys))
errors := make([]error, len(keys))
for i, thunk := range results {
pastes[i], errors[i] = thunk()
}
return pastes, errors
}
}
// Prime the cache with the provided key and value. If the key already exists, no change is made
// and false is returned.
// (To forcefully prime the cache, clear the key first with loader.clear(key).prime(key, value).)
func (l *PastesBySHALoader) Prime(key string, value *model.Paste) bool {
l.mu.Lock()
var found bool
if _, found = l.cache[key]; !found {
// make a copy when writing to the cache, its easy to pass a pointer in from a loop var
// and end up with the whole cache pointing to the same value.
cpy := *value
l.unsafeSet(key, &cpy)
}
l.mu.Unlock()
return !found
}
// Clear the value at key from the cache, if it exists
func (l *PastesBySHALoader) Clear(key string) {
l.mu.Lock()
delete(l.cache, key)
l.mu.Unlock()
}
func (l *PastesBySHALoader) unsafeSet(key string, value *model.Paste) {
if l.cache == nil {
l.cache = map[string]*model.Paste{}
}
l.cache[key] = value
}
// keyIndex will return the location of the key in the batch, if its not found
// it will add the key to the batch
func (b *pastesBySHALoaderBatch) keyIndex(l *PastesBySHALoader, key string) int {
for i, existingKey := range b.keys {
if key == existingKey {
return i
}
}
pos := len(b.keys)
b.keys = append(b.keys, key)
if pos == 0 {
go b.startTimer(l)
}
if l.maxBatch != 0 && pos >= l.maxBatch-1 {
if !b.closing {
b.closing = true
l.batch = nil
go b.end(l)
}
}
return pos
}
func (b *pastesBySHALoaderBatch) startTimer(l *PastesBySHALoader) {
time.Sleep(l.wait)
l.mu.Lock()
// we must have hit a batch limit and are already finalizing this batch
if b.closing {
l.mu.Unlock()
return
}
l.batch = nil
l.mu.Unlock()
b.end(l)
}
func (b *pastesBySHALoaderBatch) end(l *PastesBySHALoader) {
b.data, b.error = l.fetch(b.keys)
close(b.done)
}