Create a gist now

Instantly share code, notes, and snippets.

@okzk /main.go
Created Aug 19, 2016

What would you like to do?
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"runtime"
"sync"
)
type (
// Dispatcher represents a management workers.
Dispatcher struct {
queue chan interface{}
wg sync.WaitGroup
}
)
const (
maxWorkers = 3
maxQueues = 10000
)
// NewDispatcher returns a pointer of Dispatcher.
func NewDispatcher() *Dispatcher {
d := &Dispatcher{
queue: make(chan interface{}, maxQueues),
}
return d
}
// Add adds a given value to the queue of the dispatcher.
func (d *Dispatcher) Add(v interface{}) {
d.queue <- v
}
// ワーカー立ち上げるよ
func (d *Dispatcher) Start() {
d.wg.Add(maxWorkers)
for i := 0; i < maxWorkers; i++ {
go func() {
defer d.wg.Done()
for v := range d.queue {
if str, ok := v.(string); ok {
get(str)
}
}
}()
}
}
// 元コードは引数で分岐してたけど、混乱しないようにメソッド分けたよ!
func (d *Dispatcher) Stop() {
close(d.queue)
d.wg.Wait()
}
func (d *Dispatcher) StopImmediately() {
close(d.queue)
// (それが良いのかはアレだけど)溜まってるキューを全部読み捨てるよ!
for range d.queue {
}
d.wg.Wait()
}
func get(url string) {
resp, err := http.DefaultClient.Get(url)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
log.Printf("Goroutine:%d, URL:%s (%d bytes)", runtime.NumGoroutine(), url, len(body))
}
func main() {
d := NewDispatcher()
d.Start()
for i := 0; i < 100; i++ {
url := fmt.Sprintf("http://placehold.it/%dx%d", i, i)
d.Add(url)
}
d.Stop()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment