Skip to content

Commit

Permalink
fix: should reset pluginState and other fields (#692)
Browse files Browse the repository at this point in the history
Otherwise, the recorded exec time is incorrect after recycling.
Signed-off-by: spacewander <spacewanderlzx@gmail.com>
  • Loading branch information
spacewander authored Aug 21, 2024
1 parent a05cdd8 commit db65556
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
11 changes: 11 additions & 0 deletions api/internal/consumer/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,14 @@ func (p *filterPlugin) Factory() api.FilterFactory {
func (p *filterPlugin) Config() api.PluginConfig {
return &Config{}
}

type MockConsumer struct {
}

func (c *MockConsumer) Name() string {
return "mock"
}

func (c *MockConsumer) PluginConfig(_ string) api.PluginConsumerConfig {
return &ConsumerConfig{}
}
3 changes: 3 additions & 0 deletions api/pkg/filtermanager/api_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,10 @@ func (cb *filterManagerCallbackHandler) Reset() {
// We don't reset namespace, as filterManager will only be reused in the same route,
// which must have the same namespace.
cb.consumer = nil
cb.pluginState = nil
cb.streamInfo = nil
cb.logArgNames = ""
cb.logArgs = nil

cb.cacheLock.Unlock()
}
Expand Down
24 changes: 24 additions & 0 deletions api/pkg/filtermanager/api_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/agiledragon/gomonkey/v2"
"github.com/stretchr/testify/assert"

"mosn.io/htnn/api/internal/consumer"
"mosn.io/htnn/api/pkg/filtermanager/api"
"mosn.io/htnn/api/pkg/filtermanager/model"
"mosn.io/htnn/api/plugins/tests/pkg/envoy"
Expand Down Expand Up @@ -233,3 +234,26 @@ func TestLogWithArgs(t *testing.T) {
assert.Equal(t, []any{0, 1, 2}, fmtArgs[level+"f"])
}
}

func TestReset(t *testing.T) {
cb := &filterManagerCallbackHandler{
FilterCallbackHandler: envoy.NewCAPIFilterCallbackHandler(),
}
cb.SetConsumer(&consumer.MockConsumer{})
cb.PluginState()
cb.StreamInfo()
cb.WithLogArg("k", "v")

assert.NotNil(t, cb.consumer)
assert.NotNil(t, cb.pluginState)
assert.NotNil(t, cb.streamInfo)
assert.NotEqual(t, "", cb.logArgNames)
assert.NotNil(t, cb.logArgs)

cb.Reset()
assert.Nil(t, cb.consumer)
assert.Nil(t, cb.pluginState)
assert.Nil(t, cb.streamInfo)
assert.Equal(t, "", cb.logArgNames)
assert.Nil(t, cb.logArgs)
}

0 comments on commit db65556

Please sign in to comment.