-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TT-12965] ctx.GetOASDefinition()
has very poor performance
#6471
Comments
Hi! I wonder if you can overcome it by having in memory cache inside plugin. When api is reloaded your plugin will be reloaded in any case. What do you think? |
Hi @buger, we already make heavy use of caching for the authentication part of our plugin. The cache is initialized in the plugin's
We use the same plugin for multiple APIs so we need it to be configurable and make use of the plugin config data. But this would mean we can't ensure that a new plugin config is immediately applied when the API config is changed in Tyk. Because we share the same plugin with multiple APIs, the plugin's |
When api is changed on Tyk side, your plugin is reloaded with api. So its cache kind will be reloaded as well. |
ctx.GetOASDefinition()
has very poor performancectx.GetOASDefinition()
has very poor performance
@JanMa we noticed a similar performance hit in our plugin, out of curiosity how have you been able to work around the issue? Something like this ?
And is your Go plugin an 'Auth' plugin, and are you using the Session state to cache the user, or are you using a custom cache? |
Hi @blagerweij , v := r.Context().Value(ctx.OASDefinition)
if v == nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
apidef, ok := v.(*oas.OAS)
if !ok {
w.WriteHeader(http.StatusInternalServerError)
return
} The plugin is an |
Branch/Environment/Version
Describe the bug
To work around some limitations in Tyks mTLS support (see #6259), I am maintaining a custom go plugin for mTLS at Paymenttools. We recently enhanced that plugin to support OAS APIs as well and noticed a large performance regression.
To get custom per-api plugin configuration at runtime, the plugin calls
ctx.GetOASDefinition
on the context object provided to the plugin. This function performs a deep copy of the stored*oas.OAS
object before returning it which first marshals and then unmarshals the object to JSON.This turns out to be a very expensive operation and severely degrades performance of your plugin if the function is called in the hot path for every incoming request. By removing this function and manually getting the
*oas.OAS
pointer from the passed context, we could reduce our memory usage by 95% and CPU usage by 46% which severely improved the overall performance of our Tyk setup.Below you can see two generated flame graphs which show the difference (please ignore the color inconsistency).
Memory:
CPU:
Proposed solution
We've been running this fix for some time now in production and could not notice any negative side effects which might be explained by not operating on a deep copy of the passed OAS definition. Therefore I propose to remove the unneeded deep copy of the
*oas.OAS
object before returning it.The text was updated successfully, but these errors were encountered: