improve voting goroutine
This commit is contained in:
parent
baf01ec809
commit
12729cd206
@ -5,3 +5,4 @@ TODO:
|
||||
+ Decent Logging;
|
||||
+ README Documentation;
|
||||
+ Unit-Tests;
|
||||
+ Add a small cmd cli (named `exip`), using standard go code only;
|
||||
|
25
consensus.go
25
consensus.go
@ -115,28 +115,25 @@ func (c *Consensus) ExternalIP() (net.IP, error) {
|
||||
// start all source Requests on a seperate goroutine
|
||||
for _, v := range c.voters {
|
||||
go func(v voter) {
|
||||
ip, err := v.source.IP()
|
||||
if err == nil && ip == nil {
|
||||
err = InvalidIPError("")
|
||||
}
|
||||
ch <- vote{
|
||||
IP: ip,
|
||||
vote := vote{
|
||||
Count: v.weight,
|
||||
Error: err,
|
||||
Error: InvalidIPError(""),
|
||||
}
|
||||
defer func() {
|
||||
ch <- vote
|
||||
}()
|
||||
vote.IP, vote.Error = v.source.IP()
|
||||
if vote.Error == nil && vote.IP == nil {
|
||||
vote.Error = InvalidIPError("")
|
||||
}
|
||||
}(v)
|
||||
}
|
||||
|
||||
// Wait for all votes to come in
|
||||
var count int
|
||||
for count < len(c.voters) {
|
||||
select {
|
||||
case vote := <-ch:
|
||||
count++
|
||||
for range c.voters {
|
||||
vote := <-ch
|
||||
if vote.Error == nil {
|
||||
voteCollection[vote.IP.String()] += vote.Count
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
3
types.go
3
types.go
@ -6,6 +6,9 @@ import "net"
|
||||
type Source interface {
|
||||
// IP returns IPv4/IPv6 address in a non-error case
|
||||
// net.IP should never be <nil> when error is <nil>
|
||||
// NOTE: it is important that IP doesn't block indefinitely,
|
||||
// as the entire Consensus Logic will be blocked indefinitely as well
|
||||
// if this happens.
|
||||
IP() (net.IP, error)
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user