github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/providers/discovery/broadcast/options.go (about) 1 // Copyright (c) 2021, R.I. Pienaar and the Choria Project contributors 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 package broadcast 6 7 import ( 8 "sync" 9 "time" 10 11 "github.com/choria-io/go-choria/inter" 12 "github.com/choria-io/go-choria/protocol" 13 ) 14 15 type dOpts struct { 16 filter *protocol.Filter 17 collective string 18 msg inter.Message 19 discovered []string 20 cl ChoriaClient 21 mu *sync.Mutex 22 timeout time.Duration 23 dynamicTimeout bool 24 name string 25 } 26 27 // DiscoverOption configures the broadcast discovery method 28 type DiscoverOption func(o *dOpts) 29 30 // Name sets a NATS connection name to use, without this random names will be made. 31 // 32 // This setting is important if you make a daemon that makes many long client connections 33 // as each client connection makes Prometheus stats based on the name and you'll be 34 // leaking many stats over time 35 func Name(n string) DiscoverOption { 36 return func(o *dOpts) { 37 o.name = n 38 } 39 } 40 41 // Filter sets the filter to use for the discovery, else a blank one is used 42 func Filter(f *protocol.Filter) DiscoverOption { 43 return func(o *dOpts) { 44 o.filter = f 45 } 46 } 47 48 // Collective sets the collective to discover in, else main collective is used 49 func Collective(c string) DiscoverOption { 50 return func(o *dOpts) { 51 o.collective = c 52 } 53 } 54 55 // Timeout sets the discovery timeout, else the configured default is used 56 func Timeout(t time.Duration) DiscoverOption { 57 return func(o *dOpts) { 58 o.timeout = t 59 } 60 } 61 62 // SlidingWindow enables a sliding window for discovery timeout that 63 // terminates discovery after 300ms of no responses 64 func SlidingWindow() DiscoverOption { 65 return func(o *dOpts) { 66 o.dynamicTimeout = true 67 } 68 } 69 func choriaClient(c ChoriaClient) DiscoverOption { 70 return func(o *dOpts) { 71 o.cl = c 72 } 73 }