繁体   English   中英

Race(?) with Mutex - map 中的数据损坏

[英]Race(?) with Mutex - corrupt data in map

在一个项目中,我想使用缓存来存储 hash 之类的东西。 但是,有时会发生缓存中存储的值更改为键的情况。 通常从密钥中获取大约 4 个字符:

<- Set hash::helloworldtest = abcdef0123456789
-> Get hash::helloworldtest = testef0123456789

这大致是我的缓存的结构:

type node struct {
    expires nodeExpiration
    value   interface{}
}

// ...

func (c *Cache) Set(key string, value interface{}, expiration time.Duration) {
    c.mu.Lock()
    c.val[key] = &node{
        expires: c.expiration(expiration),
        value:   value,
    }
    // fmt.Println( ... )
    c.mu.Unlock()
}

func (c *Cache) Get(key string) (interface{}, bool) {
    c.mu.Lock()
    if v, o := c.val[key]; o && v != nil {
        if !v.expires.IsExpired() {
            // fmt.Println( ... )
            c.mu.Unlock()
            return v.value, true
        }
    }
    c.mu.Unlock()
    return nil, false
}

// Cache Backend
func (b *CacheBackend) GetHash(key string) (res string, err error) {
    return b.get("hash::" + key)
}
func (b *CacheBackend) get(key string) (res string, err error) {
    if v, ok := b.cache.Get(key); ok {
        if s, ok := v.(string); ok {
            return s, nil
        }
        return "", b.errCast
    }
    return "", nil
}

// go-fiber Route
func (s *WebServer) handleGetHashAnywhere(ctx *fiber.Ctx) (err error) {
    path := ctx.Params("anywhere")
    var res string
    if res, err = s.Backend.GetHash(path); err != nil {
        return
    }
    if res == "" {
        ctx.Status(404)
    } else {
        ctx.Status(200)
    }
    return ctx.SendString(res)
}

我之前使用的是sync.RWMutex ,但将其替换为sync.Mutex ,认为这可能是问题所在。 但与sync.Mutex相同。

Go-fiber在 goroutine 中调用 Get 和 Set 方法,然后返回这些值。

有谁知道这样的事情怎么会发生?

编辑 1 :保存[]byte而不是string工作正常。

在此处输入图像描述

感谢@majodev,问题终于得到妥善解决。

该问题在零分配下的文档中进行了描述。 摘录:

因为 Fiber 针对高性能进行了优化,所以从 fiber.Ctx 返回的值默认情况下不是不可变的,并且会在 requests 中重复使用 [...] 一旦您从处理程序返回,您从上下文中获得的任何值都将在未来的请求中重新使用,并将在您的脚下发生变化。

因此必须复制上下文值,或者必须在光纤配置中传递“不可变”标志。

第一种解决方案:
读取值的新缓冲区并复制其内容

buf := bytes.NewBufferString(ctx.Params("hash"))
hash := string(buf.Bytes())

第二种解决方案:
使用此处描述的内置 function utils#CopyString(string)

hash := utils.CopyString(ctx.Params("hash"))

第三种解决方案:
不可变的配置标志

cfg := &fiber.Config{Immutable: true}

然后一切正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM