Mirror of @tangled.org/core. Running on a Raspberry Pi Zero 2 (Please be gentle).
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

markup/sanitizer: build bluemonday policies once at package init

Both policies were rebuilt on every NewSanitizer() call. This included
compiling the chroma syntax-highlight class regex by iterating
chroma.StandardTypes and joining ~200 type names into a regex alternation,
on every invocation — including from inside the pull request HTTP handler,
making it a per-request cost.

Move both policies to package-level vars initialized once in init().
NewSanitizer() is now a pair of pointer assignments. This is safe per the
bluemonday README: "it is safe to use the policy in multiple goroutines".

Signed-off-by: Matías Insaurralde <matias@insaurral.de>

authored by

Matías Insaurralde and committed by tangled.org 4fb9e0a1 ae3c8205

+15 -4
+15 -4
appview/pages/markup/sanitizer.go
··· 10 10 "github.com/microcosm-cc/bluemonday" 11 11 ) 12 12 13 + // shared policies built once at init; safe for concurrent use per bluemonday docs 14 + var ( 15 + sharedDefaultPolicy *bluemonday.Policy 16 + sharedDescriptionPolicy *bluemonday.Policy 17 + ) 18 + 19 + func init() { 20 + sharedDefaultPolicy = buildDefaultPolicy() 21 + sharedDescriptionPolicy = buildDescriptionPolicy() 22 + } 23 + 13 24 type Sanitizer struct { 14 25 defaultPolicy *bluemonday.Policy 15 26 descriptionPolicy *bluemonday.Policy ··· 28 17 29 18 func NewSanitizer() Sanitizer { 30 19 return Sanitizer{ 31 - defaultPolicy: defaultPolicy(), 32 - descriptionPolicy: descriptionPolicy(), 20 + defaultPolicy: sharedDefaultPolicy, 21 + descriptionPolicy: sharedDescriptionPolicy, 33 22 } 34 23 } 35 24 ··· 40 29 return s.descriptionPolicy.Sanitize(html) 41 30 } 42 31 43 - func defaultPolicy() *bluemonday.Policy { 32 + func buildDefaultPolicy() *bluemonday.Policy { 44 33 policy := bluemonday.UGCPolicy() 45 34 46 35 // Allow generally safe attributes ··· 134 123 return policy 135 124 } 136 125 137 - func descriptionPolicy() *bluemonday.Policy { 126 + func buildDescriptionPolicy() *bluemonday.Policy { 138 127 policy := bluemonday.NewPolicy() 139 128 policy.AllowStandardURLs() 140 129