forked from usnistgov/ndn-dpdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrximpl.go
89 lines (76 loc) · 1.58 KB
/
rximpl.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
package socketface
import (
"sync"
"sync/atomic"
"github.com/usnistgov/ndn-dpdk/iface"
"github.com/zyedidia/generic/mapset"
"go.uber.org/zap"
)
type rxGroup interface {
iface.RxGroup
close()
run(face *socketFace) error
}
type rxImpl struct {
describe string
nilValue any
instance atomic.Value
nFaces atomic.Int32
create func() (rxGroup, error)
}
func (impl *rxImpl) String() string {
return impl.describe
}
func (impl *rxImpl) start(face *socketFace) error {
id, ctx := face.ID(), face.transport.Context()
if impl.nFaces.Add(1) == 1 {
rxg, e := impl.create()
if e != nil {
return e
}
impl.instance.Store(rxg)
}
go func() {
defer impl.stop()
if rxg, _ := impl.instance.Load().(rxGroup); rxg != nil {
if e := rxg.run(face); e != nil && ctx.Err() == nil {
logger.Error("face RX stopped with error", id.ZapField("id"), zap.Error(e))
}
}
}()
return nil
}
func (impl *rxImpl) stop() {
if impl.nFaces.Add(-1) > 0 {
return
}
rxg := impl.instance.Swap(impl.nilValue).(rxGroup)
rxg.close()
}
type rxFaceList struct {
set *mapset.Set[*socketFace]
lock sync.RWMutex
}
func (fl *rxFaceList) faceListPut(face *socketFace) func() {
fl.lock.Lock()
defer fl.lock.Unlock()
if fl.set == nil {
s := mapset.New[*socketFace]()
fl.set = &s
}
fl.set.Put(face)
return func() {
fl.lock.Lock()
defer fl.lock.Unlock()
fl.set.Remove(face)
}
}
// Faces implements RxGroup interface.
func (fl *rxFaceList) Faces() (list []iface.Face) {
fl.lock.RLock()
defer fl.lock.RUnlock()
fl.set.Each(func(face *socketFace) {
list = append(list, face)
})
return list
}