[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 18:12:11 +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=xOlEkVzRwxXh1MTcfFjDKTK8VFesmToA0glAgk6iTn0=; b=Iu+6q8mRM1SFADjFfIDSy6yIbPUJQeJSKeQwS2Csskjd0dyabTNq3ZXuzdsqt0svcbC1wjuUVXeeoVALdhwHfsBdlziBLoJ+TPYdEY6IuGIqdXKn3UYRlYfIdlRhk54sXEARVIvWDxKj2RfD2482ieiAPQRPTKIgASJk1pxgQgZWZ71lkyuy9lLx/xZ/ZHuX8nC7QLBeuH9fOFUQjTMnoVPmcqN1NGrN1nPgak3+T7AL4BfBFt2ibLo0af93/eA1ke4a1DNJvV8+tJZUy7S2XBnCLwMCdIMCBLE+PRcPcE8QXsQAgOoGQ/oMIw0mgOH0mnITeQ8R4A2yuvck8CVsZA==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SD8DBc4FFPzxN2crfT3/YSEzhSQKYkOYxLfE4bBbJfDxh4nT7JnVLVKZBWmy3xbcfZ8AD+CU8f7B52D+mEI6zHrZFE5A4wATagergQ1bnm5t2Nnabe1e3tH5ZMGmQ31EU97BTo/TpSyWX0HPeVMaM45f/wqdX9uHi+kSscyVXlyFjmbopJI6idI8DR5C+uV6nH+BaQArvDjwbRdPYs0UAYO1SQx4ce/h53zOyim0kLjyzj9Tx9aBU9yquuZI7RKysxXns6k3X7jimF5wZ5C++GygEf6oUUzhW46fGVgVIkTir0PewO8jL0Jza2USMkK9253rjckyyXawxaI36RQMPw==
  • 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 18:12:25 +0000
  • Ironport-hdrordr: A9a23:N0T/WKAXpsIks73lHegSsceALOsnbusQ8zAXPh9KJiC9I/b1qy nxppkmPEfP+UsssHFJo6HkBEEZKUmstKKdkrNhQYtKOzOW+FdATbsSo7cKpgePJ8SQzJ8l6U 4NSdkcNDS0NykBsS+Y2nj6Lz9D+qj+zEnAv463pB0NLT2CKZsQlDuRYjzrT3GeLzM2YabRYa DsgPav0ADQHkj/AP7LZEUtbqzmnZnmhZjmaRkJC1oM8w+Vlw6l77b8Dlyxwgoeeykn+8ZhzU H11yjCoomzufCyzRHRk0XJ6Y5NpdfnwtxfQOSRl8kuLCn2gArAXvUlZ1TChkFwnAic0idtrD D+mWZ4Ay210QKIQoiBm2qr5+An6kd015at8y7DvZKpm72HeNtzMbs+uWseSGqE16NohqAN7I tbm22erJZZFhXGgWD04MXJTQhjkg6urWMlivN7tQ0UbWOPAIUh3LD30XklWKvoJhiKo7zP0d Mee/309bJTaxeXfnrZtm5gzJilWWkyBA6PRgwHttaO2zZbkXhlxw9ArfZv0kso5dY4Ud1J9u 7EOqNnmPVHSdIXd7t0AKMETdGsAmLATBrQOCaZIEjhFqsAJ3XRwqSHrYndJNvaMaDg6aFC16 gpfGkowFLaSnief/Fmhqc7gCwlaF/NKQgF5PsulKREhg==
  • Ironport-sdr: UFPVzFUhIcoa/KSGOzMXK8efDs0nvwEmNODyCHCDlMSWMDFchXOgcL0awgTJF4k8Y/85C+oHs/ pah0GR3owMFWc5Z0xO1+kJrUdXiR4jq1jmbRYMJHd/5R9CU9HSNItz/WHiEjFdkt++KoKS/Cqe SyEHFR/LK7HLTETuhKvPoWo9fIxl9NCypp5e/xi8gXzjOWSqmLDrgOPzRi0lsbBSPjFd1V9ErU i4Nnp35za62P6hcFSSZd1ULC0+UUfqPFVJWx3vQVeicjvwrhA1MS9tUi6hlQU4eSBCHJI6SDW6 wDk=
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>
  • Thread-index: AQHXUNzBxrtRVA/FskCGl/j6ZWY3UKsZ/xcAgAAG0ICAABOXAIAAC7AAgAAUAoA=
  • Thread-topic: [RESEND PATCH 08/12] golang/xenlight: add functional options to configure Context


> On Jun 18, 2021, at 6:00 PM, Nick Rosbrook <rosbrookn@xxxxxxxxx> wrote:
> 
> On Fri, Jun 18, 2021 at 04:18:44PM +0000, George Dunlap wrote:
>> 
>> 
>>> On Jun 18, 2021, at 4:08 PM, Nick Rosbrook <rosbrookn@xxxxxxxxx> wrote:
>>> 
>>> On Fri, Jun 18, 2021 at 02:44:15PM +0000, George Dunlap wrote:
>>>> 
>>>> 
>>>>> 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?
>>> 
>>> Part of the motivation for using functional options is to abstract the
>>> "options" struct, yes. This allows internal defaults to be applied more
>>> easily -- if you require e.g. a ContextOptions struct to be passed by
>>> the caller, how do you know if they intended to override a default, or
>>> if they just didn't set the field? Additionally, using the ContextOption
>>> as an interface allows variadic arguments, which are just convenient for
>>> API users -- the same NewContext function can be used whether you need
>>> to pass 3 options or 0.
>>> 
>>> The reason we use ContextOption as an interface, rather than function
>>> pointer of sorts is for flexibility in the signatures of ContextOption
>>> implementations. E.g., we could have
>>> 
>>> func WithLogLevel(lvl LogLevel) ContextOption
>>> func WithLogContext(s string) ContextOption
>>> func WithFooAndBar(s string, n int) ContextOption
>>> 
>>> See [1] for more background on this pattern.
>>> 
>>> Thanks,
>>> NR
>>> 
>>> [1] https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis
>> 
>> Yes, I frequently use a pattern like the one described in that blog post 
>> myself. But that blog post doesn’t use interfaces — the final slide actually 
>> has the “option function” type as an open-coded function pointer type.
>> 
>> So my question was, why not do something like this:
>> 
>> type ContextOption func(*contextOptions) error
>> 
>> func WithLogLevel(level LogLevel) ContextOption {
>>  return func(co *contextOptions) {
>>    co.logLevel = level
>>  }
>> }
>> 
>> ATM the only advantage I can see of defining ContextOption as an interface 
>> rather than as a function pointer is that the godoc for ContextOption would 
>> look like:
>> 
>> type ContextOption interface {
>>   // contains filtered or unexported fields
>> }
>> 
>> Rather than
>> 
>> type ContextOption func(*contextOptions) error
>> 
>> Which shows you the name of the unexported field.
>> 
>> Is there another reason I missed?
> 
> Technically it does allow more flexibility in implementing
> ContextOption, e.g. you could do...
> 
> func (lvl LogLevel) apply(co *contextOptions) { co.logLevel = lvl }
> 
> ...and then pass a LogLevel directly as a ContextOption. But generally
> everyone implements these things as funcs.
> 
> I will admit that when it comes to my choice of using the interface
> version instead of function pointers, I am just more familiar with the
> former and encounter it more often in other Go packages I use.

OK.  It seems a bit weird to me, but that’s not really a good reason to block 
it. :-) I just wanted to make sure I understood why it was being chosen.

Acked-by: George Dunlap <george.dunlap@xxxxxxxxxx>

 


Rackspace

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