[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [RESEND PATCH 08/12] golang/xenlight: add functional options to configure Context


  • To: Nick Rosbrook <rosbrookn@xxxxxxxxx>
  • From: George Dunlap <George.Dunlap@xxxxxxxxxx>
  • Date: Fri, 18 Jun 2021 14:44:15 +0000
  • Accept-language: en-US
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=citrix.com; dmarc=pass action=none header.from=citrix.com; dkim=pass header.d=citrix.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=MEOa0gxWto6hCpCgiwuSqe8UK1DjdPyCs54qQ16sxMk=; b=FaO+jwRpXHAOY++s8fKGB88K4PGU8LyAn0lBqQF4FMqf+2Se34eeiK+dqKRBcpM1MlKIXdHC0tn117wjiYeny5k+qSlfBlvgld1se8ZuklbqEpy89Rs2LS4M4xdGoIev4twv4sxF1skmufo91MQit5PyMKopbquwEvOUvE4DXOZNNRgU39DCYHwiCQ19ZjAcpFLd/o5NmfJ7exHe66vz9m6NP9pZYBP+6sjduvHq+XslTUjRaChQ0miI+KG5YCYZMW7QfFzVWLreOob4E8eJoNaHsEbpbLqykYeuuk4I/SA5IR/Jo0pjfqAnzMyLTqHpKhz5nCW6VMqumo4EPQIaQQ==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=eNQ+kjF1n5MbIUbs1QdtGvoAZfnnSnRC3T9n1mSJkqgVKBBredNgGd/Ma182Wp2+oNsDr5LU9KSi3ClqQtOXwKzyEVwwTsuTb2F0Q7IDduXbQ3K122i7l4AV1sB3M/vlR2mEDVseeUmVRyCCGBrwm3MyRaPlaXoA93FDLp8cfETkE7/5qeFk55cw4uRU6t8w0DDx6apIApn/vDrvQ0PcSqtgSovzDoPtfrXO2+VwSkbJ98EwfRL8dCVQb/bt5L+SENvCM7pwx/kcyBaX+NhKBKAaXwRwamazP7JUO/PvBYLYbsMKArMRMi+tcXHefxbEIY86LC2Bx9YBRxMG3FtXOA==
  • Authentication-results: esa1.hc3370-68.iphmx.com; dkim=pass (signature verified) header.i=@citrix.onmicrosoft.com
  • Cc: xen-devel <xen-devel@xxxxxxxxxxxxxxxxxxxx>, Nick Rosbrook <rosbrookn@xxxxxxxxxxxx>, Ian Jackson <iwj@xxxxxxxxxxxxxx>, Wei Liu <wl@xxxxxxx>
  • Delivery-date: Fri, 18 Jun 2021 14:44:29 +0000
  • Ironport-hdrordr: A9a23:lFZnNa5yTCJY3mAp5QPXwDLXdLJyesId70hD6qkQc3FomwKj9/ xG/c5rsSMc7Qx6ZJhOo7+90cW7L080lqQFhLX5X43SPzUO0VHARO1fBOPZqAEIcBeOlNK1u5 0AT0B/YueAcGSTj6zBkXWF+wBL+qj5zEiq792usUuEVWtRGsZdB58SMHfhLqVxLjM2Y6YRJd 6nyedsgSGvQngTZtTTPAh+YwCSz+e77a4PeHQ9dmYa1DU=
  • Ironport-sdr: kwYnNaAfvjwum9uiKekYAUzvARgShfKr7QZiiRnIoiJB4A/+PIFHdMtM6JIBDDAkH+oVxW2rDr soaNAnNK/IAQtGD2uZyj2CIi82GBCzo2skFdlYHal7kMArkuz8QHifnwyBkGW0Vlwf8wV+JzKo qqsfpxbiSgzLSJ9IT1WVfFT8wcSQPy5pDkUU9heAa9raLTY2sMQX2Xtf/o5pZ/IQCqEeJx/s5T 4ZpcD/PVpVquSvnaW0L+fAgElgImAAT/Xv4AHF9fqs6EEFFLWMmq0yv7Ut6jPPe2mYQJx5ra8U azs=
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>
  • Thread-index: AQHXUNzBxrtRVA/FskCGl/j6ZWY3UKsZ/xcA
  • Thread-topic: [RESEND PATCH 08/12] golang/xenlight: add functional options to configure Context


> On May 24, 2021, at 9:36 PM, Nick Rosbrook <rosbrookn@xxxxxxxxx> wrote:
> 
> Add a ContextOption type to support functional options in NewContext.
> Then, add a variadic ContextOption parameter to NewContext, which allows
> callers to specify 0 or more configuration options.
> 
> For now, just add the WithLogLevel option so that callers can set the
> log level of the Context's xentoollog_logger. Future configuration
> options can be created by adding an appropriate field to the
> contextOptions struct and creating a With<OptionName> function to return
> a ContextOption
> 
> Signed-off-by: Nick Rosbrook <rosbrookn@xxxxxxxxxxxx>
> ---
> tools/golang/xenlight/xenlight.go | 44 +++++++++++++++++++++++++++++--
> 1 file changed, 42 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/golang/xenlight/xenlight.go 
> b/tools/golang/xenlight/xenlight.go
> index f68d7b6e97..65f93abe32 100644
> --- a/tools/golang/xenlight/xenlight.go
> +++ b/tools/golang/xenlight/xenlight.go
> @@ -136,7 +136,7 @@ func sigchldHandler(ctx *Context) {
> }
> 
> // NewContext returns a new Context.
> -func NewContext() (ctx *Context, err error) {
> +func NewContext(opts ...ContextOption) (ctx *Context, err error) {
>       ctx = &Context{}
> 
>       defer func() {
> @@ -146,8 +146,19 @@ func NewContext() (ctx *Context, err error) {
>               }
>       }()
> 
> +     // Set the default context options. These fields may
> +     // be modified by the provided opts.
> +     copts := &contextOptions{
> +             logLevel: LogLevelError,
> +     }
> +
> +     for _, opt := range opts {
> +             opt.apply(copts)
> +     }
> +
>       // Create a logger
> -     ctx.logger = C.xtl_createlogger_stdiostream(C.stderr, C.XTL_ERROR, 0)
> +     ctx.logger = C.xtl_createlogger_stdiostream(C.stderr,
> +             C.xentoollog_level(copts.logLevel), 0)
> 
>       // Allocate a context
>       ret := C.libxl_ctx_alloc(&ctx.ctx, C.LIBXL_VERSION, 0,
> @@ -201,6 +212,35 @@ func (ctx *Context) Close() error {
>       return nil
> }
> 
> +type contextOptions struct {
> +     logLevel LogLevel
> +}
> +
> +// ContextOption is used to configure options for a Context.
> +type ContextOption interface {
> +     apply(*contextOptions)
> +}
> +
> +type funcContextOption struct {
> +     f func(*contextOptions)
> +}
> +
> +func (fco *funcContextOption) apply(c *contextOptions) {
> +     fco.f(c)
> +}

Why all this convolution with interfaces and such, rather than just defining 
ContextOption as a function pointer?  Is it just to keep contextOptions out of 
the documentation page?

 -George




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.