Share HTML template renderers and create a watcher framework (#20218)

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>
This commit is contained in:
zeripath 2022-08-28 10:43:25 +01:00 committed by GitHub
parent c21d6511a8
commit bb0ff77e46
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
43 changed files with 902 additions and 618 deletions

View file

@ -5,6 +5,7 @@
package auth
import (
"context"
"net/http"
"reflect"
"strings"
@ -51,14 +52,14 @@ func (b *Group) Name() string {
}
// Init does nothing as the Basic implementation does not need to allocate any resources
func (b *Group) Init() error {
func (b *Group) Init(ctx context.Context) error {
for _, method := range b.methods {
initializable, ok := method.(Initializable)
if !ok {
continue
}
if err := initializable.Init(); err != nil {
if err := initializable.Init(ctx); err != nil {
return err
}
}

View file

@ -34,7 +34,7 @@ type Method interface {
type Initializable interface {
// Init should be called exactly once before using any of the other methods,
// in order to allow the plugin to allocate necessary resources
Init() error
Init(ctx context.Context) error
}
// Named represents a named thing

View file

@ -5,6 +5,7 @@
package auth
import (
"context"
"errors"
"net/http"
"strings"
@ -52,21 +53,14 @@ type SSPI struct {
}
// Init creates a new global websspi.Authenticator object
func (s *SSPI) Init() error {
func (s *SSPI) Init(ctx context.Context) error {
config := websspi.NewConfig()
var err error
sspiAuth, err = websspi.New(config)
if err != nil {
return err
}
s.rnd = render.New(render.Options{
Extensions: []string{".tmpl"},
Directory: "templates",
Funcs: templates.NewFuncMap(),
Asset: templates.GetAsset,
AssetNames: templates.GetAssetNames,
IsDevelopment: !setting.IsProd,
})
_, s.rnd = templates.HTMLRenderer(ctx)
return nil
}

View file

@ -7,6 +7,7 @@ package mailer
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"hash/fnv"
@ -348,7 +349,7 @@ var mailQueue queue.Queue
var Sender gomail.Sender
// NewContext start mail queue service
func NewContext() {
func NewContext(ctx context.Context) {
// Need to check if mailQueue is nil because in during reinstall (user had installed
// before but switched install lock off), this function will be called again
// while mail queue is already processing tasks, and produces a race condition.
@ -381,7 +382,7 @@ func NewContext() {
go graceful.GetManager().RunWithShutdownFns(mailQueue.Run)
subjectTemplates, bodyTemplates = templates.Mailer()
subjectTemplates, bodyTemplates = templates.Mailer(ctx)
}
// SendAsync send mail asynchronously