commit 6d93532554f164b8149bdddabc085546318ae8fd
parent ef19cb894d0c3100a5b82944c93ff57cb707f1ca
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Mon, 12 Jun 2023 20:06:09 -0700
move code
Diffstat:
2 files changed, 81 insertions(+), 75 deletions(-)
diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go
@@ -30,7 +30,6 @@ import (
"regexp"
"strconv"
"strings"
- "sync"
"syscall"
"time"
"unicode/utf8"
@@ -2272,80 +2271,6 @@ func ChatStreamMenuHandler(c echo.Context) error {
return c.HTML(http.StatusOK, s)
}
-const userMaxStream = 15
-
-var ErrTooManyStreams = errors.New("too many streams")
-
-type UserStreamsMap map[string]int64
-
-func (m *UserStreamsMap) count() (out int64) {
- for _, v := range *m {
- out += v
- }
- return
-}
-
-// UsersStreamsManager ensure that a user doesn't have more than userMaxStream
-// http long polling streams open at the same time.
-// If the limit is reached, the pages will then refuse to load.
-// This is to prevent a malicious user from opening unlimited amount of streams and wasting the server resources.
-type UsersStreamsManager struct {
- sync.RWMutex
- m map[database.UserID]UserStreamsMap
-}
-
-func NewUsersStreamsManager() *UsersStreamsManager {
- return &UsersStreamsManager{m: make(map[database.UserID]UserStreamsMap)}
-}
-
-func (m *UsersStreamsManager) Add(userID database.UserID, chessKey string) error {
- m.Lock()
- defer m.Unlock()
- tmp, found := m.m[userID]
- if found && tmp.count() >= userMaxStream {
- return ErrTooManyStreams
- }
- if !found {
- tmp = make(UserStreamsMap)
- }
- tmp[chessKey]++
- m.m[userID] = tmp
- return nil
-}
-
-func (m *UsersStreamsManager) Remove(userID database.UserID, chessKey string) {
- m.Lock()
- defer m.Unlock()
- if tmp, found := m.m[userID]; found {
- tmp[chessKey]--
- m.m[userID] = tmp
- }
-}
-
-func (m *UsersStreamsManager) GetUserStreamsCountFor(userID database.UserID, key string) (out int64) {
- m.RLock()
- defer m.RUnlock()
- if userMap, found := m.m[userID]; found {
- if nbStreams, found1 := userMap[key]; found1 {
- return nbStreams
- }
- }
- return
-}
-
-func (m *UsersStreamsManager) GetUsers() (out []database.UserID) {
- m.RLock()
- defer m.RUnlock()
- for userID, userMap := range m.m {
- if userMap.count() > 0 {
- out = append(out, userID)
- }
- }
- return
-}
-
-var usersStreamsManager = NewUsersStreamsManager()
-
func closeSignalChan(c echo.Context) <-chan struct{} {
ctx, cancel := context.WithCancel(context.Background())
// Listen to the closing of HTTP connection via CloseNotifier
diff --git a/pkg/web/handlers/usersStreamsManager.go b/pkg/web/handlers/usersStreamsManager.go
@@ -0,0 +1,81 @@
+package handlers
+
+import (
+ "dkforest/pkg/database"
+ "errors"
+ "sync"
+)
+
+const userMaxStream = 15
+
+var ErrTooManyStreams = errors.New("too many streams")
+
+type UserStreamsMap map[string]int64
+
+func (m *UserStreamsMap) count() (out int64) {
+ for _, v := range *m {
+ out += v
+ }
+ return
+}
+
+// UsersStreamsManager ensure that a user doesn't have more than userMaxStream
+// http long polling streams open at the same time.
+// If the limit is reached, the pages will then refuse to load.
+// This is to prevent a malicious user from opening unlimited amount of streams and wasting the server resources.
+type UsersStreamsManager struct {
+ sync.RWMutex
+ m map[database.UserID]UserStreamsMap
+}
+
+func NewUsersStreamsManager() *UsersStreamsManager {
+ return &UsersStreamsManager{m: make(map[database.UserID]UserStreamsMap)}
+}
+
+func (m *UsersStreamsManager) Add(userID database.UserID, chessKey string) error {
+ m.Lock()
+ defer m.Unlock()
+ tmp, found := m.m[userID]
+ if found && tmp.count() >= userMaxStream {
+ return ErrTooManyStreams
+ }
+ if !found {
+ tmp = make(UserStreamsMap)
+ }
+ tmp[chessKey]++
+ m.m[userID] = tmp
+ return nil
+}
+
+func (m *UsersStreamsManager) Remove(userID database.UserID, chessKey string) {
+ m.Lock()
+ defer m.Unlock()
+ if tmp, found := m.m[userID]; found {
+ tmp[chessKey]--
+ m.m[userID] = tmp
+ }
+}
+
+func (m *UsersStreamsManager) GetUserStreamsCountFor(userID database.UserID, key string) (out int64) {
+ m.RLock()
+ defer m.RUnlock()
+ if userMap, found := m.m[userID]; found {
+ if nbStreams, found1 := userMap[key]; found1 {
+ return nbStreams
+ }
+ }
+ return
+}
+
+func (m *UsersStreamsManager) GetUsers() (out []database.UserID) {
+ m.RLock()
+ defer m.RUnlock()
+ for userID, userMap := range m.m {
+ if userMap.count() > 0 {
+ out = append(out, userID)
+ }
+ }
+ return
+}
+
+var usersStreamsManager = NewUsersStreamsManager()