mirror of
https://github.com/EchoVault/SugarDB.git
synced 2025-10-06 00:16:53 +08:00
Removed discord label from README.md. Updated SUBSCRIBE command handler to send psubscribe event to connection when subscribing to a pattern.
This commit is contained in:
@@ -2,10 +2,7 @@
|
||||
[](https://goreportcard.com/report/github.com/echovault/echovault)
|
||||
[](https://raw.githack.com/wiki/EchoVault/EchoVault/coverage.html)
|
||||
[]()
|
||||
<br/>
|
||||
[](https://www.gnu.org/licenses/gpl-3.0.en.html)
|
||||
<br/>
|
||||
[](https://discord.gg/vt45CKfF)
|
||||
|
||||
<hr/>
|
||||
|
||||
|
@@ -53,7 +53,7 @@ func handlePublish(ctx context.Context, cmd []string, server utils.Server, conn
|
||||
return []byte(utils.OkResponse), nil
|
||||
}
|
||||
|
||||
func handlePubSubChannels(ctx context.Context, cmd []string, server utils.Server, conn *net.Conn) ([]byte, error) {
|
||||
func handlePubSubChannels(_ context.Context, cmd []string, server utils.Server, conn *net.Conn) ([]byte, error) {
|
||||
if len(cmd) > 3 {
|
||||
return nil, errors.New(utils.WrongArgsResponse)
|
||||
}
|
||||
@@ -68,7 +68,7 @@ func handlePubSubChannels(ctx context.Context, cmd []string, server utils.Server
|
||||
pattern = cmd[2]
|
||||
}
|
||||
|
||||
return pubsub.Channels(ctx, pattern), nil
|
||||
return pubsub.Channels(pattern), nil
|
||||
}
|
||||
|
||||
func handlePubSubNumPat(ctx context.Context, cmd []string, server utils.Server, conn *net.Conn) ([]byte, error) {
|
||||
|
@@ -335,7 +335,7 @@ func Test_HandlePublish(t *testing.T) {
|
||||
}()
|
||||
// Verify all the responses for each pattern subscription
|
||||
for i := 0; i < len(patterns); i++ {
|
||||
verifyEvent(c, r, []string{"subscribe", patterns[i], fmt.Sprintf("%d", i+1)})
|
||||
verifyEvent(c, r, []string{"psubscribe", patterns[i], fmt.Sprintf("%d", i+1)})
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -26,6 +26,12 @@ func NewPubSub() *PubSub {
|
||||
|
||||
func (ps *PubSub) Subscribe(ctx context.Context, conn *net.Conn, channels []string, withPattern bool) {
|
||||
r := resp.NewConn(*conn)
|
||||
|
||||
action := "subscribe"
|
||||
if withPattern {
|
||||
action = "psubscribe"
|
||||
}
|
||||
|
||||
for i := 0; i < len(channels); i++ {
|
||||
// Check if channel with given name exists
|
||||
// If it does, subscribe the connection to the channel
|
||||
@@ -45,7 +51,7 @@ func (ps *PubSub) Subscribe(ctx context.Context, conn *net.Conn, channels []stri
|
||||
newChan.Start()
|
||||
if newChan.Subscribe(conn) {
|
||||
if err := r.WriteArray([]resp.Value{
|
||||
resp.StringValue("subscribe"),
|
||||
resp.StringValue(action),
|
||||
resp.StringValue(newChan.name),
|
||||
resp.IntegerValue(i + 1),
|
||||
}); err != nil {
|
||||
@@ -57,7 +63,7 @@ func (ps *PubSub) Subscribe(ctx context.Context, conn *net.Conn, channels []stri
|
||||
// Subscribe to existing channel
|
||||
if ps.channels[channelIdx].Subscribe(conn) {
|
||||
if err := r.WriteArray([]resp.Value{
|
||||
resp.StringValue("subscribe"),
|
||||
resp.StringValue(action),
|
||||
resp.StringValue(ps.channels[channelIdx].name),
|
||||
resp.IntegerValue(i + 1),
|
||||
}); err != nil {
|
||||
@@ -172,7 +178,7 @@ func (ps *PubSub) Publish(ctx context.Context, message string, channelName strin
|
||||
}
|
||||
}
|
||||
|
||||
func (ps *PubSub) Channels(ctx context.Context, pattern string) []byte {
|
||||
func (ps *PubSub) Channels(pattern string) []byte {
|
||||
var count int
|
||||
var res string
|
||||
|
||||
@@ -183,7 +189,6 @@ func (ps *PubSub) Channels(ctx context.Context, pattern string) []byte {
|
||||
count += 1
|
||||
}
|
||||
}
|
||||
|
||||
res = fmt.Sprintf("*%d\r\n%s", count, res)
|
||||
return []byte(res)
|
||||
}
|
||||
@@ -197,6 +202,7 @@ func (ps *PubSub) Channels(ctx context.Context, pattern string) []byte {
|
||||
count += 1
|
||||
continue
|
||||
}
|
||||
// Channel is not a pattern channel. Check if the channel name matches the provided glob pattern
|
||||
if g.Match(channel.name) && channel.IsActive() {
|
||||
res += fmt.Sprintf("$%d\r\n%s\r\n", len(channel.name), channel.name)
|
||||
count += 1
|
||||
|
Reference in New Issue
Block a user