diff --git a/pkg/mongoproxy/plugins/all/all.go b/pkg/mongoproxy/plugins/all/all.go index 006b3c1..9b33b23 100644 --- a/pkg/mongoproxy/plugins/all/all.go +++ b/pkg/mongoproxy/plugins/all/all.go @@ -8,6 +8,7 @@ import ( _ "github.com/wish/mongoproxy/pkg/mongoproxy/plugins/insort" _ "github.com/wish/mongoproxy/pkg/mongoproxy/plugins/limits" _ "github.com/wish/mongoproxy/pkg/mongoproxy/plugins/mongo" + _ "github.com/wish/mongoproxy/pkg/mongoproxy/plugins/nohint" _ "github.com/wish/mongoproxy/pkg/mongoproxy/plugins/opentracing" _ "github.com/wish/mongoproxy/pkg/mongoproxy/plugins/schema" _ "github.com/wish/mongoproxy/pkg/mongoproxy/plugins/slowlog" diff --git a/pkg/mongoproxy/plugins/nohint/README.md b/pkg/mongoproxy/plugins/nohint/README.md new file mode 100644 index 0000000..f2d456e --- /dev/null +++ b/pkg/mongoproxy/plugins/nohint/README.md @@ -0,0 +1,3 @@ +# nohint + +This plugin simply removes all hints from incoming queries diff --git a/pkg/mongoproxy/plugins/nohint/nohint.go b/pkg/mongoproxy/plugins/nohint/nohint.go new file mode 100644 index 0000000..11aef77 --- /dev/null +++ b/pkg/mongoproxy/plugins/nohint/nohint.go @@ -0,0 +1,59 @@ +package nohint + +import ( + "context" + + "go.mongodb.org/mongo-driver/bson" + + "github.com/wish/mongoproxy/pkg/command" + "github.com/wish/mongoproxy/pkg/mongoproxy/plugins" +) + +const ( + Name = "nohint" +) + +func init() { + plugins.Register(func() plugins.Plugin { + return &NohintPlugin{} + }) +} + +// This is a plugin that simply strips hints out +type NohintPlugin struct { +} + +func (p *NohintPlugin) Name() string { return Name } + +// Configure configures this plugin with the given configuration object. Returns +// an error if the configuration is invalid for the plugin. +func (p *NohintPlugin) Configure(d bson.D) error { + return nil +} + +// Process is the function executed when a message is called in the pipeline. +func (p *NohintPlugin) Process(ctx context.Context, r *plugins.Request, next plugins.PipelineFunc) (bson.D, error) { + switch cmd := r.Command.(type) { + + case *command.Aggregate: + cmd.Hint = nil + + case *command.Count: + cmd.Hint = nil + + case *command.Delete: + cmd.Hint = nil + + case *command.Find: + cmd.Hint = nil + + case *command.Update: + for i, u := range cmd.Updates { + if u.Hint != nil { + u.Hint = nil + cmd.Updates[i] = u + } + } + } + return next(ctx, r) +}