-
Notifications
You must be signed in to change notification settings - Fork 46
/
middleware_locker.go
134 lines (115 loc) · 3.38 KB
/
middleware_locker.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
//
// Copyright 2019 Insolar Technologies GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package pubsubwrap
import (
"context"
"sync"
"github.com/ThreeDotsLabs/watermill/message"
"github.com/insolar/insolar/insolar"
"github.com/insolar/insolar/insolar/payload"
"github.com/insolar/insolar/instrumentation/inslogger"
"github.com/insolar/insolar/instrumentation/introspector/introproto"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type stat struct {
sync.Mutex
counters map[payload.Type]int64
}
func (s *stat) get(typ payload.Type) int64 {
s.Lock()
defer s.Unlock()
return s.counters[typ]
}
func (s *stat) inc(typ payload.Type) {
s.Lock()
defer s.Unlock()
s.counters[typ]++
}
type MessageLockerByType struct {
sync.Mutex
types map[payload.Type]struct{}
stat *stat
log insolar.Logger
}
// NewMessageLockerByType is a constructor for MessageLockerByType.
func NewMessageLockerByType(ctx context.Context) *MessageLockerByType {
inslog := inslogger.FromContext(ctx)
return &MessageLockerByType{
types: map[payload.Type]struct{}{},
stat: &stat{
counters: map[payload.Type]int64{},
},
log: inslog,
}
}
func (ml *MessageLockerByType) typeIsFiltered(pt payload.Type) bool {
ml.Lock()
defer ml.Unlock()
_, ok := ml.types[pt]
return ok
}
// Filter returns nil for filtered message types.
func (ml *MessageLockerByType) Filter(m *message.Message) (*message.Message, error) {
typ, err := decodeType(m)
if err != nil {
return m, err
}
if ml.typeIsFiltered(typ) {
ml.stat.inc(typ)
ml.log.Debugf("MessageLocker filtered '%v'", typ.String())
return nil, nil
}
return m, nil
}
// SetMessagesFilter sets filter for provided message type.
func (ml *MessageLockerByType) SetMessagesFilter(ctx context.Context, in *introproto.MessageFilterByType) (*introproto.MessageFilterByType, error) {
if in.Name == "" {
return nil, status.Error(codes.InvalidArgument, "name shouldn't be empty")
}
if _, ok := payload.TypesMap[in.Name]; !ok {
return nil, status.Errorf(codes.InvalidArgument, "'%v' unknown message payload type", in.Name)
}
ml.set(in.Name, in.Enable)
return in, nil
}
func (ml *MessageLockerByType) set(name string, enable bool) {
ml.Lock()
defer ml.Unlock()
typ := payload.TypesMap[name]
if enable {
ml.types[typ] = struct{}{}
} else {
delete(ml.types, typ)
}
}
// GetMessagesFilters returns filter state and statistic per message type (as map).
func (ml *MessageLockerByType) GetMessagesFilters(ctx context.Context, in *introproto.EmptyArgs) (*introproto.AllMessageFilterStats, error) {
ml.Lock()
defer ml.Unlock()
var filters []*introproto.MessageFilterWithStat
for name, typ := range payload.TypesMap {
_, ok := ml.types[typ]
filters = append(filters, &introproto.MessageFilterWithStat{
Name: name,
Enable: ok,
Filtered: ml.stat.get(typ),
})
}
return &introproto.AllMessageFilterStats{
Filters: filters,
}, nil
}