Skip to content

Commit

Permalink
refactor: unify Redis-related types and reduce duplication
Browse files Browse the repository at this point in the history
- Removed redundant Redis-related types (Packet, Request, Response) from multiple files
- Centralized definitions of RedisPacket, RedisRequest, and RedisResponse in the `types` package
  • Loading branch information
zishang520 committed Oct 11, 2024
1 parent f59e0a7 commit 883b793
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 46 deletions.
26 changes: 4 additions & 22 deletions adapter/redis-adapter-type.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,15 @@ import (

"github.com/zishang520/engine.io/v2/types"
"github.com/zishang520/engine.io/v2/utils"
"github.com/zishang520/socket.io-go-parser/v2/parser"
_types "github.com/zishang520/socket.io-go-redis/types"
"github.com/zishang520/socket.io/v2/adapter"
"github.com/zishang520/socket.io/v2/socket"
)

type (
Request struct {
Type adapter.MessageType `json:"type,omitempty" msgpack:"type,omitempty"`
RequestId string `json:"requestId,omitempty" msgpack:"requestId,omitempty"`
Rooms []socket.Room `json:"rooms,omitempty" msgpack:"rooms,omitempty"`
Opts *adapter.PacketOptions `json:"opts,omitempty" msgpack:"opts,omitempty"`
Sid socket.SocketId `json:"sid,omitempty" msgpack:"sid,omitempty"`
Room socket.Room `json:"room,omitempty" msgpack:"room,omitempty"`
Close bool `json:"close,omitempty" msgpack:"close,omitempty"`
Uid adapter.ServerId `json:"uid,omitempty" msgpack:"uid,omitempty"`
Data []any `json:"data,omitempty" msgpack:"data,omitempty"`
Packet *parser.Packet `json:"packet,omitempty" msgpack:"packet,omitempty"`
}
Packet = _types.RedisPacket

Request = _types.RedisRequest

RedisRequest struct {
Type adapter.MessageType
Expand All @@ -37,15 +27,7 @@ type (
Responses *types.Slice[any]
}

Response struct {
Type adapter.MessageType `json:"type,omitempty" msgpack:"type,omitempty"`
RequestId string `json:"requestId,omitempty" msgpack:"requestId,omitempty"`
Rooms []socket.Room `json:"rooms,omitempty" msgpack:"rooms,omitempty"`
Sockets []*adapter.SocketResponse `json:"sockets,omitempty" msgpack:"sockets,omitempty"`
Data []any `json:"data,omitempty" msgpack:"data,omitempty"`
ClientCount uint64 `json:"clientcount,omitempty" msgpack:"clientcount,omitempty"`
Packet []any `json:"packet,omitempty" msgpack:"packet,omitempty"`
}
Response = _types.RedisResponse

AckRequest = adapter.ClusterAckRequest

Expand Down
4 changes: 2 additions & 2 deletions adapter/redis-adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (r *redisAdapter) onMessage(pattern string, channel string, msg []byte) {
return
}

var packet *_types.Packet
var packet *Packet
if err := r.parser.Decode(msg, &packet); err != nil {
redis_log.Debug("error decoding message: %v", err)
return
Expand Down Expand Up @@ -581,7 +581,7 @@ func (r *redisAdapter) Broadcast(packet *parser.Packet, opts *socket.BroadcastOp
onlyLocal := opts != nil && opts.Flags != nil && opts.Flags.Local

if !onlyLocal {
if msg, err := r.parser.Encode(&_types.Packet{
if msg, err := r.parser.Encode(&Packet{
Uid: r.Uid(),
Packet: packet,
Opts: adapter.EncodeOptions(opts),
Expand Down
8 changes: 4 additions & 4 deletions emitter/broadcast-operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (b *BroadcastOperator) Emit(ev string, args ...any) error {
Flags: b.flags,
}

msg, err := b.broadcastOptions.Parser.Encode(&_types.Packet{
msg, err := b.broadcastOptions.Parser.Encode(&Packet{
Uid: UID,
Packet: packet,
Opts: opts,
Expand All @@ -150,7 +150,7 @@ func (b *BroadcastOperator) Emit(ev string, args ...any) error {

// Makes the matching socket instances join the specified rooms
func (b *BroadcastOperator) SocketsJoin(rooms ...socket.Room) error {
request, err := json.Marshal(&EmitMessage{
request, err := json.Marshal(&Request{
Type: _types.REMOTE_JOIN,
Opts: &adapter.PacketOptions{
Rooms: b.rooms.Keys(),
Expand All @@ -167,7 +167,7 @@ func (b *BroadcastOperator) SocketsJoin(rooms ...socket.Room) error {

// Makes the matching socket instances leave the specified rooms
func (b *BroadcastOperator) SocketsLeave(rooms ...socket.Room) error {
request, err := json.Marshal(&EmitMessage{
request, err := json.Marshal(&Request{
Type: _types.REMOTE_LEAVE,
Opts: &adapter.PacketOptions{
Rooms: b.rooms.Keys(),
Expand All @@ -184,7 +184,7 @@ func (b *BroadcastOperator) SocketsLeave(rooms ...socket.Room) error {

// Makes the matching socket instances disconnect
func (b *BroadcastOperator) DisconnectSockets(state bool) error {
request, err := json.Marshal(&EmitMessage{
request, err := json.Marshal(&Request{
Type: _types.REMOTE_DISCONNECT,
Opts: &adapter.PacketOptions{
Rooms: b.rooms.Keys(),
Expand Down
17 changes: 4 additions & 13 deletions emitter/emitter-type.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package emitter

import (
"github.com/zishang520/socket.io-go-redis/types"
"github.com/zishang520/socket.io/v2/adapter"
"github.com/zishang520/socket.io/v2/socket"
)

type (
Expand All @@ -14,16 +12,9 @@ type (
Parser types.Parser
}

EmitMessage struct {
Type adapter.MessageType `json:"type,omitempty" msgpack:"type,omitempty"`
Opts *adapter.PacketOptions `json:"opts,omitempty" msgpack:"opts,omitempty"`
Close bool `json:"close,omitempty" msgpack:"close,omitempty"`
Rooms []socket.Room `json:"rooms,omitempty" msgpack:"rooms,omitempty"`
}
Packet = types.RedisPacket

ServerSideEmitMessage struct {
Uid adapter.ServerId `json:"uid,omitempty" msgpack:"uid,omitempty"`
Type adapter.MessageType `json:"type,omitempty" msgpack:"type,omitempty"`
Data []any `json:"data,omitempty" msgpack:"data,omitempty"`
}
Request = types.RedisRequest

Response = types.RedisResponse
)
2 changes: 1 addition & 1 deletion emitter/emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (e *Emitter) ServerSideEmit(args ...any) error {
return errors.New("Acknowledgements are not supported")
}
}
request, err := json.Marshal(&ServerSideEmitMessage{
request, err := json.Marshal(&Request{
Uid: UID,
Type: types.SERVER_SIDE_EMIT,
Data: args,
Expand Down
6 changes: 3 additions & 3 deletions emitter/emitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestEmitter(t *testing.T) {
redisClient := types.NewRedisClient(context.TODO(), redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Username: "",
Password: "",
Password: "root",
DB: 0,
}))

Expand Down Expand Up @@ -101,7 +101,7 @@ func TestBroadcastOperator(t *testing.T) {
redisClient := types.NewRedisClient(context.TODO(), redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Username: "",
Password: "",
Password: "root",
DB: 0,
}))

Expand All @@ -114,7 +114,7 @@ func TestBroadcastOperator(t *testing.T) {

t.Run("Emit", func(t *testing.T) {
if err := b.Emit("test", "data", "data"); err != nil {
t.Fatal(`emit.Emit() value must be nil`)
t.Fatalf(`emit.Emit() value must be nil: %v`, err)
}
})

Expand Down
26 changes: 25 additions & 1 deletion types/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,39 @@ package types
import (
"github.com/zishang520/socket.io-go-parser/v2/parser"
"github.com/zishang520/socket.io/v2/adapter"
"github.com/zishang520/socket.io/v2/socket"
)

type (
Packet struct {
RedisPacket struct {
Uid adapter.ServerId `json:"uid,omitempty" msgpack:"uid,omitempty"`
Packet *parser.Packet `json:"packet,omitempty" msgpack:"packet,omitempty"`
Opts *adapter.PacketOptions `json:"opts,omitempty" msgpack:"opts,omitempty"`
}

RedisRequest struct {
Type adapter.MessageType `json:"type,omitempty" msgpack:"type,omitempty"`
RequestId string `json:"requestId,omitempty" msgpack:"requestId,omitempty"`
Rooms []socket.Room `json:"rooms,omitempty" msgpack:"rooms,omitempty"`
Opts *adapter.PacketOptions `json:"opts,omitempty" msgpack:"opts,omitempty"`
Sid socket.SocketId `json:"sid,omitempty" msgpack:"sid,omitempty"`
Room socket.Room `json:"room,omitempty" msgpack:"room,omitempty"`
Close bool `json:"close,omitempty" msgpack:"close,omitempty"`
Uid adapter.ServerId `json:"uid,omitempty" msgpack:"uid,omitempty"`
Data []any `json:"data,omitempty" msgpack:"data,omitempty"`
Packet *parser.Packet `json:"packet,omitempty" msgpack:"packet,omitempty"`
}

RedisResponse struct {
Type adapter.MessageType `json:"type,omitempty" msgpack:"type,omitempty"`
RequestId string `json:"requestId,omitempty" msgpack:"requestId,omitempty"`
Rooms []socket.Room `json:"rooms,omitempty" msgpack:"rooms,omitempty"`
Sockets []*adapter.SocketResponse `json:"sockets,omitempty" msgpack:"sockets,omitempty"`
Data []any `json:"data,omitempty" msgpack:"data,omitempty"`
ClientCount uint64 `json:"clientcount,omitempty" msgpack:"clientcount,omitempty"`
Packet []any `json:"packet,omitempty" msgpack:"packet,omitempty"`
}

Parser interface {
Encode(any) ([]byte, error)
Decode([]byte, any) error
Expand Down

0 comments on commit 883b793

Please sign in to comment.