FoundationDB

Is there a way to close a client and its DB connections?


(Caleb Spare) #1

When using the client bindings for Go, I was surprised that Database has no Close method.

It looks like there’s no relevant function in the C API at all.

Is this intentional? Maybe nobody ever needed it?


(Caleb Spare) #2

Never mind; I was looking for the wrong keyword (it’s called “destroy”).

The Go client uses finalizers to call it (yuck).


(Alec Grieser) #3

Sorry about the finalizers. In the Java bindings, we recently moved all of that finalization stuff from the finalizers (to be run automatically at destroy time) to a “close” method that the user has to call. This was necessary in the Java bindings because of how the JVM handles garbage collection. (In particular, if you created a lot of objects with only a small Java-memory footprint but a large native-memory footprint, then you could arrive in situations where your client might OOM because your native memory took up all of the allocated memory but GC didn’t run because it was only using JVM memory to know if it needed to.) This is, of course, ignoring the other reason people hate finalizers, namely that it runs code in the GC threads that would otherwise be, you know, doing GC.

I don’t know a whole lot about how go does garbage collection. Based on what I could find from go’s documentation, it might be susceptible to similar problems, so it’s possible we’d want to rethink using finalizers in the future. (Conceivably, we could also expose a “close” method on databases, transactions, etc., that does the destruction eagerly rather than waiting for garbage collection, and then the finalizer would only need to verify that the object is closed or close it then if it is not.)

Also, the database destroy method doesn’t actually close any of the TCP connections to the cluster (just some local memory keeping track of the connection) in 5.2 or earlier (i.e., any version you could download from our website). I believe that this was fixed with 6.0.1 with this PR: https://github.com/apple/foundationdb/pull/587