-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples_test.go
153 lines (128 loc) · 3.3 KB
/
examples_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2023-2024 Oliver Eikemeier. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
package microbatch_test
import (
"context"
"fmt"
"sync"
"time"
"fillmore-labs.com/microbatch"
)
type (
JobID int
Job struct {
ID JobID
}
JobResult struct {
ID JobID
Body string
}
Jobs []*Job
JobResults []*JobResult
)
func (j *Job) JobID() JobID { return j.ID }
func (j *JobResult) JobID() JobID { return j.ID }
// unwrap unwraps a JobResult to payload and error.
func unwrap(r *JobResult, err error) (string, error) {
if err != nil {
return "", err
}
return r.Body, nil
}
type RemoteProcessor struct{}
func (p *RemoteProcessor) ProcessJobs(jobs Jobs) (JobResults, error) {
results := make(JobResults, 0, len(jobs))
for _, job := range jobs {
result := &JobResult{
ID: job.ID,
Body: fmt.Sprintf("Processed job %d", job.ID),
}
results = append(results, result)
}
return results, nil
}
// Example (Blocking) demonstrates how to use [Batcher.SubmitJob] in a single line.
func Example_blocking() {
// Initialize
processor := &RemoteProcessor{}
batcher := microbatch.NewBatcher(
processor.ProcessJobs,
(*Job).JobID,
(*JobResult).JobID,
microbatch.WithSize(3),
microbatch.WithTimeout(10*time.Millisecond),
)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
const iterations = 5
var wg sync.WaitGroup
// Submit jobs
for i := 1; i <= iterations; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
if result, err := unwrap(batcher.Execute(ctx, &Job{ID: JobID(i)})); err == nil {
fmt.Println(result)
}
}(i) // https://go.dev/doc/faq#closures_and_goroutines
}
// Shut down
wg.Wait()
// Unordered output:
// Processed job 1
// Processed job 2
// Processed job 3
// Processed job 4
// Processed job 5
}
// Example (Asynchronous) demonstrates how to use [Batcher.SubmitJob] with a timeout.
// Note that you can shut down the batcher without waiting for the jobs to finish.
func Example_asynchronous() {
// Initialize
processor := &RemoteProcessor{}
batcher := microbatch.NewBatcher(
processor.ProcessJobs,
(*Job).JobID,
(*JobResult).JobID,
microbatch.WithSize(3),
)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
const iterations = 5
var wg sync.WaitGroup
for i := 1; i <= iterations; i++ {
future := batcher.Submit(&Job{ID: JobID(i)})
wg.Add(1)
go func(i int) {
defer wg.Done()
result, err := unwrap(future.Await(ctx))
if err == nil {
fmt.Println(result)
} else {
fmt.Printf("Error executing job %d: %v\n", i, err)
}
}(i)
}
// Shut down
batcher.Send()
wg.Wait()
// Unordered output:
// Processed job 1
// Processed job 2
// Processed job 3
// Processed job 4
// Processed job 5
}