blob: 22c9449f59cb7d2de0959eff797875a1a2bbc7e4 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001/*
2Copyright 2014 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package watch
18
19import (
20 "sync"
21)
22
23// FilterFunc should take an event, possibly modify it in some way, and return
24// the modified event. If the event should be ignored, then return keep=false.
25type FilterFunc func(in Event) (out Event, keep bool)
26
27// Filter passes all events through f before allowing them to pass on.
28// Putting a filter on a watch, as an unavoidable side-effect due to the way
29// go channels work, effectively causes the watch's event channel to have its
30// queue length increased by one.
31//
32// WARNING: filter has a fatal flaw, in that it can't properly update the
33// Type field (Add/Modified/Deleted) to reflect items beginning to pass the
34// filter when they previously didn't.
35//
36func Filter(w Interface, f FilterFunc) Interface {
37 fw := &filteredWatch{
38 incoming: w,
39 result: make(chan Event),
40 f: f,
41 }
42 go fw.loop()
43 return fw
44}
45
46type filteredWatch struct {
47 incoming Interface
48 result chan Event
49 f FilterFunc
50}
51
52// ResultChan returns a channel which will receive filtered events.
53func (fw *filteredWatch) ResultChan() <-chan Event {
54 return fw.result
55}
56
57// Stop stops the upstream watch, which will eventually stop this watch.
58func (fw *filteredWatch) Stop() {
59 fw.incoming.Stop()
60}
61
62// loop waits for new values, filters them, and resends them.
63func (fw *filteredWatch) loop() {
64 defer close(fw.result)
65 for event := range fw.incoming.ResultChan() {
66 filtered, keep := fw.f(event)
67 if keep {
68 fw.result <- filtered
69 }
70 }
71}
72
73// Recorder records all events that are sent from the watch until it is closed.
74type Recorder struct {
75 Interface
76
77 lock sync.Mutex
78 events []Event
79}
80
81var _ Interface = &Recorder{}
82
83// NewRecorder wraps an Interface and records any changes sent across it.
84func NewRecorder(w Interface) *Recorder {
85 r := &Recorder{}
86 r.Interface = Filter(w, r.record)
87 return r
88}
89
90// record is a FilterFunc and tracks each received event.
91func (r *Recorder) record(in Event) (Event, bool) {
92 r.lock.Lock()
93 defer r.lock.Unlock()
94 r.events = append(r.events, in)
95 return in, true
96}
97
98// Events returns a copy of the events sent across this recorder.
99func (r *Recorder) Events() []Event {
100 r.lock.Lock()
101 defer r.lock.Unlock()
102 copied := make([]Event, len(r.events))
103 copy(copied, r.events)
104 return copied
105}