Skip to content

Conversation

zeripath
Copy link
Contributor

@zeripath zeripath commented Jul 3, 2022

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

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>
@zeripath zeripath added the type/enhancement An improvement of existing functionality label Jul 3, 2022
@zeripath zeripath added this to the 1.18.0 milestone Jul 3, 2022
zeripath added 2 commits July 3, 2022 21:31
Signed-off-by: Andrew Thornton <art27@cantab.net>
Signed-off-by: Andrew Thornton <art27@cantab.net>
@6543
Copy link
Member

6543 commented Jul 4, 2022

lease resolve conflict ;)

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Jul 4, 2022
@lunny
Copy link
Member

lunny commented Jul 4, 2022

I would like to split locale related code to another PR.

@zeripath
Copy link
Contributor Author

zeripath commented Jul 4, 2022

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.

@zeripath
Copy link
Contributor Author

zeripath commented Jul 9, 2022

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>
Copy link
Contributor

@Gusted Gusted left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise L-GTm

Copy link
Contributor

@Gusted Gusted left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Jul 12, 2022
zeripath added 3 commits July 17, 2022 00:39
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>
@zeripath
Copy link
Contributor Author

I am not quite sure about the locale part and if it belongs here… Isn't that pretty similar to #19916?

As I said above the locale work is completely related. The watcher work makes it possible to completely simplify this code.

zeripath and others added 4 commits August 24, 2022 16:44
Signed-off-by: Andrew Thornton <art27@cantab.net>
Signed-off-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: delvh <dev.lh@web.de>
@zeripath
Copy link
Contributor Author

@delvh is there anything else that needs to be done?

Comment on lines +113 to +114
lock.Lock()
defer lock.Unlock()
Copy link
Member

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…

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look at the code.

Copy link
Member

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…

Copy link
Contributor Author

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")
Copy link
Member

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

Copy link
Contributor Author

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.

@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Aug 27, 2022
Signed-off-by: Andrew Thornton <art27@cantab.net>
Signed-off-by: Andrew Thornton <art27@cantab.net>
@codecov-commenter
Copy link

Codecov Report

Merging #20218 (aec5c2f) into main (9e0c437) will increase coverage by 0.00%.
The diff coverage is 50.51%.

@@           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     
Impacted Files Coverage Δ
cmd/admin.go 0.00% <0.00%> (ø)
cmd/web.go 0.00% <0.00%> (ø)
models/activities/action_list.go 74.28% <ø> (ø)
models/activities/notification.go 62.22% <0.00%> (ø)
models/activities/statistic.go 0.00% <0.00%> (ø)
models/admin/task.go 34.00% <ø> (ø)
models/error.go 44.11% <ø> (+5.45%) ⬆️
models/org.go 45.33% <ø> (-17.66%) ⬇️
models/repo/repo.go 61.73% <0.00%> (-0.89%) ⬇️
models/repo/upload.go 2.08% <0.00%> (ø)
... and 163 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@zeripath zeripath merged commit bb0ff77 into go-gitea:main Aug 28, 2022
@zeripath zeripath deleted the load-templates-once-and-better-reload branch August 28, 2022 09:43
@zeripath
Copy link
Contributor Author

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. type/enhancement An improvement of existing functionality
Projects
None yet
9 participants