Slide 30
Slide 30 text
type request struct {
name string
done chan struct{}
}
func BatchGreeter(batchSize int) (
< chan string,
func(int),
) {
out := make(chan string)
names := make(chan string)
func BatchGreeter(batchSize int) (
< chan string,
func(int) < chan struct{},
) {
out := make(chan string)
names := make(chan request)
go func() {
batch := make([]string, 0, batchSize)
go func() {
batch := make([]string, 0, batchSize)
}()
for request := range names {
batch = append(batch, request.name)
if len(batch) == batchSize {
out < "Hi, " + strings.Join(batch, " & ") + "!"
batch = batch[:0]
}
for name := range names {
batch = append(batch, name)
if len(batch) == batchSize {
out < "Hi, " + strings.Join(batch, " & ") + "!"
batch = batch[:0]
}
}
}()
return out, func(userID int) {
go func() {
names < fetchUserName(userID)
}()
}
}
return out, func(userID int) < chan struct{} {
done := make(chan struct{})
go func() {
names < request{
name: fetchUserName(userID),
done: done,
}
}()
-
-
-
-
-
-
-
}
-
}
close(request.done)
}
return done