tusd icon indicating copy to clipboard operation
tusd copied to clipboard

Passing the context to sync callbacks

Open stefan-scheidewig opened this issue 3 years ago • 9 comments

In a scenario when tusd is used as library it is most probably integrated into an application having some kind of authentication context attached to the actual tus requests. To access that and to also cancel operations when the client request is cancelled it is necessary to use the request's context in tusd and to pass the context to the callback functions to access these information in the application's domain.

stefan-scheidewig avatar Jun 03 '22 21:06 stefan-scheidewig

But isn't that behavior wanted. If the client cancels a patch request the underlying store should drop it, too. So the upload offset of an upload would actually be the old offset before the cancelled operation.

stefan-scheidewig avatar Jun 17 '22 12:06 stefan-scheidewig

But isn't that behavior wanted. If the client cancels a patch request the underlying store should drop it, too. So the upload offset of an upload would actually be the old offset before the cancelled operation.

No that is not what we want. If the upload offset after a PATCH request is the same as before the PATCH request, then no data was stored and the data transfer was useless. If you then try to resume, then you have to start at the previous offset again. A tus server should always try to store as much received data as possible, regardless of whether the PATCH request is completed properly or not. That is how tus implement resumable uploads.

Acconut avatar Jun 17 '22 12:06 Acconut

But what about the last PATCH request. If the client cancels the request, it will not get the response anymore by the application's callback. Actually the upload is completed. Can the callback be called by the client if the upload offset pointer stands at the upload length?

stefan-scheidewig avatar Jun 17 '22 12:06 stefan-scheidewig

If the client cancels the request, it will not get the response anymore by the application's callback

That is correct. That is why the responses for the PATCH requests should not include any critical information that must not be lost. Such information should be relied through another, application-specific system, such as another RESTful endpoint.

I think that is were you misunderstood the principal of using tusd. As in https://github.com/tus/tusd/pull/741, you are sending information in the last response to the client but that is not optimal for failure tolerance.

Can the callback be called by the client if the upload offset pointer stands at the upload length?

No, the callback is only invoked once, when the upload is completed. Never a second time.

Acconut avatar Jun 17 '22 13:06 Acconut

Made a change that passes at least the parent context's values but decouples the contexts again: https://github.com/tus/tusd/commit/ab78498b23645d284578c773c6f0473eed0018cd

I wanted to change the godoc of the synchronous callback methods in config.go but unsure how to phrase it correctly because it is contradictory if the callback can be used for validation but it is not guaranteed that the caller ever gets the result.

stefan-scheidewig avatar Jun 18 '22 09:06 stefan-scheidewig

it is contradictory if the callback can be used for validation but it is not guaranteed that the caller ever gets the result.

I don't understand that. Can you elaborate? Who is the caller? The tus client?

Acconut avatar Jun 19 '22 10:06 Acconut

it is contradictory if the callback can be used for validation but it is not guaranteed that the caller ever gets the result.

I don't understand that. Can you elaborate? Who is the caller? The tus client?

right.

stefan-scheidewig avatar Jun 19 '22 10:06 stefan-scheidewig

Can you explain what the problem with the documentation is then?

Acconut avatar Jun 19 '22 10:06 Acconut

Can you explain what the problem with the documentation is then?

the outcome of the validation is crucial to the uploading client. if the connection drops and the request is thus cancelled the client is forced to reupload the file and it will get the outcome after that second upload attempt because the callback is called just once.

stefan-scheidewig avatar Jun 19 '22 10:06 stefan-scheidewig

Thanks you for your contribution! In tusd v2, the request context will be accessible in the hooks and stores. Please see #986 for more details and #672 for the development of v2.

Acconut avatar Aug 23 '23 11:08 Acconut

cool. thanks.

stefan-scheidewig avatar Aug 23 '23 11:08 stefan-scheidewig