server: put everything under one mutex

Signed-off-by: Naman Sood <mail@nsood.in>
This commit is contained in:
Naman Sood 2021-04-24 12:33:06 -04:00
parent 1a267a1bc6
commit cd57b5cfb2

View file

@ -12,13 +12,9 @@ import (
type server struct { type server struct {
staticHandler http.Handler staticHandler http.Handler
tplMutex sync.RWMutex mu sync.RWMutex
templates map[string]*raymond.Template templates map[string]*raymond.Template
postsMutex sync.RWMutex
postList postList
cssMutex sync.RWMutex
styles map[string]string styles map[string]string
} }
@ -31,43 +27,43 @@ func newServer() (*server, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
s.postsMutex.Lock() s.mu.Lock()
s.postList = posts s.postList = posts
s.postsMutex.Unlock() s.mu.Unlock()
tpls, err := loadTemplates([]string{"page", "fullpost", "summary", "notfound", "error"}) tpls, err := loadTemplates([]string{"page", "fullpost", "summary", "notfound", "error"})
if err != nil { if err != nil {
return nil, err return nil, err
} }
s.tplMutex.Lock() s.mu.Lock()
s.templates = tpls s.templates = tpls
s.tplMutex.Unlock() s.mu.Unlock()
styles, err := newStylesMap() styles, err := newStylesMap()
if err != nil { if err != nil {
return nil, err return nil, err
} }
s.cssMutex.Lock() s.mu.Lock()
s.styles = styles s.styles = styles
s.cssMutex.Unlock() s.mu.Unlock()
postsLn := newPostListener(func(updateFn func(postList) postList) { postsLn := newPostListener(func(updateFn func(postList) postList) {
s.postsMutex.Lock() s.mu.Lock()
defer s.postsMutex.Unlock() defer s.mu.Unlock()
s.postList = updateFn(s.postList) s.postList = updateFn(s.postList)
}) })
go postsLn.listen() go postsLn.listen()
templatesLn := newTemplateListener(func(updateFn func(map[string]*raymond.Template)) { templatesLn := newTemplateListener(func(updateFn func(map[string]*raymond.Template)) {
s.tplMutex.Lock() s.mu.Lock()
defer s.tplMutex.Unlock() defer s.mu.Unlock()
updateFn(s.templates) updateFn(s.templates)
}) })
go templatesLn.listen() go templatesLn.listen()
stylesLn := newStylesListener(func(updateFn func(map[string]string)) { stylesLn := newStylesListener(func(updateFn func(map[string]string)) {
s.cssMutex.Lock() s.mu.Lock()
defer s.cssMutex.Unlock() defer s.mu.Unlock()
updateFn(s.styles) updateFn(s.styles)
}) })
go stylesLn.listen() go stylesLn.listen()
@ -80,8 +76,8 @@ func (s *server) logRequest(req *http.Request) {
} }
func (s *server) router(res http.ResponseWriter, req *http.Request) { func (s *server) router(res http.ResponseWriter, req *http.Request) {
s.tplMutex.RLock() s.mu.RLock()
defer s.tplMutex.RUnlock() defer s.mu.RUnlock()
s.logRequest(req) s.logRequest(req)
res = &errorCatcher{ res = &errorCatcher{
res: res, res: res,
@ -97,8 +93,8 @@ func (s *server) router(res http.ResponseWriter, req *http.Request) {
return return
} }
s.postsMutex.RLock() s.mu.RLock()
defer s.postsMutex.RUnlock() defer s.mu.RUnlock()
for _, p := range s.postList { for _, p := range s.postList {
if p.Slug == slug { if p.Slug == slug {
s.postPage(p, res, req) s.postPage(p, res, req)
@ -149,8 +145,8 @@ func (s *server) homePage(res http.ResponseWriter, req *http.Request) {
var posts string var posts string
s.postsMutex.RLock() s.mu.RLock()
defer s.postsMutex.RUnlock() defer s.mu.RUnlock()
for _, p := range s.postList { for _, p := range s.postList {
summary, err := p.render(s.templates["summary"]) summary, err := p.render(s.templates["summary"])
if err != nil { if err != nil {
@ -169,8 +165,8 @@ func (s *server) homePage(res http.ResponseWriter, req *http.Request) {
} }
func (s *server) loadStylesheet(res http.ResponseWriter, req *http.Request, filename string) (ok bool) { func (s *server) loadStylesheet(res http.ResponseWriter, req *http.Request, filename string) (ok bool) {
s.cssMutex.RLock() s.mu.RLock()
defer s.cssMutex.RUnlock() defer s.mu.RUnlock()
contents, ok := s.styles[filename] contents, ok := s.styles[filename]
if !ok { if !ok {
return false return false