Skip to content

Commit

Permalink
refactor: move Go to goLoad
Browse files Browse the repository at this point in the history
  • Loading branch information
sysulq committed Jul 31, 2024
1 parent 5b75ae6 commit 26a6666
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 47 deletions.
12 changes: 0 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,6 @@ func TestExample(t *testing.T) {
// Result: Result for 1
}

// Go
ch := loader.Go(ctx, 2)
result := <-ch
data, err = result.Unwrap()
if err != nil {
t.Errorf("Unexpected error: %v", err)
} else {
fmt.Printf("Result: %s\n", data)
// Output:
// Result: Result for 2
}

// LoadMany
results := loader.LoadMany(ctx, []int{3, 4, 5})
for _, result := range results {
Expand Down
18 changes: 3 additions & 15 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,19 @@ import (
)

func BenchmarkDataLoader(b *testing.B) {
callback := func(ctx context.Context, keys []int) []Result[string] {
batchFunc := func(ctx context.Context, keys []int) []Result[string] {
results := make([]Result[string], len(keys))
for i, key := range keys {
results[i] = Result[string]{data: fmt.Sprintf("Result for %d", key)}
}
return results
}

loader := New(callback)
loader := New(batchFunc)

b.Run("direct.Batch", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = callback(context.Background(), []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
}
})

b.Run("dataloader.Go", func(b *testing.B) {
for i := 0; i < b.N; i++ {
results := make([]<-chan Result[string], 10)
for j := 0; j < 10; j++ {
results[j] = loader.Go(context.Background(), j)
}
for j := 0; j < 10; j++ {
<-results[j]
}
_ = batchFunc(context.Background(), []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
}
})

Expand Down
12 changes: 5 additions & 7 deletions dataloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ type dataLoader[K comparable, V any] struct {
// different access permissions and consider creating a new instance per
// web request.
type Interface[K comparable, V any] interface {
// Go loads a single key asynchronously
Go(context.Context, K) <-chan Result[V]
// Load loads a single key
Load(context.Context, K) Result[V]
// LoadMany loads multiple keys
Expand Down Expand Up @@ -130,8 +128,8 @@ func WithWait(wait time.Duration) Option {
}
}

// Go loads a single key asynchronously
func (d *dataLoader[K, V]) Go(ctx context.Context, key K) <-chan Result[V] {
// goLoad loads a single key asynchronously
func (d *dataLoader[K, V]) goLoad(ctx context.Context, key K) <-chan Result[V] {
ch := make(chan Result[V], 1)

// Check if the key is in the cache
Expand Down Expand Up @@ -177,14 +175,14 @@ func (d *dataLoader[K, V]) Go(ctx context.Context, key K) <-chan Result[V] {

// Load loads a single key
func (d *dataLoader[K, V]) Load(ctx context.Context, key K) Result[V] {
return <-d.Go(ctx, key)
return <-d.goLoad(ctx, key)
}

// LoadMany loads multiple keys
func (d *dataLoader[K, V]) LoadMany(ctx context.Context, keys []K) []Result[V] {
chs := make([]<-chan Result[V], len(keys))
for i, key := range keys {
chs[i] = d.Go(ctx, key)
chs[i] = d.goLoad(ctx, key)
}

results := make([]Result[V], len(keys))
Expand All @@ -199,7 +197,7 @@ func (d *dataLoader[K, V]) LoadMany(ctx context.Context, keys []K) []Result[V] {
func (d *dataLoader[K, V]) LoadMap(ctx context.Context, keys []K) map[K]Result[V] {
chs := make([]<-chan Result[V], len(keys))
for i, key := range keys {
chs[i] = d.Go(ctx, key)
chs[i] = d.goLoad(ctx, key)
}

results := make(map[K]Result[V], len(keys))
Expand Down
2 changes: 1 addition & 1 deletion dataloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func testInflight(t *testing.T) {

chs := make([]<-chan Result[string], 0)
for i := 0; i < 10; i++ {
chs = append(chs, loader.Go(context.Background(), i/2))
chs = append(chs, loader.(*dataLoader[int, string]).goLoad(context.Background(), i/2))
}

for idx, ch := range chs {
Expand Down
12 changes: 0 additions & 12 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,6 @@ func TestExample(t *testing.T) {
// Result: Result for 1
}

// Go
ch := loader.Go(ctx, 2)
result := <-ch
data, err = result.Unwrap()
if err != nil {
t.Errorf("Unexpected error: %v", err)
} else {
fmt.Printf("Result: %s\n", data)
// Output:
// Result: Result for 2
}

// LoadMany
results := loader.LoadMany(ctx, []int{3, 4, 5})
for _, result := range results {
Expand Down

0 comments on commit 26a6666

Please sign in to comment.