@@ -11,7 +11,6 @@ import (
11
11
"os"
12
12
"path"
13
13
"sync"
14
- "sync/atomic"
15
14
"time"
16
15
)
17
16
@@ -92,6 +91,7 @@ type diskQueue struct {
92
91
readChan chan []byte
93
92
94
93
// internal channels
94
+ depthChan chan int64
95
95
writeChan chan []byte
96
96
writeResponseChan chan error
97
97
emptyChan chan int
@@ -114,6 +114,7 @@ func New(name string, dataPath string, maxBytesPerFile int64,
114
114
minMsgSize : minMsgSize ,
115
115
maxMsgSize : maxMsgSize ,
116
116
readChan : make (chan []byte ),
117
+ depthChan : make (chan int64 ),
117
118
writeChan : make (chan []byte ),
118
119
writeResponseChan : make (chan error ),
119
120
emptyChan : make (chan int ),
@@ -137,7 +138,7 @@ func New(name string, dataPath string, maxBytesPerFile int64,
137
138
138
139
// Depth returns the depth of the queue
139
140
func (d * diskQueue ) Depth () int64 {
140
- return atomic . LoadInt64 ( & d . depth )
141
+ return <- d . depthChan
141
142
}
142
143
143
144
// ReadChan returns the receive-only []byte channel for reading data
@@ -256,7 +257,7 @@ func (d *diskQueue) skipToNextRWFile() error {
256
257
d .readPos = 0
257
258
d .nextReadFileNum = d .writeFileNum
258
259
d .nextReadPos = 0
259
- atomic . StoreInt64 ( & d .depth , 0 )
260
+ d .depth = 0
260
261
261
262
return err
262
263
}
@@ -385,7 +386,7 @@ func (d *diskQueue) writeOne(data []byte) error {
385
386
386
387
totalBytes := int64 (4 + dataLen )
387
388
d .writePos += totalBytes
388
- atomic . AddInt64 ( & d .depth , 1 )
389
+ d .depth += 1
389
390
390
391
if d .writePos >= d .maxBytesPerFile {
391
392
d .writeFileNum ++
@@ -446,7 +447,7 @@ func (d *diskQueue) retrieveMetaData() error {
446
447
if err != nil {
447
448
return err
448
449
}
449
- atomic . StoreInt64 ( & d .depth , depth )
450
+ d .depth = depth
450
451
d .nextReadFileNum = d .readFileNum
451
452
d .nextReadPos = d .readPos
452
453
@@ -468,7 +469,7 @@ func (d *diskQueue) persistMetaData() error {
468
469
}
469
470
470
471
_ , err = fmt .Fprintf (f , "%d\n %d,%d\n %d,%d\n " ,
471
- atomic . LoadInt64 ( & d .depth ) ,
472
+ d .depth ,
472
473
d .readFileNum , d .readPos ,
473
474
d .writeFileNum , d .writePos )
474
475
if err != nil {
@@ -508,7 +509,7 @@ func (d *diskQueue) checkTailCorruption(depth int64) {
508
509
d .name , depth )
509
510
}
510
511
// force set depth 0
511
- atomic . StoreInt64 ( & d .depth , 0 )
512
+ d .depth = 0
512
513
d .needSync = true
513
514
}
514
515
@@ -534,7 +535,7 @@ func (d *diskQueue) moveForward() {
534
535
oldReadFileNum := d .readFileNum
535
536
d .readFileNum = d .nextReadFileNum
536
537
d .readPos = d .nextReadPos
537
- depth := atomic . AddInt64 ( & d .depth , - 1 )
538
+ d .depth -= 1
538
539
539
540
// see if we need to clean up the old file
540
541
if oldReadFileNum != d .nextReadFileNum {
@@ -548,7 +549,7 @@ func (d *diskQueue) moveForward() {
548
549
}
549
550
}
550
551
551
- d .checkTailCorruption (depth )
552
+ d .checkTailCorruption (d . depth )
552
553
}
553
554
554
555
func (d * diskQueue ) handleReadError () {
@@ -639,6 +640,7 @@ func (d *diskQueue) ioLoop() {
639
640
count ++
640
641
// moveForward sets needSync flag if a file is removed
641
642
d .moveForward ()
643
+ case d .depthChan <- d .depth :
642
644
case <- d .emptyChan :
643
645
d .emptyResponseChan <- d .deleteAllFiles ()
644
646
count = 0
0 commit comments