nsqio / go-diskqueue

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

fix blocking Depth() call on the closed queue #12

Closed mgurevin closed 4 years ago

ploxiln commented 4 years ago

Looks like a fixup for #11, thanks.

ploxiln commented 4 years ago

In what program did you observe this problem, btw? Was it nsqd or something else?

mgurevin commented 4 years ago

I'm using this package in my program to have disk backed channels.

I think the depth method would be better if it returns the real value from the metadata file when the queue is closed. Maybe the last value of the depth before the queue closing can also be used.

ploxiln commented 4 years ago

Yeah ... maybe something like this, in addition to your fix, would be sufficient:

--- a/diskqueue.go
+++ b/diskqueue.go
@@ -138,7 +138,12 @@ func New(name string, dataPath string, maxBytesPerFile int64,

 // Depth returns the depth of the queue
 func (d *diskQueue) Depth() int64 {
-       return <-d.depthChan
+       depth, ok := <-d.depthChan
+       if !ok {
+               // ioLoop exited
+               depth = d.depth
+       }
+       return depth
 }
mgurevin commented 4 years ago

That was my first attempt too. I prefer this if it's also ok for you.

ploxiln commented 4 years ago

Yeah that seems fine to me. You want to add that bit to this PR?