[Libguestfs] [PATCH nbdkit 3/4] New filter: exitlast filter to exit on last client connection.

Eric Blake eblake at redhat.com
Fri Mar 6 22:42:06 UTC 2020


On 3/4/20 9:17 AM, Richard W.M. Jones wrote:
> ---
>   docs/nbdkit-service.pod                     |  1 +
>   filters/exitlast/nbdkit-exitlast-filter.pod | 58 +++++++++++++++
>   filters/ip/nbdkit-ip-filter.pod             |  1 +
>   filters/rate/nbdkit-rate-filter.pod         |  1 +
>   configure.ac                                |  3 +
>   filters/exitlast/Makefile.am                | 60 ++++++++++++++++
>   tests/Makefile.am                           |  3 +
>   filters/exitlast/exitlast.c                 | 80 +++++++++++++++++++++
>   tests/test-exitlast.sh                      | 66 +++++++++++++++++
>   TODO                                        |  6 +-
>   10 files changed, 276 insertions(+), 3 deletions(-)

I'd expect docs/nbdkit-captive.pod to also gain a link to the new filter...

> +++ b/filters/exitlast/nbdkit-exitlast-filter.pod
> @@ -0,0 +1,58 @@
> +=head1 NAME
> +
> +nbdkit-exitlast-filter - exit on last client connection
> +
> +=head1 SYNOPSIS
> +
> + nbdkit --filter=exitlast PLUGIN
> +
> +=head1 DESCRIPTION
> +
> +C<nbdkit-exitlast-filter> is an nbdkit filter that causes nbdkit to
> +exit when there are no more client connections.  Note that it doesn't
> +exit before the first client connection.
> +
> +One use for this is in combination with a superserver, to save
> +resources when nbdkit is not in use (see L<nbdkit-service(1)>).
> +Another use is to ensure nbdkit exits after the client has finished
> +(but see also nbdkit-captive(1) for other ways to do this).

...as a counterpart to this one.

> +++ b/filters/exitlast/exitlast.c

> +
> +/* Counts client connections.  When this drops to zero we exit. */
> +static _Atomic unsigned connections;
> +
> +static void *
> +exitlast_open (nbdkit_next_open *next, nbdkit_backend *nxdata,
> +               int readonly)
> +{
> +  if (next (nxdata, readonly) == -1)
> +    return NULL;
> +
> +  connections++;
> +
> +  return NBDKIT_HANDLE_NOT_NEEDED;
> +}
> +
> +static void
> +exitlast_close (void *handle)
> +{
> +  if (--connections == 0) {
> +    nbdkit_debug ("exitlast: exiting on last client connection");
> +    nbdkit_shutdown ();
> +  }
> +}

I suppose there's a minor race where we can close one connection just as 
another tries to connect, where we may actually start servicing the new 
connection, but the request for shutdown will eventually end that new 
connection fairly rapidly.  Still, this is a nice way to implement 
one-shot servers.

> +++ b/TODO
> @@ -10,9 +10,6 @@ General ideas for improvements
>     sizes and threads, as that should make it easier to identify
>     systematic issues.
>   
> -* Exit on last connection (the default behaviour of qemu-nbd unless
> -  you use -t).
> -

And it's pretty slick that you added it via a filter rather than a new 
command line option.

>   * Limit number of incoming connections (like qemu-nbd -e).
>   
>   * For parallel plugins, only create threads on demand from parallel
> @@ -173,6 +170,9 @@ Suggestions for filters
>     MBs of extra data)
>     https://github.com/facebook/zstd/issues/395#issuecomment-535875379
>   
> +* nbdkit-exitlast-filter could probably use a configurable timeout so
> +  that it hangs around in case another connection comes along quickly.
> +
>   nbdkit-rate-filter:
>   
>   * allow other kinds of traffic shaping such as VBR
> 

Overall, I like the patch.

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org




More information about the Libguestfs mailing list