-
Notifications
You must be signed in to change notification settings - Fork 33
/
middleware.go
54 lines (46 loc) · 1.13 KB
/
middleware.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
package nnet
import (
"github.com/nknorg/nnet/log"
"github.com/nknorg/nnet/util"
)
// ApplyMiddleware add a middleware to node, network, router, etc. If multiple
// middleware of the same type are applied, they will be called in the order of
// being added.
func (nn *NNet) ApplyMiddleware(mw interface{}) error {
applied := false
errs := util.NewErrors()
err := nn.GetLocalNode().ApplyMiddleware(mw)
if err == nil {
applied = true
} else {
errs = append(errs, err)
}
err = nn.Network.ApplyMiddleware(mw)
if err == nil {
applied = true
} else {
errs = append(errs, err)
}
for _, router := range nn.GetRouters() {
err = router.ApplyMiddleware(mw)
if err == nil {
applied = true
} else {
errs = append(errs, err)
}
}
if !applied {
return errs.Merged()
}
return nil
}
// MustApplyMiddleware is the same as ApplyMiddleware, but will panic if an
// error occurs. This is a convenient shortcut if ApplyMiddleware is not
// expected to fail.
func (nn *NNet) MustApplyMiddleware(mw interface{}) {
err := nn.ApplyMiddleware(mw)
if err != nil {
log.Errorf("Apply middleware error: %v", err)
panic(err)
}
}