fix: open MaxMind DB once per process to prevent OOMKill on global middleware - #4
Open
rubarcelk wants to merge 1 commit into
Open
fix: open MaxMind DB once per process to prevent OOMKill on global middleware#4rubarcelk wants to merge 1 commit into
rubarcelk wants to merge 1 commit into
Conversation
…ddleware When this plugin is applied as a global Traefik entrypoint middleware, New() is called once per route at startup. The previous code opened the ~120 MB MaxMind DB file on every New() call, causing a severe memory spike that OOMKilled Traefik pods. The root cause: factoryLookups() called lib.NewLookupCity/Country/Asn on every invocation, each of which called geoip2.NewCityReaderFromFile() and loaded the full database into memory. With hundreds of routes, this means hundreds of ~120 MB readers open simultaneously. Fix: introduce package-level singletons (singletonCity/Country/Asn) protected by a sync.Mutex, so each DB file is opened at most once regardless of how many routes reference the middleware. Subsequent New() calls reuse the cached reader at no extra memory cost. This matches the pattern used in the parent project traefik-plugins/traefikgeoip2 (see its package-level var lookup and if lookup == nil guard). ResetLookup() is restored to actually nil the singletons so tests that need a fresh reader can call it before New() — it was previously a no-op stub with commented-out code. Tested against a Kubernetes cluster with ~280 Ingress routes where global middleware previously caused immediate OOMKill; all 3 Traefik pods remain stable after the fix. Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When this plugin is applied as a global Traefik entrypoint middleware (via
entryPoints.websecure.http.middlewares),New()is called once per route at startup. With hundreds of routes, the previous code opened the ~120 MB MaxMind DB file hundreds of times simultaneously, causing a severe memory spike that OOMKilled Traefik pods immediately on startup.Root cause:
factoryLookups()calledlib.NewLookupCity/Country/Asnon everyNew()invocation, and each of those calledgeoip2.NewCityReaderFromFile()which loads the full database into memory. 280 routes = 280 x 120 MB readers.Fix
Introduce package-level singletons (
singletonCity,singletonCountry,singletonAsn) protected by async.Mutex, so each DB file is opened at most once regardless of how many routes reference the middleware. SubsequentNew()calls reuse the cached reader at zero extra memory cost.This matches the pattern used in the parent project
traefik-plugins/traefikgeoip2(package-levelvar lookup LookupGeoIP2withif lookup == nilguard).ResetLookup()is restored to actually nil the singletons — it was previously a no-op stub with commented-out variables.Testing
Tested against a Kubernetes cluster with ~280 Ingress routes. Previously, Traefik pods OOMKilled within seconds of startup when the global middleware was enabled. After this fix, all pods remain stable with the same configuration.