Golang -每个值的锁

mzaanser  于 2023-03-21  发布在  Go
关注(0)|答案(2)|浏览(140)

我正在编写一个golang API,它接受一个tableName值和一个updEpoch值,即:

curl -F "tableName=abc" -F "updEpoch=123" myhost:8080/singleroute
curl -F "tableName=abc" -F "updEpoch=456" myhost:8080/singleroute
curl -F "tableName=def" -F "updEpoch=123" myhost:8080/singleroute
curl -F "tableName=def" -F "updEpoch=345" myhost:8080/singleroute

我希望允许并行处理多个不同的tableName请求,但每个tableName只能同时处理1个请求。因此,在上面的示例中,如果同时触发以上4个请求,则第1个和第3个应该能够同时运行(作为唯一的表名),但是2ND只会在1 ND完成后启动,4 ND只会在3 ND完成后启动。当我研究互斥锁时,似乎没有任何例子适合这种情况,我不想在代码中的任何地方硬编码abc/def.etc,因为相同的规则应该适用于任何任意的tableName。
根据克罗曼的帮助我猜

package main

import (
    "fmt"
    "sync"
    "time"
    "http"
)
km := KeyedMutex{}
type KeyedMutex struct {
    mutexes sync.Map // Zero value is empty and ready for use
}

func (m *KeyedMutex) Lock(key string) func() {
    value, _ := m.mutexes.LoadOrStore(key, &sync.Mutex{})
    mtx := value.(*sync.Mutex)
    mtx.Lock()

    return func() { mtx.Unlock() }
}

func myFunc(key string, data string) string {
  //do some stuff
  return "done for key:"+key+", data: "+data
}

func main() {
    key := //some form value sent to my api 
    data := //some form value sent to my api 
    unlock := km.Lock(key)
    defer unlock()
    retVal := myFunc(key, data)
}
gojuced7

gojuced71#

可以使用sync.Map,将表名作为键,将*sync.Mutex作为值。
例如:

package main

import (
    "fmt"
    "sync"
    "time"
)

type KeyedMutex struct {
    mutexes sync.Map // Zero value is empty and ready for use
}

func (m *KeyedMutex) Lock(key string) func() {
    value, _ := m.mutexes.LoadOrStore(key, &sync.Mutex{})
    mtx := value.(*sync.Mutex)
    mtx.Lock()

    return func() { mtx.Unlock() }
}

func main() {
    wg := sync.WaitGroup{}
    km := KeyedMutex{}

    for _, job := range []struct {
        key  string
        data string
    }{
        {key: "abc", data: "123"},
        {key: "abc", data: "456"},
        {key: "def", data: "123"},
        {key: "def", data: "456"},
    } {
        var job = job
        wg.Add(1)

        go func() {
            defer wg.Done()

            unlock := km.Lock(job.key)
            defer unlock()

            fmt.Printf("%s:%s mutex acquired\n", job.key, job.data)
            time.Sleep(time.Second * 1) // To ensure some goroutines visibly block
            fmt.Printf("%s:%s done\n", job.key, job.data)
        }()
    }

    wg.Wait()
}

示例输出:

crow@mac:$ ./mut
def:456 mutex acquired
abc:456 mutex acquired
abc:456 done
def:456 done
abc:123 mutex acquired
def:123 mutex acquired
def:123 done
abc:123 done
crow@mac:$

以显示具有不同表名的请求立即获得互斥锁,但是具有相同表名的请求被序列化。

wxclj1h5

wxclj1h52#

您可以创建一个包级别var map[string]*sync.Mutex,并锁定将通过表名获取/创建的相应互斥锁。

相关问题