| 1 | package options |
| 2 | |
| 3 | type RoutingPutSettings struct { |
| 4 | AllowOffline bool |
| 5 | } |
| 6 | |
| 7 | type RoutingPutOption func(*RoutingPutSettings) error |
| 8 | |
| 9 | func RoutingPutOptions(opts ...RoutingPutOption) (*RoutingPutSettings, error) { |
| 10 | options := &RoutingPutSettings{ |
| 11 | AllowOffline: false, |
| 12 | } |
| 13 | |
| 14 | for _, opt := range opts { |
| 15 | err := opt(options) |
| 16 | if err != nil { |
| 17 | return nil, err |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | return options, nil |
| 22 | } |
| 23 | |
| 24 | // nolint deprecated |
| 25 | // Deprecated: use [Routing] instead. |
| 26 | var Put = Routing |
| 27 | |
| 28 | type RoutingProvideSettings struct { |
| 29 | Recursive bool |
| 30 | } |
| 31 | |
| 32 | type RoutingFindProvidersSettings struct { |
| 33 | NumProviders int |
| 34 | } |
| 35 | |
| 36 | type ( |
| 37 | RoutingProvideOption func(*DhtProvideSettings) error |
| 38 | RoutingFindProvidersOption func(*DhtFindProvidersSettings) error |
| 39 | ) |
| 40 | |
| 41 | func RoutingProvideOptions(opts ...RoutingProvideOption) (*RoutingProvideSettings, error) { |
| 42 | options := &RoutingProvideSettings{ |
| 43 | Recursive: false, |
| 44 | } |
| 45 | |
| 46 | for _, opt := range opts { |
| 47 | err := opt(options) |
| 48 | if err != nil { |
| 49 | return nil, err |
| 50 | } |
| 51 | } |
| 52 | return options, nil |
| 53 | } |
| 54 | |
| 55 | func RoutingFindProvidersOptions(opts ...RoutingFindProvidersOption) (*RoutingFindProvidersSettings, error) { |
| 56 | options := &RoutingFindProvidersSettings{ |
| 57 | NumProviders: 20, |
| 58 | } |
| 59 | |
| 60 | for _, opt := range opts { |
| 61 | err := opt(options) |
| 62 | if err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | } |
| 66 | return options, nil |
| 67 | } |
| 68 | |
| 69 | type routingOpts struct{} |
| 70 | |
| 71 | var Routing routingOpts |
| 72 | |
| 73 | // Recursive is an option for [Routing.Provide] which specifies whether to provide |
| 74 | // the given path recursively. |
| 75 | func (routingOpts) Recursive(recursive bool) RoutingProvideOption { |
| 76 | return func(settings *DhtProvideSettings) error { |
| 77 | settings.Recursive = recursive |
| 78 | return nil |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // NumProviders is an option for [Routing.FindProviders] which specifies the |
| 83 | // number of peers to look for. Default is 20. |
| 84 | func (routingOpts) NumProviders(numProviders int) RoutingFindProvidersOption { |
| 85 | return func(settings *DhtFindProvidersSettings) error { |
| 86 | settings.NumProviders = numProviders |
| 87 | return nil |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // AllowOffline is an option for [Routing.Put] which specifies whether to allow |
| 92 | // publishing when the node is offline. Default value is false |
| 93 | func (routingOpts) AllowOffline(allow bool) RoutingPutOption { |
| 94 | return func(settings *RoutingPutSettings) error { |
| 95 | settings.AllowOffline = allow |
| 96 | return nil |
| 97 | } |
| 98 | } |