Cache player count instead of querying per user

pull/1/head
Flashfyre 2024-04-01 21:42:48 -04:00
parent fa972bab37
commit 253e462536
2 changed files with 22 additions and 6 deletions

View File

@ -3,20 +3,35 @@ package api
import (
"encoding/json"
"fmt"
"log"
"net/http"
"time"
"github.com/Flashfyre/pokerogue-server/db"
"github.com/go-co-op/gocron"
)
var (
playerCountScheduler = gocron.NewScheduler(time.UTC)
playerCount = 0
)
func SchedulePlayerCountRefresh() {
playerCountScheduler.Every(10).Second().Do(UpdatePlayerCount)
playerCountScheduler.StartAsync()
}
func UpdatePlayerCount() {
var err error
playerCount, err = db.FetchPlayerCount()
if err != nil {
log.Print(err.Error())
}
}
// /game/playercount - get player count
func (s *Server) HandlePlayerCountGet(w http.ResponseWriter, r *http.Request) {
playerCount, err := db.FetchPlayerCount()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
response, err := json.Marshal(playerCount)
if err != nil {
http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError)

View File

@ -43,6 +43,7 @@ func main() {
os.Chmod(*addr, 0777)
}
api.SchedulePlayerCountRefresh()
api.ScheduleDailyRunRefresh()
api.InitDailyRun()