/
greylist.go
82 lines (70 loc) · 2.24 KB
/
greylist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package greylist
import (
"sync"
"time"
"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/status"
"github.com/hyperledger/fabric-sdk-go/pkg/common/logging"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config/endpoint"
)
var logger = logging.NewLogger("fabsdk/client")
// Filter is a discovery filter that greylists certain peers that are
// known to be down for the configured amount of time
type Filter struct {
// greylistURLs contains a map of peer URLs as keys and timestamps as values
// peers are expired from the greylist based on these timestamps
greylistURLs sync.Map
expiryInterval time.Duration
}
// New creates a new greylist filter with the given expiry interval
func New(expire time.Duration) *Filter {
return &Filter{expiryInterval: expire}
}
// Accept returns whether or not to Accept a peer as a canditate for endorsement
func (b *Filter) Accept(peer fab.Peer) bool {
peerAddress := endpoint.ToAddress(peer.URL())
value, ok := b.greylistURLs.Load(peerAddress)
if ok {
timeAdded, ok := value.(time.Time)
if ok && timeAdded.Add(b.expiryInterval).After(time.Now()) {
logger.Infof("Rejecting peer %s", peer.URL())
return false
}
b.greylistURLs.Delete(peerAddress)
}
return true
}
// Greylist the given peer URL
func (b *Filter) Greylist(err error) {
s, ok := status.FromError(err)
if !ok {
return
}
if ok, peerURL := required(s); ok && peerURL != "" {
logger.Infof("Greylisting peer %s", peerURL)
b.greylistURLs.Store(peerURL, time.Now())
}
}
// required decides whether the given status error warrants a greylist
// on the peer causing the error
func required(s *status.Status) (bool, string) {
if s.Group == status.EndorserClientStatus && s.Code == status.ConnectionFailed.ToInt32() {
return true, peerURLFromConnectionFailedStatus(s.Details)
}
return false, ""
}
// peerURLFromConnectionFailedStatus extracts the peer url from the status error
// details
func peerURLFromConnectionFailedStatus(details []interface{}) string {
if len(details) != 0 {
url, ok := details[0].(string)
if ok {
return endpoint.ToAddress(url)
}
}
return ""
}