-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Share HTML template renderers and create a watcher framework #20218
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
Share HTML template renderers and create a watcher framework #20218
Conversation
The recovery, API, Web and package frameworks all create their own HTML Renderers. This increases the memory requirements of Gitea unnecessarily with duplicate templates being kept in memory. Further the reloading framework in dev mode for these involves locking and recompiling all of the templates on each load. This will potentially hide concurrency issues and it is inefficient. This PR stores the templates renderer in the context and stores this context in the NormalRoutes, it then creates a fsnotify.Watcher framework to watch files. The watching framework is then extended to the mailer templates which were previously not being reloaded in dev. Then the locales are simplified to a similar structure. Fix go-gitea#20210, go-gitea#20211, go-gitea#20217 Replace go-gitea#20159 Signed-off-by: Andrew Thornton <art27@cantab.net>
Signed-off-by: Andrew Thornton <art27@cantab.net>
Signed-off-by: Andrew Thornton <art27@cantab.net>
lease resolve conflict ;) |
Signed-off-by: Andrew Thornton <art27@cantab.net>
I would like to split locale related code to another PR. |
It's kinda intimately related. We're simply creating a new watcher from the same framework and then completely simplifying the localestore to remove the complicated locking that was previously necessary. I mean if you really want it separated that's fine. |
Signed-off-by: Andrew Thornton <art27@cantab.net>
Testing this locally, it reduces the baseline memory use of Gitea by 17Mb from 80Mb to 63Mb in dev mode. It also speeds up considerably the python package endpoint. |
Signed-off-by: Andrew Thornton <art27@cantab.net>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise L-GTm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
Signed-off-by: Andrew Thornton <art27@cantab.net>
This reverts commit 959595b. syncthing/notify opens goroutines etc even on prod mode. This is unacceptable and therefore we should stick with fsnotify which is already a dependency because of unrolled.
Signed-off-by: Andrew Thornton <art27@cantab.net>
As I said above the locale work is completely related. The watcher work makes it possible to completely simplify this code. |
Signed-off-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: delvh <dev.lh@web.de>
@delvh is there anything else that needs to be done? |
lock.Lock() | ||
defer lock.Unlock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we always be certain that lock == nil <==> setting.IsProd
?
Or is there even a remote possibility that this might be changed at some point?
Because if yes, we will have a NPE here…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Look at the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't mean now, I meant in the future.
I've seen the current code, and here it's a valid assumption.
However, I can't predict the future:
Some code hasn't been changed in a few years.
And other parts get changed on a seemingly monthly basis.
That's why I asked, because once someone changes this assumption, there's a possiblity he won't notice that he created an NPE with it as well…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The assumption is made within the same function.
Any situation that messes with the locking at line 52 that doesn't deal with the lock here and continues to allow the Watcher is a concurrency race and it deserves to NPE and bring down the development server until they think more carefully about concurrency.
It really is also worth remembering that this is ONLY used when !setting.IsProd
All of this code is for dev servers.
import "errors" | ||
|
||
var ( | ||
ErrLocaleAlreadyExist = errors.New("lang already exists") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That will be funny in #20891…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One PR at a time.
Signed-off-by: Andrew Thornton <art27@cantab.net>
Signed-off-by: Andrew Thornton <art27@cantab.net>
Codecov Report
@@ Coverage Diff @@
## main #20218 +/- ##
========================================
Coverage 47.03% 47.04%
========================================
Files 993 1003 +10
Lines 136484 136649 +165
========================================
+ Hits 64198 64287 +89
- Misses 64402 64476 +74
- Partials 7884 7886 +2
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
This should substantial reduce the baseline memory requirements of Gitea through proper sharing of templates. (When wrote this it reduced the memory reqs from 80Mb to 63Mb.) It should also help prevent the hiding of concurrency errors on dev builds and simplifies considerably the locale and localestores. |
The recovery, API, Web and package frameworks all create their own HTML
Renderers. This increases the memory requirements of Gitea
unnecessarily with duplicate templates being kept in memory.
Further the reloading framework in dev mode for these involves locking
and recompiling all of the templates on each load. This will potentially
hide concurrency issues and it is inefficient.
This PR stores the templates renderer in the context and stores this
context in the NormalRoutes, it then creates a fsnotify.Watcher
framework to watch files.
The watching framework is then extended to the mailer templates which
were previously not being reloaded in dev.
Then the locales are simplified to a similar structure.
Fix #20210
Fix #20211
Fix #20217
Signed-off-by: Andrew Thornton art27@cantab.net