Skip to content

Commit

Permalink
Removed InterceptWithReturn and modified Intercept to now return an E…
Browse files Browse the repository at this point in the history
…ventList and an error
  • Loading branch information
Cath3876 authored and matejpavlovic committed Dec 1, 2023
1 parent 659f19e commit 6e382ed
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 56 deletions.
10 changes: 3 additions & 7 deletions cmd/bench/stats/stat-interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -66,5 +62,5 @@ func (i *StatInterceptor) Intercept(events *events.EventList) error {
}
}
}
return nil
return events, nil
}
4 changes: 3 additions & 1 deletion node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/eventlog/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
15 changes: 6 additions & 9 deletions pkg/eventlog/multiinterceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
38 changes: 1 addition & 37 deletions pkg/eventlog/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 6e382ed

Please sign in to comment.