options.go 2.33 KB
Newer Older
1 2 3 4 5 6 7
package kbucket

import (
	"fmt"
	"time"
)

Aarsh Shah's avatar
Aarsh Shah committed
8 9
// Option is the Routing Table functional option type.
type Option func(*options) error
10

Aarsh Shah's avatar
Aarsh Shah committed
11 12 13 14 15 16 17
// options is a structure containing all the functional options that can be used when constructing a Routing Table.
type options struct {
	tableCleanup struct {
		peerValidationFnc     PeerValidationFunc
		peersForValidationFnc PeerSelectionFunc
		peerValidationTimeout time.Duration
		interval              time.Duration
18 19 20
	}
}

Aarsh Shah's avatar
Aarsh Shah committed
21 22
// apply applies the given options to this option.
func (o *options) apply(opts ...Option) error {
23 24 25 26 27 28 29 30
	for i, opt := range opts {
		if err := opt(o); err != nil {
			return fmt.Errorf("routing table option %d failed: %s", i, err)
		}
	}
	return nil
}

Aarsh Shah's avatar
Aarsh Shah committed
31 32
// PeerValidationFnc configures the Peer Validation function used for RT cleanup.
// Not configuring this disables Routing Table cleanup.
Aarsh Shah's avatar
Aarsh Shah committed
33
func PeerValidationFnc(f PeerValidationFunc) Option {
Aarsh Shah's avatar
Aarsh Shah committed
34 35
	return func(o *options) error {
		o.tableCleanup.peerValidationFnc = f
Aarsh Shah's avatar
Aarsh Shah committed
36 37 38 39
		return nil
	}
}

40
// PeersForValidationFnc configures the function that will be used to select the peers that need to be validated during cleanup.
Aarsh Shah's avatar
Aarsh Shah committed
41
func PeersForValidationFnc(f PeerSelectionFunc) Option {
Aarsh Shah's avatar
Aarsh Shah committed
42 43
	return func(o *options) error {
		o.tableCleanup.peersForValidationFnc = f
44 45 46 47 48
		return nil
	}
}

// TableCleanupInterval configures the interval between two runs of the Routing Table cleanup routine.
Aarsh Shah's avatar
Aarsh Shah committed
49
func TableCleanupInterval(i time.Duration) Option {
Aarsh Shah's avatar
Aarsh Shah committed
50 51
	return func(o *options) error {
		o.tableCleanup.interval = i
52 53 54 55 56
		return nil
	}
}

// PeerValidationTimeout sets the timeout for a single peer validation during cleanup.
Aarsh Shah's avatar
Aarsh Shah committed
57
func PeerValidationTimeout(timeout time.Duration) Option {
Aarsh Shah's avatar
Aarsh Shah committed
58 59
	return func(o *options) error {
		o.tableCleanup.peerValidationTimeout = timeout
60 61 62 63 64 65
		return nil
	}
}

// Defaults are the default options. This option will be automatically
// prepended to any options you pass to the Routing Table constructor.
Aarsh Shah's avatar
Aarsh Shah committed
66 67 68
var Defaults = func(o *options) error {
	o.tableCleanup.peerValidationTimeout = 30 * time.Second
	o.tableCleanup.interval = 2 * time.Minute
69 70

	// default selector function selects all peers that are in missing state.
Aarsh Shah's avatar
Aarsh Shah committed
71
	o.tableCleanup.peersForValidationFnc = func(peers []PeerInfo) []PeerInfo {
72 73 74 75 76 77 78 79 80 81
		var selectedPeers []PeerInfo
		for _, p := range peers {
			if p.State == PeerStateMissing {
				selectedPeers = append(selectedPeers, p)
			}
		}
		return selectedPeers
	}

	return nil
Aarsh Shah's avatar
go fmt  
Aarsh Shah committed
82
}