• Kirill Smelkov's avatar
    xnet/virtnet: SubNetwork += AutoClose · 3070cec4
    Kirill Smelkov authored
    When a virtnet network - e.g. lonet - is explicitly used in tests, the
    open and close sequence is easy to implement:
    
        network = lonet.Join(...)
        defer network.Close()
    
        hostα = network.NewHost("α")
        defer hostα.Close()
        hostβ = network.NewHost("β")
        defer hostβ.Close()
        ...
    
    However in other scenarios - for example when the code wants to call
    xnet.Networker factory that creates a networker out of several potential
    choices according to given parameters, only interface for network
    access-point (e.g. Host in lonet/virtnet case) is returned. The network
    itself (virtnet.SubNetwork) is not returned to the user. As it is this
    makes it impossible to close the network and release its resources.
    
    Instead of adding complexity to such "create-networker" functions to
    e.g. change Close of returned networker to also close subnetwork, or
    return both networker and a separate close func, let's teach virtnet to
    close SubNetwork automatically when last host of this subnetwork is
    closed. Make it opt-in since this behaviour is not universally wanted.
    
    This way a factory that is asked to connect to network as e.g. a node on
    lonet, could do the following:
    
        func neonet.Join(...) xnet.Networker {
            ...
            // join lonet "<net>" as host "<node>"
            network = lonet.Join("<net>")
            host = network.NewHost("<node>")
            network.AutoClose() // host.Close will close network
            return host
        }
    
    and network will be closed on host.Close() call.
    
    -> Add SubNetwork.AutoClose to schedule Close to be called after last host on the subnetwork is closed.
    -> Mirror this change in virtnet part of lonet.py
    3070cec4
virtnet.go 23.3 KB