Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
83 lines (69 sloc) 1.79 KB
package main
import (
"context"
"fmt"
"log"
"time"
"h12.io/run"
)
type GoogleSearch struct {
Query string
Search Search
Result Result
}
type Result string
var (
Web = fakeSearch("web")
Image = fakeSearch("image")
Video = fakeSearch("video")
)
type Search func(ctx context.Context, query string) (Result, error)
func fakeSearch(kind string) Search {
return func(ctx context.Context, query string) (Result, error) {
// a real implementation will cancel and return when ctx is cancelled
return Result(fmt.Sprintf("%s result for %q", kind, query)), nil
}
}
func (s *GoogleSearch) Run(ctx context.Context) error {
result, err := s.Search(ctx, s.Query)
if err != nil {
return err
}
s.Result = result
return nil
}
func main() {
// the goroutine pool
pool := run.NewGoroutinePool(
run.Max(8), // the pool contains maximum 8 goroutines
run.IdleTime(time.Minute), // a goroutine will stay in idle for maximum 1 minute before exiting
)
// the run group
// the goroutine pool might have longer lifespan than the group
group := run.NewGroup(
context.Background(), // a context that can cancel the whole group
run.Pool(pool), // the goroutine pool used by the group
run.Recover(true), // recover from panic and returns the PanicError
run.Log(func(info *run.LogInfo) { // a log function for all starts/stops
log.Print(info)
}),
)
searches := []*GoogleSearch{
{Search: Web, Query: "golang"},
{Search: Image, Query: "golang"},
{Search: Video, Query: "golang"},
}
for _, search := range searches {
// start searching in parallel
if err := group.Go(search); err != nil {
log.Fatal(err)
}
}
// wait for all searches stop
if err := group.Wait(); err != nil {
log.Fatal(err)
}
for _, search := range searches {
fmt.Println(search.Result)
}
}