From 6e382ed127be28d1971b14dc66e52ebdb3347248 Mon Sep 17 00:00:00 2001 From: Cath3876 Date: Tue, 28 Nov 2023 19:31:59 +0100 Subject: [PATCH] Removed InterceptWithReturn and modified Intercept to now return an EventList and an error --- cmd/bench/stats/stat-interceptor.go | 10 +++----- node.go | 4 ++- pkg/eventlog/interceptor.go | 3 +-- pkg/eventlog/multiinterceptor.go | 15 +++++------- pkg/eventlog/recorder.go | 38 +---------------------------- 5 files changed, 14 insertions(+), 56 deletions(-) diff --git a/cmd/bench/stats/stat-interceptor.go b/cmd/bench/stats/stat-interceptor.go index 91fc605f3..f9e76ac18 100644 --- a/cmd/bench/stats/stat-interceptor.go +++ b/cmd/bench/stats/stat-interceptor.go @@ -26,18 +26,14 @@ func NewStatInterceptor(s *LiveStats, txConsumer t.ModuleID) *StatInterceptor { return &StatInterceptor{s, txConsumer} } -func (i *StatInterceptor) InterceptWithReturn(events *events.EventList) (*events.EventList, error) { - panic("implement me") -} - -func (i *StatInterceptor) Intercept(events *events.EventList) error { +func (i *StatInterceptor) Intercept(events *events.EventList) (*events.EventList, error) { // Avoid nil dereference if Intercept is called on a nil *Recorder and simply do nothing. // This can happen if a pointer type to *Recorder is assigned to a variable with the interface type Interceptor. // Mir would treat that variable as non-nil, thinking there is an interceptor, and call Intercept() on it. // For more explanation, see https://mangatmodi.medium.com/go-check-nil-interface-the-right-way-d142776edef1 if i == nil { - return nil + return events, nil } it := events.Iterator() @@ -66,5 +62,5 @@ func (i *StatInterceptor) Intercept(events *events.EventList) error { } } } - return nil + return events, nil } diff --git a/node.go b/node.go index 8608bd5c2..87b811c88 100644 --- a/node.go +++ b/node.go @@ -461,6 +461,8 @@ func (n *Node) importEvents( // If the interceptor module is present, passes events to it. Otherwise, does nothing. // If an error occurs passing events to the interceptor, notifies the node by means of the workErrorNotifier. +// The interceptor has the ability to modify the EventList. +// The events returned by the interceptor are the events actually delivered to the system's modules // Note: The passed Events should be free of any follow-up Events, // as those will be intercepted separately when processed. // Make sure to call the Strip method of the EventList before passing it to interceptEvents. @@ -473,7 +475,7 @@ func (n *Node) interceptEvents(events *events.EventList) *events.EventList { // For more explanation, see https://mangatmodi.medium.com/go-check-nil-interface-the-right-way-d142776edef1 var err error if n.interceptor != nil { - events, err = n.interceptor.InterceptWithReturn(events) + events, err = n.interceptor.Intercept(events) if err != nil { n.workErrNotifier.Fail(err) } diff --git a/pkg/eventlog/interceptor.go b/pkg/eventlog/interceptor.go index ccabbf959..2bc83851c 100644 --- a/pkg/eventlog/interceptor.go +++ b/pkg/eventlog/interceptor.go @@ -14,6 +14,5 @@ type Interceptor interface { // The implementation of the concrete type must make sure that calling Intercept even on the nil value // does not cause any problems. // For more explanation, see https://mangatmodi.medium.com/go-check-nil-interface-the-right-way-d142776edef1 - Intercept(events *events.EventList) error - InterceptWithReturn(events *events.EventList) (*events.EventList, error) + Intercept(events *events.EventList) (*events.EventList, error) } diff --git a/pkg/eventlog/multiinterceptor.go b/pkg/eventlog/multiinterceptor.go index e274d6141..85143a742 100644 --- a/pkg/eventlog/multiinterceptor.go +++ b/pkg/eventlog/multiinterceptor.go @@ -8,26 +8,23 @@ type repeater struct { interceptors []Interceptor } -func (r *repeater) InterceptWithReturn(events *events.EventList) (*events.EventList, error) { - panic("implement me") -} - -func (r *repeater) Intercept(events *events.EventList) error { +func (r *repeater) Intercept(events *events.EventList) (*events.EventList, error) { // Avoid nil dereference if Intercept is called on a nil *Recorder and simply do nothing. // This can happen if a pointer type to *Recorder is assigned to a variable with the interface type Interceptor. // Mir would treat that variable as non-nil, thinking there is an interceptor, and call Intercept() on it. // For more explanation, see https://mangatmodi.medium.com/go-check-nil-interface-the-right-way-d142776edef1 if r == nil { - return nil + return events, nil } for _, i := range r.interceptors { - if err := i.Intercept(events); err != nil { - return err + _, err := i.Intercept(events) + if err != nil { + return events, err } } - return nil + return events, nil } func MultiInterceptor(interceptors ...Interceptor) Interceptor { diff --git a/pkg/eventlog/recorder.go b/pkg/eventlog/recorder.go index 26898674c..e199970f1 100644 --- a/pkg/eventlog/recorder.go +++ b/pkg/eventlog/recorder.go @@ -136,43 +136,7 @@ func NewRecorder( // If there is no room in the buffer, it blocks. If draining the buffer // to the output stream has completed (successfully or otherwise), Intercept // returns an error. -func (i *Recorder) Intercept(events *events.EventList) error { - - // Avoid nil dereference if Intercept is called on a nil *Recorder and simply do nothing. - // This can happen if a pointer type to *Recorder is assigned to a variable with the interface type Interceptor. - // Mir would treat that variable as non-nil, thinking there is an interceptor, and call Intercept() on it. - // For more explanation, see https://mangatmodi.medium.com/go-check-nil-interface-the-right-way-d142776edef1 - if i == nil { - return nil - } - - // If synchronous writing is enabled, write data and return immediately, without using any channels. - if i.syncWrite { - i.writerLock.Lock() - defer i.writerLock.Unlock() - var err error - _, err = i.writeEvents(events, i.timeSource()) - if err != nil { - return es.Errorf("error writing events: %w", err) - } - if err := i.dest.Flush(); err != nil { - return es.Errorf("error flushing written events: %w", err) - } - return nil - } - - // If writing is asynchronous, pass the record to the background writing goroutine. - select { - case i.eventC <- eventRecord{events, i.timeSource()}: - return nil - case <-i.exitC: - i.exitErrMutex.Lock() - defer i.exitErrMutex.Unlock() - return i.exitErr - } -} - -func (i *Recorder) InterceptWithReturn(events *events.EventList) (*events.EventList, error) { +func (i *Recorder) Intercept(events *events.EventList) (*events.EventList, error) { // Avoid nil dereference if Intercept is called on a nil *Recorder and simply do nothing. // This can happen if a pointer type to *Recorder is assigned to a variable with the interface type Interceptor.