-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserve.go
More file actions
138 lines (125 loc) · 4.41 KB
/
serve.go
File metadata and controls
138 lines (125 loc) · 4.41 KB
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
package avpipeline
import (
"context"
"fmt"
"sync"
"github.com/facebookincubator/go-belt"
"github.com/xaionaro-go/avpipeline/logger"
"github.com/xaionaro-go/avpipeline/node"
"github.com/xaionaro-go/avpipeline/node/condition"
"github.com/xaionaro-go/observability"
"github.com/xaionaro-go/xcontext"
)
const (
tooVerbosePTRTracing = false
)
type ServeConfig struct {
EachNode node.ServeConfig
NodeTreeFilter condition.Condition
NodeFilter condition.Condition
AutoServeNewBranches bool
}
func Serve[T node.Abstract](
ctx context.Context,
serveConfig ServeConfig,
errCh chan<- node.Error,
nodes ...T,
) {
var nodesWG sync.WaitGroup
defer nodesWG.Wait()
dstAlreadyVisited := map[node.Abstract]struct{}{}
serve(ctx, serveConfig, errCh, &nodesWG, &dstAlreadyVisited, nodes...)
}
func serve[T node.Abstract](
ctx context.Context,
serveConfig ServeConfig,
errCh chan<- node.Error,
nodesWG *sync.WaitGroup,
dstAlreadyVisited *map[node.Abstract]struct{},
nodes ...T,
) {
for _, n := range nodes {
func(n T) {
if tooVerbosePTRTracing && logger.FromCtx(ctx).Level() >= logger.LevelTrace {
ctx = logger.CtxWithLogger(ctx, logger.FromCtx(ctx).WithMessagePrefix(fmt.Sprintf("%p: ", any(n))))
ctx = belt.WithField(ctx, "node_ptr", fmt.Sprintf("%p", any(n)))
ctx = belt.WithField(ctx, "proc_ptr", fmt.Sprintf("%p", any(n.GetProcessor())))
}
nodeKey := fmt.Sprintf("%s:%p", any(n), any(n))
logger.Tracef(ctx, "Serve[%s]", nodeKey)
if _, ok := (*dstAlreadyVisited)[n]; ok {
logger.Tracef(ctx, "/Serve[%s]: already visited", nodeKey)
return
}
logger.Tracef(ctx, "Serve[%s]: was not visited (%v)", nodeKey, (*dstAlreadyVisited))
(*dstAlreadyVisited)[n] = struct{}{}
if serveConfig.NodeTreeFilter != nil && !serveConfig.NodeTreeFilter.Match(ctx, n) {
logger.Tracef(ctx, "/Serve[%s]: skipped the whole tree", nodeKey)
return
}
ctx, cancel := context.WithCancel(ctx)
childrenCtx := xcontext.DetachDone(ctx)
shouldSkip := false
if serveConfig.NodeFilter != nil && !serveConfig.NodeFilter.Match(ctx, n) {
shouldSkip = true
childrenCtx = ctx // TODO: explain
}
childrenCtx, childrenCancelFn := context.WithCancel(childrenCtx)
pushPacketsChangeChan := n.GetChangeChanPushPacketsTo()
currentPushPacketsTos := n.GetPushPacketsTos(ctx)
pushFramesChangeChan := n.GetChangeChanPushFramesTo()
currentPushFramesTos := n.GetPushFramesTos(ctx)
nodesWG.Add(1)
observability.Go(ctx, func(ctx context.Context) {
defer nodesWG.Done()
for {
select {
case <-ctx.Done():
logger.Tracef(ctx, "/Serve[%s]: context done", nodeKey)
return
case <-pushPacketsChangeChan:
pushPacketsChangeChan = n.GetChangeChanPushPacketsTo()
newPushPacketsTos := n.GetPushPacketsTos(ctx)
newNodes := newPushPacketsTos.Nodes().Without(currentPushPacketsTos.Nodes())
logger.Tracef(ctx, "Serve[%s]: push packets change; new nodes count: %d", nodeKey, len(newNodes))
for _, newNode := range newNodes {
serve(childrenCtx, serveConfig, errCh, nodesWG, dstAlreadyVisited, newNode)
}
currentPushPacketsTos = newPushPacketsTos
case <-pushFramesChangeChan:
pushFramesChangeChan = n.GetChangeChanPushFramesTo()
newPushFramesTos := n.GetPushFramesTos(ctx)
newNodes := newPushFramesTos.Nodes().Without(currentPushFramesTos.Nodes())
logger.Tracef(ctx, "Serve[%s]: push frames change; new nodes count: %d", nodeKey, len(newNodes))
for _, newNode := range newNodes {
serve(childrenCtx, serveConfig, errCh, nodesWG, dstAlreadyVisited, newNode)
}
currentPushFramesTos = newPushFramesTos
}
}
})
for _, pushTo := range currentPushPacketsTos {
serve(childrenCtx, serveConfig, errCh, nodesWG, dstAlreadyVisited, pushTo.Node)
}
for _, pushTo := range currentPushFramesTos {
serve(childrenCtx, serveConfig, errCh, nodesWG, dstAlreadyVisited, pushTo.Node)
}
if shouldSkip {
logger.Tracef(ctx, "/Serve[%s]: skipped", nodeKey)
return
}
logger.Tracef(ctx, "Serve[%s]: starting", nodeKey)
nodesWG.Add(1)
observability.Go(ctx, func(ctx context.Context) {
defer cancel()
defer nodesWG.Done()
defer logger.Tracef(ctx, "/Serve[%s]: ended", nodeKey)
defer func() {
logger.Debugf(ctx, "Serve[%s]: cancelling context...", nodeKey)
childrenCancelFn()
}()
n.Serve(ctx, serveConfig.EachNode, errCh)
})
}(n)
}
}