nsqio / go-diskqueue

A Go package providing a filesystem-backed FIFO queue
MIT License
471 stars 101 forks source link

confused about the depth field assignment way when retrieveMetaData() #3

Closed carbin-gun closed 7 years ago

carbin-gun commented 7 years ago
// retrieveMetaData initializes state from the filesystem
func (d *diskQueue) retrieveMetaData() error {
    var f *os.File
    var err error

    fileName := d.metaDataFileName()
    f, err = os.OpenFile(fileName, os.O_RDONLY, 0600)
    if err != nil {
        return err
    }
    defer f.Close()

    var depth int64
    _, err = fmt.Fscanf(f, "%d\n%d,%d\n%d,%d\n",
        &depth,
        &d.readFileNum, &d.readPos,
        &d.writeFileNum, &d.writePos)
    if err != nil {
        return err
    }
    atomic.StoreInt64(&d.depth, depth)
    d.nextReadFileNum = d.readFileNum
    d.nextReadPos = d.readPos

    return nil
}

I noticed that the depth was assigned to a local depth variable. then use the atomic.StoreInt64 to set the local depth value to the struct field. I'm confused for this, Is there anything special for the d.depth, why not just

_, err = fmt.Fscanf(f, "%d\n%d,%d\n%d,%d\n",
        &d.depth,
        &d.readFileNum, &d.readPos,
        &d.writeFileNum, &d.writePos)
mreiferson commented 7 years ago

It's because elsewhere we read just the depth value atomically.