Repository navigation
Expand file tree
/
Copy pathload.go
More file actions
125 lines (102 loc) · 3.15 KB
/
Copy pathload.go
File metadata and controls
125 lines (102 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package views
import (
"fmt"
"sync"
"sync/atomic"
)
// loader is the loading rule that every engine of this package follows:
//
// - Load parses now and returns the error, so a program can refuse to start.
// - A render on an engine that was never loaded loads it first. Renders that
// arrive together wait for that one load.
// - With Reload(true) every render loads again.
//
// The engine gives it one function, build, that parses everything and
// publishes the result only when the whole parse succeeded. A failed load
// therefore leaves the templates of the previous one in place.
type loader struct {
mu sync.Mutex // serializes loads.
// state is nil until the first load has been tried.
state atomic.Pointer[loadState]
}
// loadState is what a render sees of the loads so far. err is set while the
// engine has nothing it can render, and says why.
type loadState struct {
err error
}
// load runs build and returns its error. It is what Load calls.
func (l *loader) load(build func() error) error {
l.mu.Lock()
defer l.mu.Unlock()
return l.run(build)
}
// run needs l.mu.
func (l *loader) run(build func() error) error {
err := protect(build)
state := l.state.Load()
usable := state != nil && state.err == nil
switch {
case err == nil && !usable:
l.state.Store(&loadState{})
case err != nil && !usable:
l.state.Store(&loadState{err: err})
}
return err
}
// ready is called by every render before it looks a template up. It returns
// nil when there are templates to render with.
func (l *loader) ready(reload bool, build func() error) error {
if reload {
return l.load(build)
}
if state := l.state.Load(); state != nil {
return state.err
}
l.mu.Lock()
defer l.mu.Unlock()
// Another request loaded while this one waited for the lock.
if state := l.state.Load(); state != nil {
return state.err
}
return l.run(build)
}
// add runs fn, which records a template given as text, while no load runs.
// fn is told whether a load has been tried. If one has, fn builds the engine
// again with the new template: it can then be rendered at once, and a
// template the engine cannot load with is refused. If none has, fn only
// checks the template, and the first load picks it up.
//
// fn changes nothing when it returns an error.
func (l *loader) add(fn func(attempted bool) error) error {
l.mu.Lock()
defer l.mu.Unlock()
attempted := l.state.Load() != nil
err := protect(func() error {
return fn(attempted)
})
if err == nil && attempted {
l.state.Store(&loadState{})
}
return err
}
// loaded reports whether a load has succeeded.
func (l *loader) loaded() bool {
state := l.state.Load()
return state != nil && state.err == nil
}
// protect runs fn and turns a panic into an error. The parsers behind the
// engines panic on input they do not accept (html/template on a value that is
// not a function, raymond on a helper with two results), and a template must
// never take the process down with it.
func protect(fn func() error) (err error) {
defer func() {
if r := recover(); r != nil {
if e, ok := r.(error); ok {
err = fmt.Errorf("views: %w", e)
return
}
err = fmt.Errorf("views: %v", r)
}
}()
return fn()
}