如何在 Go 中 gc 互斥锁映射?

Tho*_*mas 5 thread-safety go

我正在围绕数据库制作缓存包装器。为了解决可能缓慢的数据库调用,我在考虑每个键的互斥锁(伪 Go 代码):

mutexes = map[string]*sync.Mutex // instance variable

mutexes[key].Lock()
defer mutexes[key].Unlock()

if value, ok := cache.find(key); ok {
   return value
}
value = databaseCall(key)
cache.save(key, value)
return value
Run Code Online (Sandbox Code Playgroud)

但是我不希望我的地图增长太多。我的缓存是一个 LRU,我想有一个固定的大小,因为这里没有提到的其他一些原因。我想做类似的事情

delete(mutexes, key)
Run Code Online (Sandbox Code Playgroud)

当钥匙上的所有锁都结束但......对我来说这看起来不是线程安全的......我应该怎么做?

注意:我在 Go 中发现了这个问题 ,我们可以使用每个键的锁来同步地图的每个键吗?但没有答案

Bra*_*ody 4

互斥体映射是实现此目的的有效方法,但是映射本身也必须同步。引用计数可用于跟踪并发使用的条目并在不再需要时将其删除。这是互斥体的工作图,包含测试和基准测试。

(更新:此包提供类似的功能:https://pkg.go.dev/golang.org/x/sync/singleflight

地图网

// Package mapofmu provides locking per-key.
// For example, you can acquire a lock for a specific user ID and all other requests for that user ID
// will block until that entry is unlocked (effectively your work load will be run serially per-user ID),
// and yet have work for separate user IDs happen concurrently.
package mapofmu

import (
    "fmt"
    "sync"
)

// M wraps a map of mutexes.  Each key locks separately.
type M struct {
    ml sync.Mutex              // lock for entry map
    ma map[interface{}]*mentry // entry map
}

type mentry struct {
    m   *M          // point back to M, so we can synchronize removing this mentry when cnt==0
    el  sync.Mutex  // entry-specific lock
    cnt int         // reference count
    key interface{} // key in ma
}

// Unlocker provides an Unlock method to release the lock.
type Unlocker interface {
    Unlock()
}

// New returns an initalized M.
func New() *M {
    return &M{ma: make(map[interface{}]*mentry)}
}

// Lock acquires a lock corresponding to this key.
// This method will never return nil and Unlock() must be called
// to release the lock when done.
func (m *M) Lock(key interface{}) Unlocker {

    // read or create entry for this key atomically
    m.ml.Lock()
    e, ok := m.ma[key]
    if !ok {
        e = &mentry{m: m, key: key}
        m.ma[key] = e
    }
    e.cnt++ // ref count
    m.ml.Unlock()

    // acquire lock, will block here until e.cnt==1
    e.el.Lock()

    return e
}

// Unlock releases the lock for this entry.
func (me *mentry) Unlock() {

    m := me.m

    // decrement and if needed remove entry atomically
    m.ml.Lock()
    e, ok := m.ma[me.key]
    if !ok { // entry must exist
        m.ml.Unlock()
        panic(fmt.Errorf("Unlock requested for key=%v but no entry found", me.key))
    }
    e.cnt--        // ref count
    if e.cnt < 1 { // if it hits zero then we own it and remove from map
        delete(m.ma, me.key)
    }
    m.ml.Unlock()

    // now that map stuff is handled, we unlock and let
    // anything else waiting on this key through
    e.el.Unlock()

}
Run Code Online (Sandbox Code Playgroud)

mapofmu_test.go:

package mapofmu

import (
    "math/rand"
    "strconv"
    "strings"
    "sync"
    "testing"
    "time"
)

func TestM(t *testing.T) {

    r := rand.New(rand.NewSource(42))

    m := New()
    _ = m

    keyCount := 20
    iCount := 10000
    out := make(chan string, iCount*2)

    // run a bunch of concurrent requests for various keys,
    // the idea is to have a lot of lock contention
    var wg sync.WaitGroup
    wg.Add(iCount)
    for i := 0; i < iCount; i++ {
        go func(rn int) {
            defer wg.Done()
            key := strconv.Itoa(rn)

            // you can prove the test works by commenting the locking out and seeing it fail
            l := m.Lock(key)
            defer l.Unlock()

            out <- key + " A"
            time.Sleep(time.Microsecond) // make 'em wait a mo'
            out <- key + " B"
        }(r.Intn(keyCount))
    }
    wg.Wait()
    close(out)

    // verify the map is empty now
    if l := len(m.ma); l != 0 {
        t.Errorf("unexpected map length at test end: %v", l)
    }

    // confirm that the output always produced the correct sequence
    outLists := make([][]string, keyCount)
    for s := range out {
        sParts := strings.Fields(s)
        kn, err := strconv.Atoi(sParts[0])
        if err != nil {
            t.Fatal(err)
        }
        outLists[kn] = append(outLists[kn], sParts[1])
    }
    for kn := 0; kn < keyCount; kn++ {
        l := outLists[kn] // list of output for this particular key
        for i := 0; i < len(l); i += 2 {
            if l[i] != "A" || l[i+1] != "B" {
                t.Errorf("For key=%v and i=%v got unexpected values %v and %v", kn, i, l[i], l[i+1])
                break
            }
        }
    }
    if t.Failed() {
        t.Logf("Failed, outLists: %#v", outLists)
    }

}

func BenchmarkM(b *testing.B) {

    m := New()

    b.ResetTimer()
    for i := 0; i < b.N; i++ {
        // run uncontended lock/unlock - should be quite fast
        m.Lock(i).Unlock()
    }

}
Run Code Online (Sandbox Code Playgroud)