♻️ Refactor websocket client id
This commit is contained in:
parent
1f07c0edf4
commit
8ac88413e0
@ -20,7 +20,7 @@ func (v *Server) CountStreamConnection(ctx context.Context, request *proto.Count
|
||||
|
||||
func (v *Server) PushStream(ctx context.Context, request *proto.PushStreamRequest) (*proto.PushStreamResponse, error) {
|
||||
var cnt int
|
||||
var successes []uint64
|
||||
var successes []string
|
||||
var errs []error
|
||||
if request.UserId != nil {
|
||||
cnt, successes, errs = ws.WebsocketPush(uint(request.GetUserId()), request.GetBody())
|
||||
@ -32,7 +32,7 @@ func (v *Server) PushStream(ctx context.Context, request *proto.PushStreamReques
|
||||
|
||||
success := len(successes)
|
||||
log.Debug().
|
||||
Uint64("client_id", request.GetClientId()).
|
||||
Str("client_id", request.GetClientId()).
|
||||
Uint64("user_id", request.GetUserId()).
|
||||
Int("count", cnt).
|
||||
Int("success", success).
|
||||
@ -61,7 +61,7 @@ func (v *Server) PushStream(ctx context.Context, request *proto.PushStreamReques
|
||||
|
||||
func (v *Server) PushStreamBatch(ctx context.Context, request *proto.PushStreamBatchRequest) (*proto.PushStreamResponse, error) {
|
||||
var cnt int
|
||||
var successes []uint64
|
||||
var successes []string
|
||||
var errs []error
|
||||
if len(request.UserId) != 0 {
|
||||
cnt, successes, errs = ws.WebsocketPushBatch(
|
||||
|
@ -1,11 +1,12 @@
|
||||
package ws
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/internal/directory"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"github.com/google/uuid"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/gofiber/contrib/websocket"
|
||||
@ -13,20 +14,28 @@ import (
|
||||
|
||||
var (
|
||||
wsMutex sync.Mutex
|
||||
wsConn = make(map[uint]map[uint64]*websocket.Conn)
|
||||
wsConn = make(map[uint]map[string]*websocket.Conn)
|
||||
)
|
||||
|
||||
func ClientRegister(user sec.UserInfo, conn *websocket.Conn) uint64 {
|
||||
func ClientRegister(user sec.UserInfo, conn *websocket.Conn) (string, error) {
|
||||
wsMutex.Lock()
|
||||
if wsConn[user.ID] == nil {
|
||||
wsConn[user.ID] = make(map[uint64]*websocket.Conn)
|
||||
wsConn[user.ID] = make(map[string]*websocket.Conn)
|
||||
}
|
||||
var clientId string
|
||||
if userDefinedId := conn.Query("clientId"); len(userDefinedId) > 0 {
|
||||
clientId = userDefinedId
|
||||
} else {
|
||||
clientId = uuid.NewString()
|
||||
}
|
||||
if _, ok := wsConn[user.ID][clientId]; ok {
|
||||
return clientId, fmt.Errorf("client already conncted")
|
||||
}
|
||||
clientId := rand.Uint64()
|
||||
wsConn[user.ID][clientId] = conn
|
||||
wsMutex.Unlock()
|
||||
|
||||
log.Debug().
|
||||
Uint64("client_id", clientId).
|
||||
Str("client_id", clientId).
|
||||
Uint("user_id", user.ID).
|
||||
Msg("An client connected to stream endpoint...")
|
||||
|
||||
@ -35,19 +44,19 @@ func ClientRegister(user sec.UserInfo, conn *websocket.Conn) uint64 {
|
||||
"id": clientId,
|
||||
})
|
||||
|
||||
return clientId
|
||||
return clientId, nil
|
||||
}
|
||||
|
||||
func ClientUnregister(user sec.UserInfo, id uint64) {
|
||||
func ClientUnregister(user sec.UserInfo, id string) {
|
||||
wsMutex.Lock()
|
||||
if wsConn[user.ID] == nil {
|
||||
wsConn[user.ID] = make(map[uint64]*websocket.Conn)
|
||||
wsConn[user.ID] = make(map[string]*websocket.Conn)
|
||||
}
|
||||
delete(wsConn[user.ID], id)
|
||||
wsMutex.Unlock()
|
||||
|
||||
log.Debug().
|
||||
Uint64("client_id", id).
|
||||
Str("client_id", id).
|
||||
Uint("user_id", user.ID).
|
||||
Msg("An client disconnected from stream endpoint...")
|
||||
|
||||
@ -61,19 +70,19 @@ func ClientCount(uid uint) int {
|
||||
return len(wsConn[uid])
|
||||
}
|
||||
|
||||
func WebsocketPush(uid uint, body []byte) (count int, successes []uint64, errs []error) {
|
||||
func WebsocketPush(uid uint, body []byte) (count int, successes []string, errs []error) {
|
||||
for _, conn := range wsConn[uid] {
|
||||
if err := conn.WriteMessage(1, body); err != nil {
|
||||
errs = append(errs, err)
|
||||
} else {
|
||||
successes = append(successes, uint64(uid))
|
||||
successes = append(successes, fmt.Sprintf("%d", uid))
|
||||
}
|
||||
count++
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func WebsocketPushDirect(clientId uint64, body []byte) (count int, successes []uint64, errs []error) {
|
||||
func WebsocketPushDirect(clientId string, body []byte) (count int, successes []string, errs []error) {
|
||||
for _, m := range wsConn {
|
||||
if conn, ok := m[clientId]; ok {
|
||||
if err := conn.WriteMessage(1, body); err != nil {
|
||||
@ -87,13 +96,13 @@ func WebsocketPushDirect(clientId uint64, body []byte) (count int, successes []u
|
||||
return
|
||||
}
|
||||
|
||||
func WebsocketPushBatch(uidList []uint, body []byte) (count int, successes []uint64, errs []error) {
|
||||
func WebsocketPushBatch(uidList []uint, body []byte) (count int, successes []string, errs []error) {
|
||||
for _, uid := range uidList {
|
||||
for _, conn := range wsConn[uid] {
|
||||
if err := conn.WriteMessage(1, body); err != nil {
|
||||
errs = append(errs, err)
|
||||
} else {
|
||||
successes = append(successes, uint64(uid))
|
||||
successes = append(successes, fmt.Sprintf("%d", uid))
|
||||
}
|
||||
count++
|
||||
}
|
||||
@ -101,7 +110,7 @@ func WebsocketPushBatch(uidList []uint, body []byte) (count int, successes []uin
|
||||
return
|
||||
}
|
||||
|
||||
func WebsocketPushBatchDirect(clientIdList []uint64, body []byte) (count int, successes []uint64, errs []error) {
|
||||
func WebsocketPushBatchDirect(clientIdList []string, body []byte) (count int, successes []string, errs []error) {
|
||||
for _, clientId := range clientIdList {
|
||||
for _, m := range wsConn {
|
||||
if conn, ok := m[clientId]; ok {
|
||||
|
@ -11,24 +11,34 @@ import (
|
||||
"github.com/gofiber/contrib/websocket"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/samber/lo"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func Listen(c *websocket.Conn) {
|
||||
user, ok := c.Locals("nex_user").(*sec.UserInfo)
|
||||
if !ok {
|
||||
_ = c.WriteMessage(1, nex.WebSocketPackage{
|
||||
Action: "error",
|
||||
Message: "unauthorized",
|
||||
}.Marshal())
|
||||
c.Close()
|
||||
return
|
||||
}
|
||||
|
||||
// Push connection
|
||||
clientId := ClientRegister(*user, c)
|
||||
var err error
|
||||
clientId, err := ClientRegister(*user, c)
|
||||
if err != nil {
|
||||
_ = c.WriteMessage(1, nex.WebSocketPackage{
|
||||
Action: "error",
|
||||
Message: "client with this id already connected",
|
||||
}.Marshal())
|
||||
c.Close()
|
||||
return
|
||||
}
|
||||
|
||||
// Event loop
|
||||
var mt int
|
||||
var data []byte
|
||||
var err error
|
||||
|
||||
var packet nex.WebSocketPackage
|
||||
|
||||
for {
|
||||
@ -42,11 +52,6 @@ func Listen(c *websocket.Conn) {
|
||||
continue
|
||||
}
|
||||
|
||||
aliasingMap := viper.GetStringMapString("services.aliases")
|
||||
if val, ok := aliasingMap[packet.Endpoint]; ok {
|
||||
packet.Endpoint = val
|
||||
}
|
||||
|
||||
service := directory.GetServiceInstanceByType(packet.Endpoint)
|
||||
if service == nil {
|
||||
_ = c.WriteMessage(mt, nex.WebSocketPackage{
|
||||
|
@ -116,7 +116,7 @@ type PushStreamRequest struct {
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
UserId *uint64 `protobuf:"varint,1,opt,name=user_id,json=userId,proto3,oneof" json:"user_id,omitempty"`
|
||||
ClientId *uint64 `protobuf:"varint,2,opt,name=client_id,json=clientId,proto3,oneof" json:"client_id,omitempty"`
|
||||
ClientId *string `protobuf:"bytes,2,opt,name=client_id,json=clientId,proto3,oneof" json:"client_id,omitempty"`
|
||||
Body []byte `protobuf:"bytes,3,opt,name=body,proto3" json:"body,omitempty"`
|
||||
}
|
||||
|
||||
@ -157,11 +157,11 @@ func (x *PushStreamRequest) GetUserId() uint64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *PushStreamRequest) GetClientId() uint64 {
|
||||
func (x *PushStreamRequest) GetClientId() string {
|
||||
if x != nil && x.ClientId != nil {
|
||||
return *x.ClientId
|
||||
}
|
||||
return 0
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *PushStreamRequest) GetBody() []byte {
|
||||
@ -177,7 +177,7 @@ type PushStreamBatchRequest struct {
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
UserId []uint64 `protobuf:"varint,1,rep,packed,name=user_id,json=userId,proto3" json:"user_id,omitempty"`
|
||||
ClientId []uint64 `protobuf:"varint,2,rep,packed,name=client_id,json=clientId,proto3" json:"client_id,omitempty"`
|
||||
ClientId []string `protobuf:"bytes,2,rep,name=client_id,json=clientId,proto3" json:"client_id,omitempty"`
|
||||
Body []byte `protobuf:"bytes,3,opt,name=body,proto3" json:"body,omitempty"`
|
||||
}
|
||||
|
||||
@ -218,7 +218,7 @@ func (x *PushStreamBatchRequest) GetUserId() []uint64 {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *PushStreamBatchRequest) GetClientId() []uint64 {
|
||||
func (x *PushStreamBatchRequest) GetClientId() []string {
|
||||
if x != nil {
|
||||
return x.ClientId
|
||||
}
|
||||
@ -240,7 +240,7 @@ type PushStreamResponse struct {
|
||||
IsAllSuccess bool `protobuf:"varint,1,opt,name=is_all_success,json=isAllSuccess,proto3" json:"is_all_success,omitempty"`
|
||||
AffectedCount int64 `protobuf:"varint,2,opt,name=affected_count,json=affectedCount,proto3" json:"affected_count,omitempty"`
|
||||
FailedCount int64 `protobuf:"varint,3,opt,name=failed_count,json=failedCount,proto3" json:"failed_count,omitempty"`
|
||||
SuccessList []uint64 `protobuf:"varint,4,rep,packed,name=success_list,json=successList,proto3" json:"success_list,omitempty"`
|
||||
SuccessList []string `protobuf:"bytes,4,rep,name=success_list,json=successList,proto3" json:"success_list,omitempty"`
|
||||
}
|
||||
|
||||
func (x *PushStreamResponse) Reset() {
|
||||
@ -294,7 +294,7 @@ func (x *PushStreamResponse) GetFailedCount() int64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *PushStreamResponse) GetSuccessList() []uint64 {
|
||||
func (x *PushStreamResponse) GetSuccessList() []string {
|
||||
if x != nil {
|
||||
return x.SuccessList
|
||||
}
|
||||
@ -308,7 +308,7 @@ type StreamEventRequest struct {
|
||||
|
||||
Event string `protobuf:"bytes,1,opt,name=event,proto3" json:"event,omitempty"`
|
||||
UserId uint64 `protobuf:"varint,2,opt,name=user_id,json=userId,proto3" json:"user_id,omitempty"`
|
||||
ClientId uint64 `protobuf:"varint,3,opt,name=client_id,json=clientId,proto3" json:"client_id,omitempty"`
|
||||
ClientId string `protobuf:"bytes,3,opt,name=client_id,json=clientId,proto3" json:"client_id,omitempty"`
|
||||
Payload []byte `protobuf:"bytes,4,opt,name=payload,proto3" json:"payload,omitempty"`
|
||||
}
|
||||
|
||||
@ -356,11 +356,11 @@ func (x *StreamEventRequest) GetUserId() uint64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *StreamEventRequest) GetClientId() uint64 {
|
||||
func (x *StreamEventRequest) GetClientId() string {
|
||||
if x != nil {
|
||||
return x.ClientId
|
||||
}
|
||||
return 0
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *StreamEventRequest) GetPayload() []byte {
|
||||
@ -420,7 +420,7 @@ var file_stream_proto_rawDesc = []byte{
|
||||
0x73, 0x68, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12,
|
||||
0x1c, 0x0a, 0x07, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x04,
|
||||
0x48, 0x00, 0x52, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x88, 0x01, 0x01, 0x12, 0x20, 0x0a,
|
||||
0x09, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x5f, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x04,
|
||||
0x09, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x5f, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09,
|
||||
0x48, 0x01, 0x52, 0x08, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x49, 0x64, 0x88, 0x01, 0x01, 0x12,
|
||||
0x12, 0x0a, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x62,
|
||||
0x6f, 0x64, 0x79, 0x42, 0x0a, 0x0a, 0x08, 0x5f, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x42,
|
||||
@ -429,7 +429,7 @@ var file_stream_proto_rawDesc = []byte{
|
||||
0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x17, 0x0a, 0x07, 0x75, 0x73, 0x65, 0x72, 0x5f,
|
||||
0x69, 0x64, 0x18, 0x01, 0x20, 0x03, 0x28, 0x04, 0x52, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64,
|
||||
0x12, 0x1b, 0x0a, 0x09, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x5f, 0x69, 0x64, 0x18, 0x02, 0x20,
|
||||
0x03, 0x28, 0x04, 0x52, 0x08, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x49, 0x64, 0x12, 0x12, 0x0a,
|
||||
0x03, 0x28, 0x09, 0x52, 0x08, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x49, 0x64, 0x12, 0x12, 0x0a,
|
||||
0x04, 0x62, 0x6f, 0x64, 0x79, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x62, 0x6f, 0x64,
|
||||
0x79, 0x22, 0xa7, 0x01, 0x0a, 0x12, 0x50, 0x75, 0x73, 0x68, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d,
|
||||
0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x24, 0x0a, 0x0e, 0x69, 0x73, 0x5f, 0x61,
|
||||
@ -440,14 +440,14 @@ var file_stream_proto_rawDesc = []byte{
|
||||
0x43, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x21, 0x0a, 0x0c, 0x66, 0x61, 0x69, 0x6c, 0x65, 0x64, 0x5f,
|
||||
0x63, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0b, 0x66, 0x61, 0x69,
|
||||
0x6c, 0x65, 0x64, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x21, 0x0a, 0x0c, 0x73, 0x75, 0x63, 0x63,
|
||||
0x65, 0x73, 0x73, 0x5f, 0x6c, 0x69, 0x73, 0x74, 0x18, 0x04, 0x20, 0x03, 0x28, 0x04, 0x52, 0x0b,
|
||||
0x65, 0x73, 0x73, 0x5f, 0x6c, 0x69, 0x73, 0x74, 0x18, 0x04, 0x20, 0x03, 0x28, 0x09, 0x52, 0x0b,
|
||||
0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x4c, 0x69, 0x73, 0x74, 0x22, 0x7a, 0x0a, 0x12, 0x53,
|
||||
0x74, 0x72, 0x65, 0x61, 0x6d, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73,
|
||||
0x74, 0x12, 0x14, 0x0a, 0x05, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09,
|
||||
0x52, 0x05, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x12, 0x17, 0x0a, 0x07, 0x75, 0x73, 0x65, 0x72, 0x5f,
|
||||
0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x04, 0x52, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64,
|
||||
0x12, 0x1b, 0x0a, 0x09, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x5f, 0x69, 0x64, 0x18, 0x03, 0x20,
|
||||
0x01, 0x28, 0x04, 0x52, 0x08, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x49, 0x64, 0x12, 0x18, 0x0a,
|
||||
0x01, 0x28, 0x09, 0x52, 0x08, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x49, 0x64, 0x12, 0x18, 0x0a,
|
||||
0x07, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07,
|
||||
0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x22, 0x15, 0x0a, 0x13, 0x53, 0x74, 0x72, 0x65, 0x61,
|
||||
0x6d, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x32, 0xfd,
|
||||
|
@ -20,13 +20,13 @@ message CountConnectionResponse {
|
||||
|
||||
message PushStreamRequest {
|
||||
optional uint64 user_id = 1;
|
||||
optional uint64 client_id = 2;
|
||||
optional string client_id = 2;
|
||||
bytes body = 3;
|
||||
}
|
||||
|
||||
message PushStreamBatchRequest {
|
||||
repeated uint64 user_id = 1;
|
||||
repeated uint64 client_id = 2;
|
||||
repeated string client_id = 2;
|
||||
bytes body = 3;
|
||||
}
|
||||
|
||||
@ -34,13 +34,13 @@ message PushStreamResponse {
|
||||
bool is_all_success = 1;
|
||||
int64 affected_count = 2;
|
||||
int64 failed_count = 3;
|
||||
repeated uint64 success_list = 4;
|
||||
repeated string success_list = 4;
|
||||
}
|
||||
|
||||
message StreamEventRequest {
|
||||
string event = 1;
|
||||
uint64 user_id = 2;
|
||||
uint64 client_id = 3;
|
||||
string client_id = 3;
|
||||
bytes payload = 4;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user