Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added context to fetch method #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewUserLoader() *UserLoader {
return &UserLoader{
wait: 2 * time.Millisecond,
maxBatch: 100,
fetch: func(keys []string) ([]*User, []error) {
fetch: func(ctx context.Context, keys []string) ([]*User, []error) {
users := make([]*User, len(keys))
errors := make([]error, len(keys))

Expand All @@ -41,7 +41,7 @@ Then wherever you want to call the dataloader
```go
loader := NewUserLoader()

user, err := loader.Load("123")
user, err := loader.Load(context.Background(), "123")
```

This method will block for a short amount of time, waiting for any other similar requests to come in, call your fetch
Expand Down Expand Up @@ -87,7 +87,7 @@ func NewLoader(ctx context.Context) *UserLoader {
return &UserLoader{
wait: 2 * time.Millisecond,
maxBatch: 100,
fetch: func(keys []string) ([]*User, []error) {
fetch: func(ctx context.Context, keys []string) ([]*User, []error) {
// you now have a ctx to work with
},
}
Expand Down
9 changes: 5 additions & 4 deletions example/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package example

import (
"context"
"fmt"
"math/rand"
"strconv"
Expand All @@ -13,7 +14,7 @@ func BenchmarkLoader(b *testing.B) {
dl := &UserLoader{
wait: 500 * time.Nanosecond,
maxBatch: 100,
fetch: func(keys []string) ([]*User, []error) {
fetch: func(fCtx context.Context, keys []string) ([]*User, []error) {
users := make([]*User, len(keys))
errors := make([]error, len(keys))

Expand All @@ -33,7 +34,7 @@ func BenchmarkLoader(b *testing.B) {
b.Run("caches", func(b *testing.B) {
thunks := make([]func() (*User, error), b.N)
for i := 0; i < b.N; i++ {
thunks[i] = dl.LoadThunk(strconv.Itoa(rand.Int() % 300))
thunks[i] = dl.LoadThunk(context.Background(), strconv.Itoa(rand.Int()%300))
}

for i := 0; i < b.N; i++ {
Expand All @@ -44,7 +45,7 @@ func BenchmarkLoader(b *testing.B) {
b.Run("random spread", func(b *testing.B) {
thunks := make([]func() (*User, error), b.N)
for i := 0; i < b.N; i++ {
thunks[i] = dl.LoadThunk(strconv.Itoa(rand.Int()))
thunks[i] = dl.LoadThunk(context.Background(), strconv.Itoa(rand.Int()))
}

for i := 0; i < b.N; i++ {
Expand All @@ -58,7 +59,7 @@ func BenchmarkLoader(b *testing.B) {
wg.Add(1)
go func() {
for j := 0; j < b.N; j++ {
dl.Load(strconv.Itoa(rand.Int()))
dl.Load(context.Background(), strconv.Itoa(rand.Int()))
}
wg.Done()
}()
Expand Down
35 changes: 18 additions & 17 deletions example/pkgname/userloader_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion example/slice/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package slice

import (
"context"
"strconv"
"time"

Expand All @@ -13,7 +14,7 @@ func NewLoader() *UserSliceLoader {
return &UserSliceLoader{
wait: 2 * time.Millisecond,
maxBatch: 100,
fetch: func(keys []int) ([][]example.User, []error) {
fetch: func(ctx context.Context, keys []int) ([][]example.User, []error) {
users := make([][]example.User, len(keys))
errors := make([]error, len(keys))

Expand Down
35 changes: 18 additions & 17 deletions example/slice/usersliceloader_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading