core: Do not emit post-receive hook when the offset didn't change

This commit is contained in:
Marius 2019-05-15 23:57:20 +02:00
parent 966711019b
commit 6de723ec8b
5 changed files with 15 additions and 10 deletions

View File

@ -43,7 +43,7 @@ This event will be triggered after an upload has been terminated, meaning that t
### post-receive
This event will be triggered for every running upload to indicate its current progress. It will occur for each open PATCH request, every second. The offset property will be set to the number of bytes which have been transfered to the server, at the time in total. Please be aware that this number may be higher than the number of bytes which have been stored by the data store!
This event will be triggered for every running upload to indicate its current progress. It will be emitted whenever the server has received more data from the client but at most every second. The offset property will be set to the number of bytes which have been transfered to the server, at the time in total. Please be aware that this number may be higher than the number of bytes which have been stored by the data store!
## File Hooks

View File

@ -85,7 +85,7 @@ func TestHead(t *testing.T) {
SubTest(t, "DeferLengthHeader", func(t *testing.T, store *MockFullDataStore) {
store.EXPECT().GetInfo("yes").Return(FileInfo{
SizeIsDeferred: true,
Size: 0,
Size: 0,
}, nil)
handler, _ := NewHandler(Config{
@ -109,7 +109,7 @@ func TestHead(t *testing.T) {
gomock.InOrder(
store.EXPECT().GetInfo("yes").Return(FileInfo{
SizeIsDeferred: false,
Size: 10,
Size: 10,
}, nil),
)

View File

@ -458,10 +458,8 @@ func TestPatch(t *testing.T) {
writer.Close()
info = <-c
a.Equal("yes", info.ID)
a.Equal(int64(100), info.Size)
a.Equal(int64(18), info.Offset)
// No progress event is sent after the writer is closed
// because an event for 18 bytes was already emitted.
}()
(&httpTest{

View File

@ -149,7 +149,7 @@ func TestPost(t *testing.T) {
Method: "POST",
URL: "",
ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0",
"Tus-Resumable": "1.0.0",
},
Code: http.StatusBadRequest,
}).Run(handler, t)

View File

@ -822,6 +822,7 @@ func (w *progressWriter) Write(b []byte) (int, error) {
// It will stop sending these instances once the returned channel has been
// closed. The returned reader should be used to read the request body.
func (handler *UnroutedHandler) sendProgressMessages(info FileInfo, reader io.Reader) (io.Reader, chan<- struct{}) {
previousOffset := int64(0)
progress := &progressWriter{
Offset: info.Offset,
}
@ -833,11 +834,17 @@ func (handler *UnroutedHandler) sendProgressMessages(info FileInfo, reader io.Re
select {
case <-stop:
info.Offset = atomic.LoadInt64(&progress.Offset)
handler.UploadProgress <- info
if info.Offset != previousOffset {
handler.UploadProgress <- info
previousOffset = info.Offset
}
return
case <-time.After(1 * time.Second):
info.Offset = atomic.LoadInt64(&progress.Offset)
handler.UploadProgress <- info
if info.Offset != previousOffset {
handler.UploadProgress <- info
previousOffset = info.Offset
}
}
}
}()