The end of use of a backend

Hello,

is it possible in a backend to detect the end of its use or the end of struct FS?
make a destructor in the backend to close the variables of the FS struct.

What is your use case?

You can add a finalizer to the struct - these aren't guaranteed to be run though.

https://golang.org/pkg/runtime/#SetFinalizer

I am using a variable in the backend FS structure that allocates connections to the server. My problem is to close these connections when they are no longer in use, ie at the end of using the backend.

Thanks, I will try this to see if it fixes my problem

Backends don't have a life cycle at the moment.

This is something I'd like to fix though as I've run into a very similar problem myself recently.

unfortunately finalizer is not the answer to my problem which is to be able to close the connections at the end of the backend.

Do you have another idea for this?

If you are just concerned about the binary exit then you can use the facilities in lib/atexit. These are used in other backends to make sure we cancel multipart uploads even on exit.

What sort of connections are they?

The OS will close most the connections when the binary exits.

How are you using rclone? Which command are you running?

I think to fix this properly it will be necessary to introduce a Finalize optional method for the backends, or make them take a context in New and then you can listen out for context cancellation.

no, I would like to be able to close the connections, at the end of a command which did not necessarily have an error (without exit). It's connections similar to sftp.
I use the same method as sftp to manage connections, i.e. I have an array with a connection list that I reuse along the way and if necessary new connections are created but at the end they do not are not closed.
I also noticed that in the sftp backend the connections are not closed at the end of command with sftpClient.Close (). Are they closed otherwise?

In general backends are designed to live until the end of the rclone process.

So they store open connections and re-use them - this is what the sftp backend does. When rclone exits all the open connections are closed by the OS.

If you wanted you could close all the open connections using lib/atexit when the binary ends.

There isn't a way at the moment of shutting down a backend.

Why don't you want to keep the connections open?

Thanks for your help, the connections i am using seem to be getting closed fine also with OS.

Great!

Backend lifecycles need fixing at some point...

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.