Oops, need to generate a cached copy of each theme's lists in Hyperdrive.

Renamed common.getTheme to common.GetThemeByReq.
This commit is contained in:
Azareal 2019-04-29 22:23:17 +10:00
parent 48a993d067
commit c8948a778c
2 changed files with 59 additions and 27 deletions

View File

@ -95,7 +95,7 @@ func cascadeForumPerms(fperms *ForumPerms, user *User) {
// Even if they have the right permissions, the control panel is only open to supermods+. There are many areas without subpermissions which assume that the current user is a supermod+ and admins are extremely unlikely to give these permissions to someone who isn't at-least a supermod to begin with // Even if they have the right permissions, the control panel is only open to supermods+. There are many areas without subpermissions which assume that the current user is a supermod+ and admins are extremely unlikely to give these permissions to someone who isn't at-least a supermod to begin with
// TODO: Do a panel specific theme? // TODO: Do a panel specific theme?
func panelUserCheck(w http.ResponseWriter, r *http.Request, user *User) (header *Header, stats PanelStats, rerr RouteError) { func panelUserCheck(w http.ResponseWriter, r *http.Request, user *User) (header *Header, stats PanelStats, rerr RouteError) {
theme := getTheme(r) theme := GetThemeByReq(r)
header = &Header{ header = &Header{
Site: Site, Site: Site,
Settings: SettingBox.Load().(SettingMap), Settings: SettingBox.Load().(SettingMap),
@ -176,7 +176,7 @@ func simpleUserCheck(w http.ResponseWriter, r *http.Request, user *User) (header
}, nil }, nil
} }
func getTheme(r *http.Request) *Theme { func GetThemeByReq(r *http.Request) *Theme {
var theme = &Theme{Name: ""} var theme = &Theme{Name: ""}
cookie, err := r.Cookie("current_theme") cookie, err := r.Cookie("current_theme")
@ -196,7 +196,7 @@ func getTheme(r *http.Request) *Theme {
// TODO: Add the ability for admins to restrict certain themes to certain groups? // TODO: Add the ability for admins to restrict certain themes to certain groups?
// ! Be careful about firing errors off here as CustomError uses this // ! Be careful about firing errors off here as CustomError uses this
func userCheck(w http.ResponseWriter, r *http.Request, user *User) (header *Header, rerr RouteError) { func userCheck(w http.ResponseWriter, r *http.Request, user *User) (header *Header, rerr RouteError) {
theme := getTheme(r) theme := GetThemeByReq(r)
header = &Header{ header = &Header{
Site: Site, Site: Site,
Settings: SettingBox.Load().(SettingMap), Settings: SettingBox.Load().(SettingMap),
@ -219,9 +219,9 @@ func userCheck(w http.ResponseWriter, r *http.Request, user *User) (header *Head
// An optimisation so we don't populate StartedAt for users who shouldn't see the stat anyway // An optimisation so we don't populate StartedAt for users who shouldn't see the stat anyway
// ? - Should we only show this in debug mode? It might be useful for detecting issues in production, if we show it there as-well // ? - Should we only show this in debug mode? It might be useful for detecting issues in production, if we show it there as-well
//if user.IsAdmin { if user.IsAdmin {
header.StartedAt = time.Now() header.StartedAt = time.Now()
//} }
//PrepResources(user,header,theme) //PrepResources(user,header,theme)
return header, nil return header, nil

View File

@ -50,10 +50,11 @@ type Hyperspace struct {
func newHyperspace() *Hyperspace { func newHyperspace() *Hyperspace {
pageCache := new(Hyperspace) pageCache := new(Hyperspace)
pageCache.topicList.Store([]byte("")) blank := make(map[string][]byte,len(c.Themes))
pageCache.gzipTopicList.Store([]byte("")) pageCache.topicList.Store(blank)
pageCache.forumList.Store([]byte("")) pageCache.gzipTopicList.Store(blank)
pageCache.gzipForumList.Store([]byte("")) pageCache.forumList.Store(blank)
pageCache.gzipForumList.Store(blank)
pageCache.lastTopicListUpdate.Store(int64(0)) pageCache.lastTopicListUpdate.Store(int64(0))
return pageCache return pageCache
} }
@ -68,39 +69,50 @@ func tickHdrive(args ...interface{}) (skip bool, rerr c.RouteError) {
c.DebugLog("Refueling...") c.DebugLog("Refueling...")
// Avoid accidentally caching already cached content // Avoid accidentally caching already cached content
hyperspace.topicList.Store([]byte("")) blank := make(map[string][]byte,len(c.Themes))
hyperspace.gzipTopicList.Store([]byte("")) hyperspace.topicList.Store(blank)
hyperspace.forumList.Store([]byte("")) hyperspace.gzipTopicList.Store(blank)
hyperspace.gzipForumList.Store([]byte("")) hyperspace.forumList.Store(blank)
hyperspace.gzipForumList.Store(blank)
tListMap := make(map[string][]byte)
gtListMap := make(map[string][]byte)
fListMap := make(map[string][]byte)
gfListMap := make(map[string][]byte)
var cacheTheme = func(tname string) (skip bool, fail bool, rerr c.RouteError) {
themeCookie := http.Cookie{Name: "current_theme", Value: tname, Path: "/", MaxAge: c.Year}
w := httptest.NewRecorder() w := httptest.NewRecorder()
req := httptest.NewRequest("get", "/topics/", bytes.NewReader(nil)) req := httptest.NewRequest("get", "/topics/", bytes.NewReader(nil))
req.AddCookie(&themeCookie)
user := c.GuestUser user := c.GuestUser
head, rerr := c.UserCheck(w, req, &user) head, rerr := c.UserCheck(w, req, &user)
if rerr != nil { if rerr != nil {
return true, rerr return true, true, rerr
} }
rerr = routes.TopicList(w, req, user, head) rerr = routes.TopicList(w, req, user, head)
if rerr != nil { if rerr != nil {
return true, rerr return true, true, rerr
} }
if w.Code != 200 { if w.Code != 200 {
c.LogWarning(errors.New("not 200 for topic list in hyperdrive")) c.LogWarning(errors.New("not 200 for topic list in hyperdrive"))
return false, nil return false, true, nil
} }
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
buf.ReadFrom(w.Result().Body) buf.ReadFrom(w.Result().Body)
hyperspace.topicList.Store(buf.Bytes()) tListMap[tname] = buf.Bytes()
gbuf, err := c.CompressBytesGzip(buf.Bytes()) gbuf, err := c.CompressBytesGzip(buf.Bytes())
if err != nil { if err != nil {
c.LogWarning(err) c.LogWarning(err)
return false, nil return false, true, nil
} }
hyperspace.gzipTopicList.Store(gbuf) gtListMap[tname] = gbuf
w = httptest.NewRecorder() w = httptest.NewRecorder()
req = httptest.NewRequest("get", "/forums/", bytes.NewReader(nil)) req = httptest.NewRequest("get", "/forums/", bytes.NewReader(nil))
@ -108,39 +120,59 @@ func tickHdrive(args ...interface{}) (skip bool, rerr c.RouteError) {
head, rerr = c.UserCheck(w, req, &user) head, rerr = c.UserCheck(w, req, &user)
if rerr != nil { if rerr != nil {
return true, rerr return true, true, rerr
} }
rerr = routes.ForumList(w, req, user, head) rerr = routes.ForumList(w, req, user, head)
if rerr != nil { if rerr != nil {
return true, rerr return true, true, rerr
} }
if w.Code != 200 { if w.Code != 200 {
c.LogWarning(errors.New("not 200 for forum list in hyperdrive")) c.LogWarning(errors.New("not 200 for forum list in hyperdrive"))
return false, nil return false, true, nil
} }
buf = new(bytes.Buffer) buf = new(bytes.Buffer)
buf.ReadFrom(w.Result().Body) buf.ReadFrom(w.Result().Body)
hyperspace.forumList.Store(buf.Bytes()) fListMap[tname] = buf.Bytes()
gbuf, err = c.CompressBytesGzip(buf.Bytes()) gbuf, err = c.CompressBytesGzip(buf.Bytes())
if err != nil { if err != nil {
c.LogWarning(err) c.LogWarning(err)
return false, nil return false, true, nil
} }
hyperspace.gzipForumList.Store(gbuf) gfListMap[tname] = gbuf
return false, false, nil
}
for tname, _ := range c.Themes {
skip, fail, rerr := cacheTheme(tname)
if fail || rerr != nil {
return skip, rerr
}
}
hyperspace.topicList.Store(tListMap)
hyperspace.gzipTopicList.Store(gtListMap)
hyperspace.forumList.Store(fListMap)
hyperspace.gzipForumList.Store(gfListMap)
hyperspace.lastTopicListUpdate.Store(time.Now().Unix()) hyperspace.lastTopicListUpdate.Store(time.Now().Unix())
return false, nil return false, nil
} }
func jumpHdriveTopicList(args ...interface{}) (skip bool, rerr c.RouteError) { func jumpHdriveTopicList(args ...interface{}) (skip bool, rerr c.RouteError) {
return jumpHdrive(hyperspace.gzipTopicList.Load().([]byte), hyperspace.topicList.Load().([]byte), args) theme := c.GetThemeByReq(args[1].(*http.Request))
p := hyperspace.topicList.Load().(map[string][]byte)
pg := hyperspace.gzipTopicList.Load().(map[string][]byte)
return jumpHdrive(pg[theme.Name], p[theme.Name], args)
} }
func jumpHdriveForumList(args ...interface{}) (skip bool, rerr c.RouteError) { func jumpHdriveForumList(args ...interface{}) (skip bool, rerr c.RouteError) {
return jumpHdrive(hyperspace.gzipForumList.Load().([]byte), hyperspace.forumList.Load().([]byte), args) theme := c.GetThemeByReq(args[1].(*http.Request))
p := hyperspace.forumList.Load().(map[string][]byte)
pg := hyperspace.gzipForumList.Load().(map[string][]byte)
return jumpHdrive(pg[theme.Name], p[theme.Name], args)
} }
func jumpHdrive(pg []byte, p []byte, args []interface{}) (skip bool, rerr c.RouteError) { func jumpHdrive(pg []byte, p []byte, args []interface{}) (skip bool, rerr c.RouteError) {