-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Serialize writes of the JSON playlist
- Loading branch information
1 parent
f383b08
commit 61d77c2
Showing
8 changed files
with
253 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package drivers | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/golang/glog" | ||
"github.com/livepeer/go-livepeer/common" | ||
) | ||
|
||
const ( | ||
timeoutMultiplier = 1.5 | ||
) | ||
|
||
type ( | ||
OvewriteQueue struct { | ||
desc string | ||
name string | ||
session OSSession | ||
maxRetries int | ||
initialTimeout time.Duration | ||
maxTimeout time.Duration | ||
queue chan []byte | ||
quit chan struct{} | ||
} | ||
) | ||
|
||
func NewOverwriteQueue(session OSSession, name, desc string, maxRetries int, initialTimeout, maxTimeout time.Duration) *OvewriteQueue { | ||
oq := &OvewriteQueue{ | ||
desc: desc, | ||
name: name, | ||
maxRetries: maxRetries, | ||
session: session, | ||
initialTimeout: initialTimeout, | ||
maxTimeout: maxTimeout, | ||
queue: make(chan []byte, 32), | ||
quit: make(chan struct{}), | ||
} | ||
if maxRetries < 1 { | ||
panic("maxRetries should be greater than zero") | ||
} | ||
go oq.workerLoop() | ||
return oq | ||
} | ||
|
||
// Save queues data to be saved | ||
func (oq *OvewriteQueue) Save(data []byte) { | ||
oq.queue <- data | ||
} | ||
|
||
// StopAfter stops reading loop after some time | ||
func (oq *OvewriteQueue) StopAfter(pause time.Duration) { | ||
go func(p time.Duration) { | ||
time.Sleep(p) | ||
close(oq.quit) | ||
}(pause) | ||
} | ||
|
||
// waitForQueueToClear used in tests | ||
func (oq *OvewriteQueue) waitForQueueToClear(timeout time.Duration) { | ||
// wait for work to be queued | ||
time.Sleep(5 * time.Millisecond) | ||
start := time.Now() | ||
for { | ||
if len(oq.queue) == 0 { | ||
return | ||
} | ||
if time.Since(start) > timeout { | ||
return | ||
} | ||
time.Sleep(20 * time.Millisecond) | ||
} | ||
} | ||
|
||
func (oq *OvewriteQueue) workerLoop() { | ||
var err error | ||
var took time.Duration | ||
for { | ||
select { | ||
case data := <-oq.queue: | ||
timeout := oq.initialTimeout | ||
for try := 0; try < oq.maxRetries; try++ { | ||
// we only care about last data | ||
data = oq.getLastMessage(data) | ||
glog.V(common.VERBOSE).Infof("Start saving %s name=%s bytes=%d try=%d", oq.desc, oq.name, len(data), try) | ||
now := time.Now() | ||
_, err = oq.session.SaveData(oq.name, data, nil, timeout) | ||
took := time.Since(now) | ||
if err == nil { | ||
glog.V(common.VERBOSE).Infof("Saving %s name=%s bytes=%d took=%s try=%d", oq.desc, oq.name, | ||
len(data), took, try) | ||
break | ||
} | ||
timeout = time.Duration(float64(timeout) * timeoutMultiplier) | ||
if timeout > oq.maxTimeout { | ||
timeout = oq.maxTimeout | ||
} | ||
} | ||
if err != nil { | ||
glog.Errorf("Error saving %s name=%s bytes=%d took=%s try=%d err=%v", oq.desc, oq.name, | ||
len(data), took, oq.maxRetries, err) | ||
} | ||
|
||
case <-oq.quit: | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (oq *OvewriteQueue) getLastMessage(current []byte) []byte { | ||
res := current | ||
for { | ||
select { | ||
case data := <-oq.queue: | ||
res = data | ||
default: | ||
return res | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package drivers | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestOverwriteQueueShouldCallSave(t *testing.T) { | ||
timeout := 500 * time.Millisecond | ||
mos := &MockOSSession{} | ||
oq := NewOverwriteQueue(mos, "f1", "save", 2, timeout, 15*time.Second) | ||
|
||
data1 := []byte("data01") | ||
|
||
var meta map[string]string | ||
mos.On("SaveData", "f1", data1, meta, timeout).Return("not used", nil).Once() | ||
|
||
oq.Save(data1) | ||
oq.waitForQueueToClear(5 * time.Second) | ||
mos.AssertExpectations(t) | ||
oq.StopAfter(0) | ||
time.Sleep(10 * time.Millisecond) | ||
} | ||
|
||
func TestOverwriteQueueShouldRetry(t *testing.T) { | ||
timeout := 500 * time.Millisecond | ||
mos := &MockOSSession{} | ||
oq := NewOverwriteQueue(mos, "f1", "retry", 2, timeout, 15*time.Second) | ||
|
||
data1 := []byte("data01") | ||
|
||
var meta map[string]string | ||
mos.On("SaveData", "f1", data1, meta, timeout).Return("not used", errors.New("no1")).Once() | ||
timeout = time.Duration(float64(timeout) * timeoutMultiplier) | ||
mos.On("SaveData", "f1", data1, meta, timeout).Return("not used", nil).Once() | ||
|
||
oq.Save(data1) | ||
oq.waitForQueueToClear(5 * time.Second) | ||
mos.AssertExpectations(t) | ||
oq.StopAfter(0) | ||
time.Sleep(10 * time.Millisecond) | ||
} | ||
|
||
func TestOverwriteQueueShouldUseLastValue(t *testing.T) { | ||
timeout := 300 * time.Millisecond | ||
mos := &MockOSSession{} | ||
mos.init() | ||
oq := NewOverwriteQueue(mos, "f1", "use last", 2, timeout, 15*time.Second) | ||
|
||
dataw1 := []byte("dataw01") | ||
data2 := []byte("data02") | ||
data3 := []byte("data03") | ||
|
||
var meta map[string]string | ||
mos.On("SaveData", "f1", dataw1, meta, timeout).Return("not used", nil).Once() | ||
mos.On("SaveData", "f1", data3, meta, timeout).Return("not used", nil).Once() | ||
|
||
mos.waitForCh = true | ||
oq.Save(dataw1) | ||
<-mos.back | ||
oq.Save(data2) | ||
oq.Save(data3) | ||
mos.waitCh <- nil | ||
|
||
oq.waitForQueueToClear(5 * time.Second) | ||
mos.AssertExpectations(t) | ||
oq.StopAfter(0) | ||
time.Sleep(10 * time.Millisecond) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.