|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "strings" |
| 7 | + "sync" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/go-ping/ping" |
| 11 | + "github.com/projecteru2/core/log" |
| 12 | +) |
| 13 | + |
| 14 | +// EndpointPusher pushes endpoints to registered channels if the ep is L3 reachable |
| 15 | +type EndpointPusher struct { |
| 16 | + chans []chan []string |
| 17 | + pendingEndpoints sync.Map |
| 18 | + availableEndpoints sync.Map |
| 19 | +} |
| 20 | + |
| 21 | +// NewEndpointPusher . |
| 22 | +func NewEndpointPusher() *EndpointPusher { |
| 23 | + return &EndpointPusher{} |
| 24 | +} |
| 25 | + |
| 26 | +// Register registers a channel that will receive the endpoints later |
| 27 | +func (p *EndpointPusher) Register(ch chan []string) { |
| 28 | + p.chans = append(p.chans, ch) |
| 29 | +} |
| 30 | + |
| 31 | +// Push pushes endpoint candicates |
| 32 | +func (p *EndpointPusher) Push(endpoints []string) { |
| 33 | + p.delOutdated(endpoints) |
| 34 | + p.addCheck(endpoints) |
| 35 | +} |
| 36 | + |
| 37 | +func (p *EndpointPusher) delOutdated(endpoints []string) { |
| 38 | + newEps := make(map[string]struct{}) |
| 39 | + for _, e := range endpoints { |
| 40 | + newEps[e] = struct{}{} |
| 41 | + } |
| 42 | + |
| 43 | + p.pendingEndpoints.Range(func(key, value interface{}) bool { |
| 44 | + ep, ok := key.(string) |
| 45 | + if !ok { |
| 46 | + log.Error("[EruResolver] failed to cast key while ranging pendingEndpoints") |
| 47 | + return true |
| 48 | + } |
| 49 | + cancel, ok := value.(context.CancelFunc) |
| 50 | + if !ok { |
| 51 | + log.Error("[EruResolver] failed to cast value while ranging pendingEndpoints") |
| 52 | + } |
| 53 | + if _, ok := newEps[ep]; !ok { |
| 54 | + cancel() |
| 55 | + p.pendingEndpoints.Delete(ep) |
| 56 | + log.Debugf("[EruResolver] pending endpoint deleted: %s", ep) |
| 57 | + } |
| 58 | + return true |
| 59 | + }) |
| 60 | + |
| 61 | + p.availableEndpoints.Range(func(key, _ interface{}) bool { |
| 62 | + ep, ok := key.(string) |
| 63 | + if !ok { |
| 64 | + log.Error("[EruResolver] failed to cast key while ranging availableEndpoints") |
| 65 | + return true |
| 66 | + } |
| 67 | + if _, ok := newEps[ep]; !ok { |
| 68 | + p.availableEndpoints.Delete(ep) |
| 69 | + log.Debugf("[EruResolver] available endpoint deleted: %s", ep) |
| 70 | + } |
| 71 | + return true |
| 72 | + }) |
| 73 | +} |
| 74 | + |
| 75 | +func (p *EndpointPusher) addCheck(endpoints []string) { |
| 76 | + for _, endpoint := range endpoints { |
| 77 | + if _, ok := p.pendingEndpoints.Load(endpoint); ok { |
| 78 | + continue |
| 79 | + } |
| 80 | + if _, ok := p.availableEndpoints.Load(endpoint); ok { |
| 81 | + continue |
| 82 | + } |
| 83 | + |
| 84 | + ctx, cancel := context.WithCancel(context.Background()) |
| 85 | + p.pendingEndpoints.Store(endpoint, cancel) |
| 86 | + go p.pollReachability(ctx, endpoint) |
| 87 | + log.Debugf("[EruResolver] pending endpoint added: %s", endpoint) |
| 88 | + } |
| 89 | +} |
| 90 | +func (p *EndpointPusher) pollReachability(ctx context.Context, endpoint string) { |
| 91 | + parts := strings.Split(endpoint, ":") |
| 92 | + if len(parts) != 2 { |
| 93 | + log.Errorf("[EruResolver] wrong format of endpoint: %s", endpoint) |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + for { |
| 98 | + select { |
| 99 | + case <-ctx.Done(): |
| 100 | + log.Debugf("[EruResolver] reachability goroutine ends: %s", endpoint) |
| 101 | + return |
| 102 | + default: |
| 103 | + } |
| 104 | + |
| 105 | + time.Sleep(time.Second) |
| 106 | + if err := p.checkReachability(parts[0]); err != nil { |
| 107 | + continue |
| 108 | + } |
| 109 | + |
| 110 | + p.pendingEndpoints.Delete(endpoint) |
| 111 | + p.availableEndpoints.Store(endpoint, struct{}{}) |
| 112 | + p.pushEndpoints() |
| 113 | + log.Debugf("[EruResolver] available endpoint added: %s", endpoint) |
| 114 | + return |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func (p *EndpointPusher) checkReachability(host string) (err error) { |
| 119 | + pinger, err := ping.NewPinger(host) |
| 120 | + if err != nil { |
| 121 | + log.Errorf("[EruResolver] failed to create pinger: %+v", err) |
| 122 | + return |
| 123 | + } |
| 124 | + defer pinger.Stop() |
| 125 | + |
| 126 | + pinger.Count = 1 |
| 127 | + pinger.Timeout = time.Second |
| 128 | + if err = pinger.Run(); err != nil { |
| 129 | + return |
| 130 | + } |
| 131 | + if pinger.Statistics().PacketsRecv != 1 { |
| 132 | + return errors.New("icmp packet lost") |
| 133 | + } |
| 134 | + return |
| 135 | +} |
| 136 | + |
| 137 | +func (p *EndpointPusher) pushEndpoints() { |
| 138 | + endpoints := []string{} |
| 139 | + p.availableEndpoints.Range(func(key, value interface{}) bool { |
| 140 | + endpoint, ok := key.(string) |
| 141 | + if !ok { |
| 142 | + log.Error("[EruResolver] failed to cast key while ranging availableEndpoints") |
| 143 | + return true |
| 144 | + } |
| 145 | + endpoints = append(endpoints, endpoint) |
| 146 | + return true |
| 147 | + }) |
| 148 | + for _, ch := range p.chans { |
| 149 | + ch <- endpoints |
| 150 | + } |
| 151 | +} |
0 commit comments