⚡ Use map to improve message delivery time
This commit is contained in:
parent
35f9580499
commit
fe27b0bf1c
6597
.idea/dataSources/723637bc-6ce3-4bbe-afb3-d88730c75a1b.xml
Normal file
6597
.idea/dataSources/723637bc-6ce3-4bbe-afb3-d88730c75a1b.xml
Normal file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1 @@
|
||||
#n:passport
|
@ -0,0 +1,2 @@
|
||||
#n:public
|
||||
!<md> [41831, 0, null, null, -2147483648, -2147483648]
|
@ -4,10 +4,11 @@
|
||||
<option name="autoReloadType" value="ALL" />
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":bug: Dumb man make dumb mistake again">
|
||||
<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" />
|
||||
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":bug: Fix new realm owner missing permissions">
|
||||
<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>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
@ -149,7 +150,8 @@
|
||||
<MESSAGE value=":bug: Bug fix on missing id in realm" />
|
||||
<MESSAGE value=":bug: Bug fixes on realm missing member on creation" />
|
||||
<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 name="VgoProject">
|
||||
<settings-migrated>true</settings-migrated>
|
||||
|
@ -4,14 +4,13 @@ import (
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/models"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/services"
|
||||
"github.com/gofiber/contrib/websocket"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
func listenNotifications(c *websocket.Conn) {
|
||||
user := c.Locals("principal").(models.Account)
|
||||
|
||||
// Push connection
|
||||
services.WsConn[user.ID] = append(services.WsConn[user.ID], c)
|
||||
services.ClientRegister(user, c)
|
||||
|
||||
// Event loop
|
||||
var err error
|
||||
@ -22,7 +21,5 @@ func listenNotifications(c *websocket.Conn) {
|
||||
}
|
||||
|
||||
// Pop connection
|
||||
services.WsConn[user.ID] = lo.Filter(services.WsConn[user.ID], func(item *websocket.Conn, idx int) bool {
|
||||
return item != c
|
||||
})
|
||||
services.ClientUnregister(user, c)
|
||||
}
|
||||
|
@ -5,13 +5,18 @@ import (
|
||||
"github.com/gofiber/contrib/websocket"
|
||||
)
|
||||
|
||||
type WsPushRequest struct {
|
||||
Payload []byte
|
||||
RecipientID uint
|
||||
var wsConn = make(map[uint]map[*websocket.Conn]bool)
|
||||
|
||||
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 CheckOnline(user models.Account) bool {
|
||||
return len(WsConn[user.ID]) > 0
|
||||
func ClientUnregister(user models.Account, conn *websocket.Conn) {
|
||||
if wsConn[user.ID] == nil {
|
||||
wsConn[user.ID] = make(map[*websocket.Conn]bool)
|
||||
}
|
||||
delete(wsConn[user.ID], conn)
|
||||
}
|
||||
|
@ -41,7 +41,7 @@ func NewNotification(notification models.Notification) error {
|
||||
|
||||
func PushNotification(notification models.Notification) error {
|
||||
raw, _ := jsoniter.Marshal(notification)
|
||||
for _, conn := range WsConn[notification.RecipientID] {
|
||||
for conn := range wsConn[notification.RecipientID] {
|
||||
_ = conn.WriteMessage(1, raw)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user