Use map to improve message delivery time

This commit is contained in:
LittleSheep 2024-05-07 20:54:01 +08:00
parent 35f9580499
commit fe27b0bf1c
7 changed files with 6621 additions and 17 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,2 @@
#n:public
!<md> [41831, 0, null, null, -2147483648, -2147483648]

10
.idea/workspace.xml generated
View File

@ -4,10 +4,11 @@
<option name="autoReloadType" value="ALL" /> <option name="autoReloadType" value="ALL" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":bug: Dumb man make dumb mistake again"> <list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":bug: Fix new realm owner missing permissions">
<change beforePath="$PROJECT_DIR$/.idea/dataSources/74bcf3ef-a2b9-435b-b9e5-f32902a33b25.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/dataSources/74bcf3ef-a2b9-435b-b9e5-f32902a33b25.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/services/realms.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/services/realms.go" afterDir="false" /> <change beforePath="$PROJECT_DIR$/pkg/server/notify_ws.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/server/notify_ws.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/services/connections.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/services/connections.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/services/notifications.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/services/notifications.go" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -149,7 +150,8 @@
<MESSAGE value=":bug: Bug fix on missing id in realm" /> <MESSAGE value=":bug: Bug fix on missing id in realm" />
<MESSAGE value=":bug: Bug fixes on realm missing member on creation" /> <MESSAGE value=":bug: Bug fixes on realm missing member on creation" />
<MESSAGE value=":bug: Dumb man make dumb mistake again" /> <MESSAGE value=":bug: Dumb man make dumb mistake again" />
<option name="LAST_COMMIT_MESSAGE" value=":bug: Dumb man make dumb mistake again" /> <MESSAGE value=":bug: Fix new realm owner missing permissions" />
<option name="LAST_COMMIT_MESSAGE" value=":bug: Fix new realm owner missing permissions" />
</component> </component>
<component name="VgoProject"> <component name="VgoProject">
<settings-migrated>true</settings-migrated> <settings-migrated>true</settings-migrated>

View File

@ -4,14 +4,13 @@ import (
"git.solsynth.dev/hydrogen/passport/pkg/models" "git.solsynth.dev/hydrogen/passport/pkg/models"
"git.solsynth.dev/hydrogen/passport/pkg/services" "git.solsynth.dev/hydrogen/passport/pkg/services"
"github.com/gofiber/contrib/websocket" "github.com/gofiber/contrib/websocket"
"github.com/samber/lo"
) )
func listenNotifications(c *websocket.Conn) { func listenNotifications(c *websocket.Conn) {
user := c.Locals("principal").(models.Account) user := c.Locals("principal").(models.Account)
// Push connection // Push connection
services.WsConn[user.ID] = append(services.WsConn[user.ID], c) services.ClientRegister(user, c)
// Event loop // Event loop
var err error var err error
@ -22,7 +21,5 @@ func listenNotifications(c *websocket.Conn) {
} }
// Pop connection // Pop connection
services.WsConn[user.ID] = lo.Filter(services.WsConn[user.ID], func(item *websocket.Conn, idx int) bool { services.ClientUnregister(user, c)
return item != c
})
} }

View File

@ -5,13 +5,18 @@ import (
"github.com/gofiber/contrib/websocket" "github.com/gofiber/contrib/websocket"
) )
type WsPushRequest struct { var wsConn = make(map[uint]map[*websocket.Conn]bool)
Payload []byte
RecipientID uint func ClientRegister(user models.Account, conn *websocket.Conn) {
if wsConn[user.ID] == nil {
wsConn[user.ID] = make(map[*websocket.Conn]bool)
}
wsConn[user.ID][conn] = true
} }
var WsConn = make(map[uint][]*websocket.Conn) func ClientUnregister(user models.Account, conn *websocket.Conn) {
if wsConn[user.ID] == nil {
func CheckOnline(user models.Account) bool { wsConn[user.ID] = make(map[*websocket.Conn]bool)
return len(WsConn[user.ID]) > 0 }
delete(wsConn[user.ID], conn)
} }

View File

@ -41,7 +41,7 @@ func NewNotification(notification models.Notification) error {
func PushNotification(notification models.Notification) error { func PushNotification(notification models.Notification) error {
raw, _ := jsoniter.Marshal(notification) raw, _ := jsoniter.Marshal(notification)
for _, conn := range WsConn[notification.RecipientID] { for conn := range wsConn[notification.RecipientID] {
_ = conn.WriteMessage(1, raw) _ = conn.WriteMessage(1, raw)
} }