Database allocator

This commit is contained in:
2024-10-20 19:04:41 +08:00
parent e5a32bc05a
commit 4adbfe9c19
23 changed files with 536 additions and 29 deletions

View File

@@ -52,9 +52,6 @@ func invokeCommand(c *fiber.Ctx) error {
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else {
if !out.IsDelivered {
log.Debug().Str("id", command).Str("method", method).Msg("Invoking command from HTTP Gateway... failed, delivery not confirmed")
}
c.Set(fiber.HeaderContentType, out.ContentType)
return c.Status(int(out.Status)).Send(out.Payload)
}

View File

@@ -1,6 +1,7 @@
package api
import (
"git.solsynth.dev/hypernet/nexus/pkg/http/ws"
"github.com/gofiber/contrib/websocket"
"github.com/gofiber/fiber/v2"
)
@@ -22,7 +23,7 @@ func MapAPIs(app *fiber.App) {
return err
}*/
return c.Next()
}).Get("/ws", websocket.New(listenWebsocket))
}).Get("/ws", websocket.New(ws.Listen))
app.All("/cgi/:command", invokeCommand)
}

View File

@@ -0,0 +1,95 @@
package ws
import (
"math/rand"
"sync"
"git.solsynth.dev/hypernet/nexus/pkg/internal/models"
"github.com/gofiber/contrib/websocket"
)
var (
wsMutex sync.Mutex
wsConn = make(map[uint]map[uint64]*websocket.Conn)
)
func ClientRegister(user models.Account, conn *websocket.Conn) uint64 {
wsMutex.Lock()
if wsConn[user.ID] == nil {
wsConn[user.ID] = make(map[uint64]*websocket.Conn)
}
clientId := rand.Uint64()
wsConn[user.ID][clientId] = conn
wsMutex.Unlock()
return clientId
}
func ClientUnregister(user models.Account, id uint64) {
wsMutex.Lock()
if wsConn[user.ID] == nil {
wsConn[user.ID] = make(map[uint64]*websocket.Conn)
}
delete(wsConn[user.ID], id)
wsMutex.Unlock()
}
func ClientCount(uid uint) int {
return len(wsConn[uid])
}
func WebsocketPush(uid uint, body []byte) (count int, success int, errs []error) {
for _, conn := range wsConn[uid] {
if err := conn.WriteMessage(1, body); err != nil {
errs = append(errs, err)
} else {
success++
}
count++
}
return
}
func WebsocketPushDirect(clientId uint64, body []byte) (count int, success int, errs []error) {
for _, m := range wsConn {
if conn, ok := m[clientId]; ok {
if err := conn.WriteMessage(1, body); err != nil {
errs = append(errs, err)
} else {
success++
}
count++
}
}
return
}
func WebsocketPushBatch(uidList []uint, body []byte) (count int, success int, errs []error) {
for _, uid := range uidList {
for _, conn := range wsConn[uid] {
if err := conn.WriteMessage(1, body); err != nil {
errs = append(errs, err)
} else {
success++
}
count++
}
}
return
}
func WebsocketPushBatchDirect(clientIdList []uint64, body []byte) (count int, success int, errs []error) {
for _, clientId := range clientIdList {
for _, m := range wsConn {
if conn, ok := m[clientId]; ok {
if err := conn.WriteMessage(1, body); err != nil {
errs = append(errs, err)
} else {
success++
}
count++
}
}
}
return
}

View File

@@ -1,8 +1,7 @@
package api
package ws
import (
"git.solsynth.dev/hypernet/nexus/pkg/internal/models"
"git.solsynth.dev/hypernet/nexus/pkg/internal/services"
"git.solsynth.dev/hypernet/nexus/pkg/nex"
"github.com/gofiber/contrib/websocket"
jsoniter "github.com/json-iterator/go"
@@ -10,11 +9,11 @@ import (
"github.com/spf13/viper"
)
func listenWebsocket(c *websocket.Conn) {
func Listen(c *websocket.Conn) {
user := c.Locals("user").(models.Account)
// Push connection
clientId := services.ClientRegister(user, c)
clientId := ClientRegister(user, c)
log.Debug().
Uint("user", user.ID).
Uint64("clientId", clientId).
@@ -78,7 +77,7 @@ func listenWebsocket(c *websocket.Conn) {
}
// Pop connection
services.ClientUnregister(user, clientId)
ClientUnregister(user, clientId)
log.Debug().
Uint("user", user.ID).
Uint64("clientId", clientId).