github.com/sandwich-go/boost@v1.3.29/httputil/dns/gen_options_optiongen.go (about)

     1  // Code generated by optiongen. DO NOT EDIT.
     2  // optiongen: github.com/timestee/optiongen
     3  
     4  package dns
     5  
     6  import (
     7  	"net"
     8  	"sync/atomic"
     9  	"time"
    10  	"unsafe"
    11  )
    12  
    13  // Options should use NewOptions to initialize it
    14  type Options struct {
    15  	TTL           time.Duration `xconf:"ttl" usage:"Cache过期ttl"`
    16  	Dialer        Dialer        `xconf:"dialer" usage:"拨号器"`
    17  	Resolver      Resolver      `xconf:"resolver" usage:"解析器"`
    18  	Policy        Policy        `xconf:"policy" usage:"拨号策略"`
    19  	LookupTimeout time.Duration `xconf:"lookup_timeout" usage:"搜索超时"`
    20  	OnLookup      OnLookup      `xconf:"on_lookup" usage:"当成功搜索"`
    21  }
    22  
    23  // NewOptions new Options
    24  func NewOptions(opts ...Option) *Options {
    25  	cc := newDefaultOptions()
    26  	for _, opt := range opts {
    27  		opt(cc)
    28  	}
    29  	if watchDogOptions != nil {
    30  		watchDogOptions(cc)
    31  	}
    32  	return cc
    33  }
    34  
    35  // ApplyOption apply multiple new option and return the old ones
    36  // sample:
    37  // old := cc.ApplyOption(WithTimeout(time.Second))
    38  // defer cc.ApplyOption(old...)
    39  func (cc *Options) ApplyOption(opts ...Option) []Option {
    40  	var previous []Option
    41  	for _, opt := range opts {
    42  		previous = append(previous, opt(cc))
    43  	}
    44  	return previous
    45  }
    46  
    47  // Option option func
    48  type Option func(cc *Options) Option
    49  
    50  // WithTTL Cache过期ttl
    51  func WithTTL(v time.Duration) Option {
    52  	return func(cc *Options) Option {
    53  		previous := cc.TTL
    54  		cc.TTL = v
    55  		return WithTTL(previous)
    56  	}
    57  }
    58  
    59  // WithDialer 拨号器
    60  func WithDialer(v Dialer) Option {
    61  	return func(cc *Options) Option {
    62  		previous := cc.Dialer
    63  		cc.Dialer = v
    64  		return WithDialer(previous)
    65  	}
    66  }
    67  
    68  // WithResolver 解析器
    69  func WithResolver(v Resolver) Option {
    70  	return func(cc *Options) Option {
    71  		previous := cc.Resolver
    72  		cc.Resolver = v
    73  		return WithResolver(previous)
    74  	}
    75  }
    76  
    77  // WithPolicy 拨号策略
    78  func WithPolicy(v Policy) Option {
    79  	return func(cc *Options) Option {
    80  		previous := cc.Policy
    81  		cc.Policy = v
    82  		return WithPolicy(previous)
    83  	}
    84  }
    85  
    86  // WithLookupTimeout 搜索超时
    87  func WithLookupTimeout(v time.Duration) Option {
    88  	return func(cc *Options) Option {
    89  		previous := cc.LookupTimeout
    90  		cc.LookupTimeout = v
    91  		return WithLookupTimeout(previous)
    92  	}
    93  }
    94  
    95  // WithOnLookup 当成功搜索
    96  func WithOnLookup(v OnLookup) Option {
    97  	return func(cc *Options) Option {
    98  		previous := cc.OnLookup
    99  		cc.OnLookup = v
   100  		return WithOnLookup(previous)
   101  	}
   102  }
   103  
   104  // InstallOptionsWatchDog the installed func will called when NewOptions  called
   105  func InstallOptionsWatchDog(dog func(cc *Options)) { watchDogOptions = dog }
   106  
   107  // watchDogOptions global watch dog
   108  var watchDogOptions func(cc *Options)
   109  
   110  // newDefaultOptions new default Options
   111  func newDefaultOptions() *Options {
   112  	cc := &Options{}
   113  
   114  	for _, opt := range [...]Option{
   115  		WithTTL(0),
   116  		WithDialer(defaultDialer),
   117  		WithResolver(net.DefaultResolver),
   118  		WithPolicy(PolicyFirst),
   119  		WithLookupTimeout(3 * time.Second),
   120  		WithOnLookup(nil),
   121  	} {
   122  		opt(cc)
   123  	}
   124  
   125  	return cc
   126  }
   127  
   128  // AtomicSetFunc used for XConf
   129  func (cc *Options) AtomicSetFunc() func(interface{}) { return AtomicOptionsSet }
   130  
   131  // atomicOptions global *Options holder
   132  var atomicOptions unsafe.Pointer
   133  
   134  // onAtomicOptionsSet global call back when  AtomicOptionsSet called by XConf.
   135  // use OptionsInterface.ApplyOption to modify the updated cc
   136  // if passed in cc not valid, then return false, cc will not set to atomicOptions
   137  var onAtomicOptionsSet func(cc OptionsInterface) bool
   138  
   139  // InstallCallbackOnAtomicOptionsSet install callback
   140  func InstallCallbackOnAtomicOptionsSet(callback func(cc OptionsInterface) bool) {
   141  	onAtomicOptionsSet = callback
   142  }
   143  
   144  // AtomicOptionsSet atomic setter for *Options
   145  func AtomicOptionsSet(update interface{}) {
   146  	cc := update.(*Options)
   147  	if onAtomicOptionsSet != nil && !onAtomicOptionsSet(cc) {
   148  		return
   149  	}
   150  	atomic.StorePointer(&atomicOptions, (unsafe.Pointer)(cc))
   151  }
   152  
   153  // AtomicOptions return atomic *OptionsVisitor
   154  func AtomicOptions() OptionsVisitor {
   155  	current := (*Options)(atomic.LoadPointer(&atomicOptions))
   156  	if current == nil {
   157  		defaultOne := newDefaultOptions()
   158  		if watchDogOptions != nil {
   159  			watchDogOptions(defaultOne)
   160  		}
   161  		atomic.CompareAndSwapPointer(&atomicOptions, nil, (unsafe.Pointer)(defaultOne))
   162  		return (*Options)(atomic.LoadPointer(&atomicOptions))
   163  	}
   164  	return current
   165  }
   166  
   167  // all getter func
   168  func (cc *Options) GetTTL() time.Duration           { return cc.TTL }
   169  func (cc *Options) GetDialer() Dialer               { return cc.Dialer }
   170  func (cc *Options) GetResolver() Resolver           { return cc.Resolver }
   171  func (cc *Options) GetPolicy() Policy               { return cc.Policy }
   172  func (cc *Options) GetLookupTimeout() time.Duration { return cc.LookupTimeout }
   173  func (cc *Options) GetOnLookup() OnLookup           { return cc.OnLookup }
   174  
   175  // OptionsVisitor visitor interface for Options
   176  type OptionsVisitor interface {
   177  	GetTTL() time.Duration
   178  	GetDialer() Dialer
   179  	GetResolver() Resolver
   180  	GetPolicy() Policy
   181  	GetLookupTimeout() time.Duration
   182  	GetOnLookup() OnLookup
   183  }
   184  
   185  // OptionsInterface visitor + ApplyOption interface for Options
   186  type OptionsInterface interface {
   187  	OptionsVisitor
   188  	ApplyOption(...Option) []Option
   189  }