tyk
tyk copied to clipboard
[TT-12965] `ctx.GetOASDefinition()` has very poor performance
Branch/Environment/Version
- Branch/Version: 5.4.0
- Environment: On-Prem
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.