2018-05-03 14:19:53 -07:00
|
|
|
package dht
|
|
|
|
|
2019-05-26 23:33:15 +01:00
|
|
|
import "github.com/libp2p/go-libp2p-core/routing"
|
2018-05-03 14:19:53 -07:00
|
|
|
|
|
|
|
type quorumOptionKey struct{}
|
|
|
|
|
2020-01-21 09:53:07 -08:00
|
|
|
const defaultQuorum = 0
|
2018-07-18 20:22:53 +02:00
|
|
|
|
2018-05-03 14:19:53 -07:00
|
|
|
// Quorum is a DHT option that tells the DHT how many peers it needs to get
|
|
|
|
// values from before returning the best one.
|
|
|
|
//
|
|
|
|
// Default: 16
|
2019-05-26 23:33:15 +01:00
|
|
|
func Quorum(n int) routing.Option {
|
|
|
|
return func(opts *routing.Options) error {
|
2018-05-03 14:19:53 -07:00
|
|
|
if opts.Other == nil {
|
|
|
|
opts.Other = make(map[interface{}]interface{}, 1)
|
|
|
|
}
|
|
|
|
opts.Other[quorumOptionKey{}] = n
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 23:33:15 +01:00
|
|
|
func getQuorum(opts *routing.Options, ndefault int) int {
|
2018-05-03 14:19:53 -07:00
|
|
|
responsesNeeded, ok := opts.Other[quorumOptionKey{}].(int)
|
|
|
|
if !ok {
|
2018-07-18 20:22:53 +02:00
|
|
|
responsesNeeded = ndefault
|
2018-05-03 14:19:53 -07:00
|
|
|
}
|
|
|
|
return responsesNeeded
|
|
|
|
}
|