2018-02-19 04:26:01 +00:00
|
|
|
package counters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"sync"
|
|
|
|
|
2022-02-21 03:53:13 +00:00
|
|
|
c "git.tuxpa.in/a/gosora/common"
|
|
|
|
qgen "git.tuxpa.in/a/gosora/query_gen"
|
2019-07-28 04:01:33 +00:00
|
|
|
"github.com/pkg/errors"
|
2018-02-19 04:26:01 +00:00
|
|
|
)
|
|
|
|
|
2018-02-22 02:27:17 +00:00
|
|
|
var ForumViewCounter *DefaultForumViewCounter
|
2018-02-19 04:26:01 +00:00
|
|
|
|
|
|
|
// TODO: Unload forum counters without any views over the past 15 minutes, if the admin has configured the forumstore with a cap and it's been hit?
|
|
|
|
// Forums can be reloaded from the database at any time, so we want to keep the counters separate from them
|
|
|
|
type DefaultForumViewCounter struct {
|
|
|
|
oddMap map[int]*RWMutexCounterBucket // map[fid]struct{counter,sync.RWMutex}
|
|
|
|
evenMap map[int]*RWMutexCounterBucket
|
|
|
|
oddLock sync.RWMutex
|
|
|
|
evenLock sync.RWMutex
|
|
|
|
|
|
|
|
insert *sql.Stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefaultForumViewCounter() (*DefaultForumViewCounter, error) {
|
2018-08-04 11:46:36 +00:00
|
|
|
acc := qgen.NewAcc()
|
2019-07-28 04:01:33 +00:00
|
|
|
co := &DefaultForumViewCounter{
|
2018-02-19 04:26:01 +00:00
|
|
|
oddMap: make(map[int]*RWMutexCounterBucket),
|
|
|
|
evenMap: make(map[int]*RWMutexCounterBucket),
|
2020-02-28 23:11:07 +00:00
|
|
|
insert: acc.Insert("viewchunks_forums").Columns("count,createdAt,forum").Fields("?,UTC_TIMESTAMP(),?").Prepare(),
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
2021-05-03 00:36:29 +00:00
|
|
|
c.Tasks.FifteenMin.Add(co.Tick) // There could be a lot of routes, so we don't want to be running this every second
|
|
|
|
//c.Tasks.Sec.Add(co.Tick)
|
|
|
|
c.Tasks.Shutdown.Add(co.Tick)
|
2019-07-28 04:01:33 +00:00
|
|
|
return co, acc.FirstError()
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
|
|
|
|
2019-07-28 04:01:33 +00:00
|
|
|
func (co *DefaultForumViewCounter) Tick() error {
|
|
|
|
cLoop := func(l *sync.RWMutex, m map[int]*RWMutexCounterBucket) error {
|
|
|
|
l.RLock()
|
2020-02-28 23:11:07 +00:00
|
|
|
for fid, f := range m {
|
2019-07-28 04:01:33 +00:00
|
|
|
l.RUnlock()
|
|
|
|
var count int
|
2020-02-28 23:11:07 +00:00
|
|
|
f.RLock()
|
|
|
|
count = f.counter
|
|
|
|
f.RUnlock()
|
2019-07-28 04:01:33 +00:00
|
|
|
// TODO: Only delete the bucket when it's zero to avoid hitting popular forums?
|
|
|
|
l.Lock()
|
2020-02-28 23:11:07 +00:00
|
|
|
delete(m, fid)
|
2019-07-28 04:01:33 +00:00
|
|
|
l.Unlock()
|
2021-04-27 10:20:26 +00:00
|
|
|
e := co.insertChunk(count, fid)
|
|
|
|
if e != nil {
|
2021-05-03 00:36:29 +00:00
|
|
|
return errors.Wrap(errors.WithStack(e), "forum counter")
|
2019-07-28 04:01:33 +00:00
|
|
|
}
|
|
|
|
l.RLock()
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
2019-07-28 04:01:33 +00:00
|
|
|
l.RUnlock()
|
|
|
|
return nil
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
2021-05-03 00:36:29 +00:00
|
|
|
e := cLoop(&co.oddLock, co.oddMap)
|
2021-04-27 10:20:26 +00:00
|
|
|
if e != nil {
|
|
|
|
return e
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
2021-05-03 00:36:29 +00:00
|
|
|
return cLoop(&co.evenLock, co.evenMap)
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 23:11:07 +00:00
|
|
|
func (co *DefaultForumViewCounter) insertChunk(count, forum int) error {
|
2018-02-19 04:26:01 +00:00
|
|
|
if count == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2019-07-28 04:01:33 +00:00
|
|
|
c.DebugLogf("Inserting a vchunk with a count of %d for forum %d", count, forum)
|
2021-04-27 10:20:26 +00:00
|
|
|
_, e := co.insert.Exec(count, forum)
|
|
|
|
return e
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 23:11:07 +00:00
|
|
|
func (co *DefaultForumViewCounter) Bump(fid int) {
|
2018-02-22 02:27:17 +00:00
|
|
|
// Is the ID even?
|
2020-02-28 23:11:07 +00:00
|
|
|
if fid%2 == 0 {
|
2019-07-28 04:01:33 +00:00
|
|
|
co.evenLock.RLock()
|
2020-02-28 23:11:07 +00:00
|
|
|
f, ok := co.evenMap[fid]
|
2019-07-28 04:01:33 +00:00
|
|
|
co.evenLock.RUnlock()
|
2018-02-22 02:27:17 +00:00
|
|
|
if ok {
|
2020-02-28 23:11:07 +00:00
|
|
|
f.Lock()
|
|
|
|
f.counter++
|
|
|
|
f.Unlock()
|
2018-02-22 02:27:17 +00:00
|
|
|
} else {
|
2019-07-28 04:01:33 +00:00
|
|
|
co.evenLock.Lock()
|
2020-02-28 23:11:07 +00:00
|
|
|
co.evenMap[fid] = &RWMutexCounterBucket{counter: 1}
|
2019-07-28 04:01:33 +00:00
|
|
|
co.evenLock.Unlock()
|
2018-02-22 02:27:17 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-28 04:01:33 +00:00
|
|
|
co.oddLock.RLock()
|
2020-02-28 23:11:07 +00:00
|
|
|
f, ok := co.oddMap[fid]
|
2019-07-28 04:01:33 +00:00
|
|
|
co.oddLock.RUnlock()
|
2018-02-22 02:27:17 +00:00
|
|
|
if ok {
|
2020-02-28 23:11:07 +00:00
|
|
|
f.Lock()
|
|
|
|
f.counter++
|
|
|
|
f.Unlock()
|
2018-02-22 02:27:17 +00:00
|
|
|
} else {
|
2019-07-28 04:01:33 +00:00
|
|
|
co.oddLock.Lock()
|
2020-02-28 23:11:07 +00:00
|
|
|
co.oddMap[fid] = &RWMutexCounterBucket{counter: 1}
|
2019-07-28 04:01:33 +00:00
|
|
|
co.oddLock.Unlock()
|
2018-02-22 02:27:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-19 04:26:01 +00:00
|
|
|
// TODO: Add a forum counter backed by two maps which grow as forums are created but never shrinks
|