/
loopback.go
157 lines (134 loc) · 3.54 KB
/
loopback.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2016 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package loopback implements loopback plugin for discovery service.
//
// This plugin simply returns back all the advertisements that are being
// advertised to itself. It would be useful when multiple applications on
// a single runtime advertise their services and want to discover each other.
package loopback
import (
"sync"
"v.io/v23/context"
"v.io/v23/discovery"
idiscovery "v.io/x/ref/lib/discovery"
)
type plugin struct {
mu sync.Mutex
adinfoMap map[string]map[discovery.AdId]*idiscovery.AdInfo // GUARDED_BY(mu)
updated *sync.Cond
updateSeq int // GUARDED_BY(mu)
adStopper *idiscovery.Trigger
}
func (p *plugin) Advertise(ctx *context.T, adinfo *idiscovery.AdInfo, done func()) error {
p.addAd(adinfo)
stop := func() {
p.removeAd(adinfo)
done()
}
p.adStopper.Add(stop, ctx.Done())
return nil
}
func (p *plugin) Scan(ctx *context.T, interfaceName string, callback func(*idiscovery.AdInfo), done func()) error {
updated := p.listenToUpdates(ctx)
go func() {
defer done()
seen := make(map[discovery.AdId]idiscovery.AdInfo)
for {
current := p.getAds(interfaceName)
changed := make([]idiscovery.AdInfo, 0, len(current))
for id, adinfo := range current {
if old, ok := seen[id]; !ok || old.Hash != adinfo.Hash {
changed = append(changed, adinfo)
}
}
for id, adinfo := range seen {
if _, ok := current[id]; !ok {
adinfo.Lost = true
changed = append(changed, adinfo)
}
}
// Push new changes.
for i := range changed {
callback(&changed[i])
}
seen = current
// Wait the next update.
select {
case <-updated:
case <-ctx.Done():
return
}
}
}()
return nil
}
func (p *plugin) Close() {}
func (p *plugin) addAd(adinfo *idiscovery.AdInfo) {
p.mu.Lock()
// Added adinfo to empty key ("") as well to simplify wildcard scan.
for _, key := range []string{adinfo.Ad.InterfaceName, ""} {
adinfos := p.adinfoMap[key]
if adinfos == nil {
adinfos = make(map[discovery.AdId]*idiscovery.AdInfo)
p.adinfoMap[key] = adinfos
}
adinfos[adinfo.Ad.Id] = adinfo
}
p.updateSeq++
p.mu.Unlock()
p.updated.Broadcast()
}
func (p *plugin) removeAd(adinfo *idiscovery.AdInfo) {
p.mu.Lock()
for _, key := range []string{adinfo.Ad.InterfaceName, ""} {
adinfos := p.adinfoMap[key]
delete(adinfos, adinfo.Ad.Id)
if len(adinfos) == 0 {
delete(p.adinfoMap, key)
}
}
p.updateSeq++
p.mu.Unlock()
p.updated.Broadcast()
}
func (p *plugin) getAds(interfaceName string) map[discovery.AdId]idiscovery.AdInfo {
adinfos := make(map[discovery.AdId]idiscovery.AdInfo)
p.mu.Lock()
for id, adinfo := range p.adinfoMap[interfaceName] {
adinfos[id] = *adinfo
}
p.mu.Unlock()
return adinfos
}
func (p *plugin) listenToUpdates(ctx *context.T) <-chan struct{} {
// TODO(jhahn): Find a more efficient way to notify updates.
ch := make(chan struct{})
go func() {
defer close(ch)
var updateSeqSeen int
for {
p.mu.Lock()
for updateSeqSeen == p.updateSeq {
p.updated.Wait()
}
updateSeqSeen = p.updateSeq
p.mu.Unlock()
select {
case ch <- struct{}{}:
case <-ctx.Done():
return
}
}
}()
return ch
}
// New returns a new loopback plugin instance.
func New(ctx *context.T, host string) (idiscovery.Plugin, error) {
p := &plugin{
adinfoMap: make(map[string]map[discovery.AdId]*idiscovery.AdInfo),
adStopper: idiscovery.NewTrigger(),
}
p.updated = sync.NewCond(&p.mu)
return p, nil
}