こんにちは、ナナオです。
sync.Mutex
データの複数書き込みに対応するための機能です。
Rustの場合Mutex<...>のような形で書かれますが、Goの場合は構造体の一プロパティとして定義するようです。
package main
import (
"fmt"
"sync"
"time"
)
// SafeCounter is safe to use concurrently.
type SafeCounter struct {
mu sync.Mutex
v map[string]int
}
// Inc increments the counter for the given key.
func (c *SafeCounter) Inc(key string) {
c.mu.Lock()
// Lock so only one goroutine at a time can access the map c.v.
c.v[key]++
c.mu.Unlock()
}
// Value returns the current value of the counter for the given key.
func (c *SafeCounter) Value(key string) int {
c.mu.Lock()
// Lock so only one goroutine at a time can access the map c.v.
defer c.mu.Unlock()
return c.v[key]
}
func main() {
c := SafeCounter{v: make(map[string]int)}
for i := 0; i < 1000; i++ {
go c.Inc("somekey")
}
// 一秒待つ(ゴルーチンのループを待つ)
// ちなみに実務ではこんなは書き方しない
time.Sleep(time.Second)
fmt.Println(c.Value("somekey"))
}
理解のためにこれをチャンネルで書き換えたコードが以下の通りです。
package main
import (
"fmt"
"sync"
)
// 依頼内容を定義
type request struct {
key string
response chan int // 結果を返してもらうための専用チャネル
}
func main() {
// 1. 依頼を受け付ける窓口を作る
incChan := make(chan string) // インクリメント用
getChan := make(chan request) // 値取得用
// 2. データを管理する専用ゴルーチン(マネージャー)
go func() {
// データはこのゴルーチンの中だけに閉じ込める(非公開)
stats := make(map[string]int)
for {
select {
case key := <-incChan:
// ここを通るのは常に1人だけなので、競合が起きない!
stats[key]++
case req := <-getChan:
// 値をコピーして返信用チャネルに送る
req.response <- stats[req.key]
}
}
}()
// 3. 利用側(複数のゴルーチンから一斉にアクセス)
var wg sync.WaitGroup
for i := 0; i < 1000; i++ {
wg.Add(1)
go func() {
defer wg.Done()
incChan <- "somekey"
}()
}
wg.Wait()
// 値を取得する
resCh := make(chan int)
getChan <- request{key: "somekey", response: resCh}
fmt.Println("最終結果:", <-resCh)
}
使い分けのための思考プロセスとしては以下の通りです。
- まずチャネルでシンプルに設計できないか考える(情報の流れを重視)。
- もしチャネルだとコードが複雑になりすぎたり、パフォーマンスがボトルネックになったりする場合に、Mutexを検討する。
- データが壊れないように、構造体に mu sync.Mutex を忍ばせてカプセル化する。
Exercise: Web Crawler
さて、最難関のWebクロウラー実装です。
1時間ほど格闘し、AIの力を借りて以下のように実装しました。
package main
import (
"fmt"
"sync"
)
type Fetcher interface {
Fetch(url string) (body string, urls []string, err error)
}
// 1. 重複チェック用の安全な構造体
type SafeHistory struct {
mu sync.Mutex
fetched map[string]bool
}
// URLをチェックし、未踏なら登録してtrueを返す
func (h *SafeHistory) shouldCrawl(url string) bool {
h.mu.Lock()
defer h.mu.Unlock()
if h.fetched[url] {
return false
}
h.fetched[url] = true
return true
}
// 2. Crawl 関数(引数に WaitGroup を追加)
func Crawl(url string, depth int, fetcher Fetcher, h *SafeHistory, wg *sync.WaitGroup) {
defer wg.Done() // 関数終了時にWaitGroupを減らす
if depth <= 0 {
return
}
// 重複チェック
if !h.shouldCrawl(url) {
return
}
body, urls, err := fetcher.Fetch(url)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("found: %s %q\n", url, body)
// 子要素の探索
for _, u := range urls {
wg.Add(1) // 新しいゴルーチンを起動する前にカウントを増やす
go Crawl(u, depth-1, fetcher, h, wg)
}
}
func main() {
h := &SafeHistory{fetched: make(map[string]bool)}
var wg sync.WaitGroup
wg.Add(1)
go Crawl("https://golang.org/", 10, fetcher, h, &wg)
wg.Wait() // すべてのゴルーチンが終わるまで待機
}
type fakeFetcher map[string]*fakeResult
type fakeResult struct {
body string
urls []string
}
func (f fakeFetcher) Fetch(url string) (string, []string, error) {
if res, ok := f[url]; ok {
return res.body, res.urls, nil
}
return "", nil, fmt.Errorf("not found: %s", url)
}
var fetcher = fakeFetcher{
"https://golang.org/": &fakeResult{
"The Go Programming Language",
[]string{
"https://golang.org/pkg/",
"https://golang.org/cmd/",
},
},
"https://golang.org/pkg/": &fakeResult{
"Packages",
[]string{
"https://golang.org/",
"https://golang.org/cmd/",
"https://golang.org/pkg/fmt/",
"https://golang.org/pkg/os/",
},
},
"https://golang.org/pkg/fmt/": &fakeResult{
"Package fmt",
[]string{
"https://golang.org/",
"https://golang.org/pkg/",
},
},
"https://golang.org/pkg/os/": &fakeResult{
"Package os",
[]string{
"https://golang.org/",
"https://golang.org/pkg/",
},
},
}