rgbkrk / libvirt-go

[DEPRECATED] Go bindings for libvirt
https://github.com/libvirt/libvirt-go
MIT License
166 stars 50 forks source link

Add RegisterCloseCallback() method #101

Closed vincentbernat closed 8 years ago

vincentbernat commented 8 years ago

Hey!

This enables to register one callback when the connection is closed unexpectedly. Only one callback can be registered this way. This is easier to handle this way.

It's quite hard to test if a connection is closed since it only works for unexpected close (virConnectClose() won't do anything as the callback will keep a reference to the connection). Therefore, we rely on keepalive feature for that.

The first commit is from #99. It is absolutely not needed except that I wouldn't know how to handle SetPtr in the second commit. SetPtr would be safe only if c.ptr is nil. If needed, the first commit can therefore be removed.

The second commit is a preparation to the last commit. It could be its own PR as it fixes a design issue introduced in #96: since there are multiple instances of VirConnection for the same connection, we shouldn't attach data to a VirConnection. Instead, we keep a map from virConnectPtr to ancillary data. We apply this to the error callback stuff.

The third commit could also be its own PR as it just moves callback handling into a separate file instead of events.go. This stuff is used in events.go and error.go and soon in libvirt.go. It seems better to not hide it in events.go.

The last commit is the real thing.

Tell me if we keep the first commit or if more PR are needed.

rgbkrk commented 8 years ago

I think you can rebase this one now that #99 has been merged.

vincentbernat commented 8 years ago

Just rebased.