paint-brush
मैंने Golang का उपयोग करके Redis-संगत Pub/Sub सिस्टम कैसे बनायाद्वारा@kelvinm
2,815 रीडिंग
2,815 रीडिंग

मैंने Golang का उपयोग करके Redis-संगत Pub/Sub सिस्टम कैसे बनाया

द्वारा Kelvin Clement M.18m2024/04/28
Read on Terminal Reader

बहुत लंबा; पढ़ने के लिए

पिछले एक साल से मैं EchoVault बना रहा हूँ, जो Golang इकोसिस्टम के लिए एक एम्बेड करने योग्य Redis विकल्प है। EchoVault का लक्ष्य Redis की अधिकांश विशेषताओं को दोहराना है, साथ ही एक एम्बेडेड इंटरफ़ेस और क्लाइंट-सर्वर इंटरफ़ेस प्रदान करना है जो RESP प्रोटोकॉल का उपयोग करके मौजूदा Redis क्लाइंट के साथ संगत है। EchoVault में लागू की गई सुविधाओं में से एक Pub/Sub सुविधा है। यह लेख इस बात का एक संक्षिप्त विवरण है कि लेखन के समय Pub/Sub मॉड्यूल को कैसे लागू किया गया है।
featured image - मैंने Golang का उपयोग करके Redis-संगत Pub/Sub सिस्टम कैसे बनाया
Kelvin Clement M. HackerNoon profile picture
0-item

पिछले साल से, मैं EchoVault का निर्माण कर रहा हूँ, जो Golang पारिस्थितिकी तंत्र के लिए एक एम्बेड करने योग्य Redis विकल्प है। EchoVault का लक्ष्य Redis की अधिकांश विशेषताओं को दोहराना है, जबकि एक एम्बेडेड इंटरफ़ेस और एक क्लाइंट-सर्वर इंटरफ़ेस दोनों प्रदान करना है जो RESP प्रोटोकॉल का उपयोग करके मौजूदा Redis क्लाइंट के साथ संगत है।


इकोवॉल्ट में लागू की गई सुविधाओं में से एक पब/सब सुविधा है। यह लेख इस बात का संक्षिप्त विवरण है कि लेखन के समय पब/सब मॉड्यूल को कैसे लागू किया गया है।

पब/सब क्या है?

पब/सब का मतलब है पब्लिश/सब्सक्राइब। यह पैटर्न उपभोक्ताओं को कुछ चैनलों की सदस्यता लेने की अनुमति देता है। निर्माता चैनलों पर संदेश प्रकाशित करते हैं, और उस चैनल की सदस्यता लेने वाले सभी उपभोक्ताओं को संदेश प्राप्त होता है।


हमारे संदर्भ में, हमें EchoVault एंबेड होने पर निर्दिष्ट चैनलों की सदस्यता लेने के लिए Go प्रक्रिया को सक्षम करना चाहिए। क्लाइंट-सर्वर मोड में चलने पर, हमें चैनलों की सदस्यता लेने के लिए क्लाइंट TCP कनेक्शन की अनुमति देनी चाहिए।


प्रकाशन पक्ष पर, एक गो प्रक्रिया को एम्बेडेड मोड में एक चैनल पर प्रकाशित करने में सक्षम होना चाहिए, और एक टीसीपी क्लाइंट कनेक्शन को क्लाइंट-सर्वर मोड में एक चैनल पर एक संदेश प्रकाशित करने में सक्षम होना चाहिए।

आवश्यकताएं

आरंभ करने से पहले, हमें इस कार्यान्वयन की आवश्यकताओं और स्कोर को निर्धारित करने की आवश्यकता है। लेखन के समय EchoVault Redis Pub/Sub में उपलब्ध सभी सुविधाओं को लागू नहीं करता है। हालाँकि, सबसे महत्वपूर्ण मुख्य सुविधाएँ लागू की गई हैं। यहाँ बताया गया है कि हमें EchoVault PubSub के साथ क्या करने में सक्षम होना चाहिए:


  1. एक TCP क्लाइंट को SUBSCRIBE channel [channel …] कमांड का उपयोग करके चैनलों की सूची की सदस्यता लेने की अनुमति दें। सर्वर को क्लाइंट कनेक्शन को सदस्यता की पुष्टि भेजनी चाहिए।


  2. एक एम्बेडेड इकोवॉल्ट इंस्टैंस को चैनलों की सूची की सदस्यता लेने में सक्षम होना चाहिए।


  3. PSUBSCRIBE pattern [pattern …] का उपयोग करके एक TCP क्लाइंट को एक पैटर्न की सदस्यता लेने की अनुमति दें, जहां पैटर्न एक ग्लोब स्ट्रिंग है जो क्लाइंट को पैटर्न को संतुष्ट करने वाले सभी चैनलों पर प्रकाशित संदेशों को प्राप्त करने की अनुमति देता है।


  4. एक एम्बेडेड इकोवॉल्ट इंस्टैंस को पैटर्न की सूची की सदस्यता लेने में सक्षम होना चाहिए।


  5. PUBLISH channel message कमांड का उपयोग करके TCP क्लाइंट को चैनल पर संदेश प्रकाशित करने की अनुमति दें।


  6. एम्बेडेड EchoVault इंस्टैंस से चैनल पर प्रकाशित करें.


  7. TCP क्लाइंट को क्रमशः UNSUBSCRIBE channel [channel …] और PUNSUBSCRIBE pattern [pattern …] कमांड का उपयोग करके चैनलों और पैटर्न से सदस्यता समाप्त करने की अनुमति दें।


  8. TCP क्लाइंट कनेक्शन को PUBSUB CHANNELS [pattern] कमांड का उपयोग करने की अनुमति दें ताकि दिए गए पैटर्न से मेल खाने वाले चैनलों वाली एक सरणी देखी जा सके। यदि कोई पैटर्न प्रदान नहीं किया गया है, तो सभी सक्रिय चैनल वापस कर दिए जाते हैं। सक्रिय चैनल एक या अधिक सब्सक्राइबर वाले चैनल होते हैं।


  9. TCP क्लाइंट को PUBSUB NUMPAT कमांड का उपयोग करने की अनुमति दें, ताकि क्लाइंट द्वारा वर्तमान में सब्सक्राइब किए गए पैटर्न की संख्या देखी जा सके।


  10. सक्रिय पैटर्न की संख्या देखने के लिए एक एम्बेडेड API प्रदान करें.


  11. टीसीपी क्लाइंट को PUBSUB NUMSUB [channel [channel ...]] कमांड का उपयोग करने की अनुमति दें ताकि प्रदान किए गए चैनल नाम और कितने क्लाइंट वर्तमान में चैनल के लिए सब्सक्राइब हैं, वाले सरणियों की एक सरणी को देख सकें।


  12. दिए गए चैनलों में ग्राहकों की संख्या देखने के लिए एक एम्बेडेड एपीआई प्रदान करें।

कार्यान्वयन

अब जब हमने आवश्यकताएँ निर्धारित कर ली हैं, तो चलिए कार्यान्वयन में उतरते हैं। उच्च स्तर पर, हमारे पास एक PubSub संरचना होगी जो सभी PubSub कार्यक्षमता प्रदान करती है। हमारे पास एक चैनल संरचना भी होगी जो चैनल और पैटर्न चैनल के लिए कार्यक्षमता प्रदान करती है।


इस अनुभाग के लिए, हम tidwall/resp पैकेज का उपयोग करेंगे। EchoVault उपयोगकर्ता चैनल सब्सक्राइबरों को RESP प्रतिक्रियाएँ भेजने के लिए इस पैकेज का उपयोग करते हैं। gobwas/glob पैकेज ग्लोब पैटर्न लॉजिक को संभालता है।

चैनल

सबसे पहले, हम Channel स्ट्रक्चर और उसके सभी तरीके बनाएंगे। हमारे पास दो प्रकार के चैनल होंगे: नियमित चैनल और पैटर्न चैनल।

नियमित चैनल ऐसे चैनल होते हैं जिनके नाम होते हैं और उनसे कोई पैटर्न जुड़ा नहीं होता। पैटर्न चैनल नाम के रूप में पैटर्न का उपयोग करेंगे, और चैनल के साथ एक संकलित ग्लोब पैटर्न जुड़ा होगा।


पैटर्न चैनलों का उपयोग पैटर्न की सदस्यता के लिए किया जाता है। अन्यथा, नियमित चैनलों का उपयोग किया जाता है।


Channel संरचना का आकार निम्नलिखित है:


 type Channel struct { name string // Channel name. This can be a glob pattern string. pattern glob.Glob // Compiled glob pattern. This is nil if the channel is not a pattern channel. subscribersRWMut sync.RWMutex // RWMutex to concurrency control when accessing channel subscribers. subscribers map[*net.Conn]*resp.Conn // Map containing the channel subscribers. messageChan *chan string // Messages published to this channel will be sent to this channel. }


हम एक नया चैनल इंस्टेंस बनाने के लिए विकल्प पैटर्न का उपयोग करेंगे। यहाँ दो विकल्प उपलब्ध हैं:


 // WithName option sets the channels name. func WithName(name string) func(channel *Channel) { return func(channel *Channel) { channel.name = name } } // WithPattern option sets the compiled glob pattern for the channel if it's a pattern channel. func WithPattern(pattern string) func(channel *Channel) { return func(channel *Channel) { channel.name = pattern channel.pattern = glob.MustCompile(pattern) } } func NewChannel(options ...func(channel *Channel)) *Channel { messageChan := make(chan string, 4096) // messageChan is buffered. This could be a configurable value. channel := &Channel{ name: "", pattern: nil, subscribersRWMut: sync.RWMutex{}, subscribers: make(map[*net.Conn]*resp.Conn), messageChan: &messageChan, } for _, option := range options { option(channel) } return channel }


चैनल स्ट्रक्चर के लिए पहली विधि Start विधि है। यह विधि एक गोरूटीन शुरू करती है जो अपने सभी सब्सक्राइबरों को प्रसारित करने के लिए messageChan पर संदेश सुनती है। यहाँ कार्यान्वयन है:


 func (ch *Channel) Start() { go func() { for { message := <-*ch.messageChan ch.subscribersRWMut.RLock() for _, conn := range ch.subscribers { go func(conn *resp.Conn) { if err := conn.WriteArray([]resp.Value{ resp.StringValue("message"), resp.StringValue(ch.name), resp.StringValue(message), }); err != nil { log.Println(err) } }(conn) } ch.subscribersRWMut.RUnlock() } }() }


गोरूटीन messageChan से अगला संदेश उठाता है, सब्सक्राइबर के लिए रीड-लॉक प्राप्त करता है, प्रत्येक सब्सक्राइबर को संदेश प्रसारित करता है, और फिर रीड-लॉक जारी करता है। जैसा कि आप देख सकते हैं, tidwall/resp पैकेज हमें क्लाइंट कनेक्शन पर RESP मान आसानी से भेजने की अनुमति देता है।


ज़्यादातर Redis क्लाइंट को उम्मीद है कि pub/sub मैसेज फ़ॉर्मेट इस तरह की एक सरणी होगी: [“message”, <channel name>, <message string>]. यही EchoVault द्वारा प्रसारित किया जाता है.


आप देख सकते हैं कि हम resp.Conn को संदेश भेज रहे हैं, net.Conn नहीं। हम resp.Conn उपयोग कर रहे हैं क्योंकि यह RESP मान लिखने के लिए सहायक विधियाँ प्रदान करता है, जैसे WriteArray । यह अनिवार्य रूप से net.Conn चारों ओर एक आवरण है जैसा कि हम Subscribe विधि में देखेंगे:


 func (ch *Channel) Subscribe(conn *net.Conn) bool { ch.subscribersRWMut.Lock() // Acquire write-lock because we'll be modifying the subscriber map. defer ch.subscribersRWMut.Unlock() // If the connection does not exist in the subscriber map, add it. if _, ok := ch.subscribers[conn]; !ok { ch.subscribers[conn] = resp.NewConn(*conn) } _, ok := ch.subscribers[conn] return ok }


Subscribe विधि के साथ-साथ हमें Unsubscribe विधि की भी आवश्यकता है:


 func (ch *Channel) Unsubscribe(conn *net.Conn) bool { ch.subscribersRWMut.Lock() defer ch.subscribersRWMut.Unlock() if _, ok := ch.subscribers[conn]; !ok { return false } delete(ch.subscribers, conn) return true }


यदि कनेक्शन पाया गया और सब्सक्राइबर मैप से हटा दिया गया तो Unsubscribe विधि true लौटाती है। अन्यथा, यह false लौटाती है। उपरोक्त विधियों के अलावा, Channel संरचना के लिए कुछ और सहायक विधियाँ भी हैं:


 func (ch *Channel) Name() string { return ch.name } func (ch *Channel) Pattern() glob.Glob { return ch.pattern } func (ch *Channel) Publish(message string) { *ch.messageChan <- message } // IsActive returns true when the channel has 1 or more subscribers. func (ch *Channel) IsActive() bool { ch.subscribersRWMut.RLock() defer ch.subscribersRWMut.RUnlock() active := len(ch.subscribers) > 0 return active } // NumSubs returns the number of subscribers for this channel. func (ch *Channel) NumSubs() int { ch.subscribersRWMut.RLock() defer ch.subscribersRWMut.RUnlock() n := len(ch.subscribers) return n } // Subscribers returns a copy of the subscriber map. func (ch *Channel) Subscribers() map[*net.Conn]*resp.Conn { ch.subscribersRWMut.RLock() defer ch.subscribersRWMut.RUnlock() subscribers := make(map[*net.Conn]*resp.Conn, len(ch.subscribers)) for k, v := range ch.subscribers { subscribers[k] = v } return subscribers }

पबसब

हम चैनलों के साथ बातचीत करने के लिए PubSub मॉड्यूल का उपयोग करेंगे। PubSub संरचना का आकार इस प्रकार है:


 type PubSub struct { channels []*Channel // Slice of references to channels channelsRWMut sync.RWMutex // RWMutex for concurrency controls when accessing channels } func NewPubSub() *PubSub { return &PubSub{ channels: []*Channel{}, channelsRWMut: sync.RWMutex{}, } }


पहली विधि Subscribe विधि है। यह विधि क्लाइंट को निर्दिष्ट चैनल(ओं) की सदस्यता प्रदान करती है।


 func (ps *PubSub) Subscribe(_ context.Context, conn *net.Conn, channels []string, withPattern bool) { ps.channelsRWMut.Lock() // Acquire write-lock as we may edit the slice of channels. defer ps.channelsRWMut.Unlock() r := resp.NewConn(*conn) // Wrap net.Conn connection with resp.Conn. action := "subscribe" if withPattern { action = "psubscribe" } // Loop through all the channels that the client has requested to subscribe to. for i := 0; i < len(channels); i++ { // Check if channel with given name exists // If it does, subscribe the connection to the channel // If it does not, create the channel and subscribe to it channelIdx := slices.IndexFunc(ps.channels, func(channel *Channel) bool { return channel.name == channels[i] }) if channelIdx == -1 { // If the channel does not exist, create new channel, start it, and subscribe to it. var newChan *Channel if withPattern { newChan = NewChannel(WithPattern(channels[i])) } else { newChan = NewChannel(WithName(channels[i])) } newChan.Start() if newChan.Subscribe(conn) { // Write string array to the client connection confirming the subscription. if err := r.WriteArray([]resp.Value{ resp.StringValue(action), resp.StringValue(newChan.name), resp.IntegerValue(i + 1), }); err != nil { log.Println(err) } } ps.channels = append(ps.channels, newChan) // Append the new channel to the list of channels. } else { // Subscribe to existing channel if ps.channels[channelIdx].Subscribe(conn) { // Write string array to the client connection confirming the subscription. if err := r.WriteArray([]resp.Value{ resp.StringValue(action), resp.StringValue(ps.channels[channelIdx].name), resp.IntegerValue(i + 1), }); err != nil { log.Println(err) } } } } }


किसी क्लाइंट को चैनल पर सब्सक्राइब करने के लिए, EchoVault SUBSCRIBE या PSUBSCRIBE कमांड को सुनता है। जब क्लाइंट से कमांड प्राप्त होता है, तो क्लाइंट का TCP कनेक्शन, चैनलों की सूची के साथ, Subscribe विधि को पास कर दिया जाता है। जब कोई क्लाइंट PSUBSCRIBE कमांड का उपयोग करके सब्सक्राइब करता है, तो withPatterns पैरामीटर true होगा, और action वैरिएबल का मान “psubscribe” पर सेट किया जाएगा।


Unsubscribe विधि क्लाइंट को चैनल या पैटर्न से अनसब्सक्राइब करने की अनुमति देती है, इस आधार पर कि उन्होंने SUBSCRIBE या PSUBSCRIBE उपयोग किया है। इसका कार्यान्वयन इस प्रकार है:


 func (ps *PubSub) Unsubscribe(_ context.Context, conn *net.Conn, channels []string, withPattern bool) []byte { ps.channelsRWMut.RLock() defer ps.channelsRWMut.RUnlock() action := "unsubscribe" if withPattern { action = "punsubscribe" } unsubscribed := make(map[int]string) // A map of all the channels/patterns successfully unsubscribed from. idx := 1 // idx holds the 1-based index of the channel/pattern unsubscribed from. if len(channels) <= 0 { if !withPattern { // If the channels slice is empty, and no pattern is provided // unsubscribe from all channels. for _, channel := range ps.channels { if channel.pattern != nil { // Skip pattern channels continue } if channel.Unsubscribe(conn) { unsubscribed[idx] = channel.name idx += 1 } } } else { // If the channels slice is empty, and pattern is provided // unsubscribe from all patterns. for _, channel := range ps.channels { if channel.pattern == nil { // Skip non-pattern channels continue } if channel.Unsubscribe(conn) { unsubscribed[idx] = channel.name idx += 1 } } } } // Unsubscribe from channels where the name exactly matches channel name. // If unsubscribing from a pattern, also unsubscribe from all channel whose // names exactly matches the pattern name. for _, channel := range ps.channels { // For each channel in PubSub for _, c := range channels { // For each channel name provided if channel.name == c && channel.Unsubscribe(conn) { unsubscribed[idx] = channel.name idx += 1 } } } // If withPattern is true, unsubscribe from channels where pattern matches pattern provided, // also unsubscribe from channels where the name matches the given pattern. if withPattern { for _, pattern := range channels { g := glob.MustCompile(pattern) for _, channel := range ps.channels { // If it's a pattern channel, directly compare the patterns if channel.pattern != nil && channel.name == pattern { if channel.Unsubscribe(conn) { unsubscribed[idx] = channel.name idx += 1 } continue } // If this is a regular channel, check if the channel name matches the pattern given if g.Match(channel.name) { if channel.Unsubscribe(conn) { unsubscribed[idx] = channel.name idx += 1 } } } } } // Construct a RESP response confirming the channels/patterns unsubscribed from. res := fmt.Sprintf("*%d\r\n", len(unsubscribed)) for key, value := range unsubscribed { res += fmt.Sprintf("*3\r\n+%s\r\n$%d\r\n%s\r\n:%d\r\n", action, len(value), value, key) } return []byte(res) }


जब क्लाइंट चैनल/पैटर्न से सदस्यता समाप्त करता है, तो उन्हें एक सरणी की सरणी युक्त प्रतिक्रिया प्राप्त होगी। प्रत्येक आंतरिक सरणी में क्रिया (सदस्यता समाप्त/सदस्यता रद्द), चैनल/पैटर्न नाम और सूचकांक शामिल होता है।


अगली विधि है प्रकाशित विधि। यह विधि संदेश और चैनल नाम को स्वीकार करती है और फिर संदेश के प्रकाशन को आंतरिक रूप से संभालती है।


 func (ps *PubSub) Publish(_ context.Context, message string, channelName string) { ps.channelsRWMut.RLock() defer ps.channelsRWMut.RUnlock() // Loop through all of the existing channels. for _, channel := range ps.channels { // If it's a regular channel, check if the channel name matches the name given. if channel.pattern == nil { if channel.name == channelName { channel.Publish(message) // Publish the message to the channel. } continue } // If it's a glob pattern channel, check if the provided channel name matches the pattern. if channel.pattern.Match(channelName) { channel.Publish(message) // Publish the message to the channel } } }


अगली विधि Channels विधि है, जो PUBSUB CHANNELS [pattern] कमांड को संभालती है:


 func (ps *PubSub) Channels(pattern string) []byte { ps.channelsRWMut.RLock() defer ps.channelsRWMut.RUnlock() var count int var res string // If pattern is an empty string, return all the active channels. if pattern == "" { for _, channel := range ps.channels { if channel.IsActive() { res += fmt.Sprintf("$%d\r\n%s\r\n", len(channel.name), channel.name) count += 1 } } res = fmt.Sprintf("*%d\r\n%s", count, res) return []byte(res) } g := glob.MustCompile(pattern) for _, channel := range ps.channels { // If channel is a pattern channel, then directly compare the channel name to pattern. if channel.pattern != nil && channel.name == pattern && channel.IsActive() { res += fmt.Sprintf("$%d\r\n%s\r\n", len(channel.name), channel.name) 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 } } // Return a RESP array containing all the active channel names. return []byte(fmt.Sprintf("*%d\r\n%s", count, res)) }


PUBSUB NUMPAT और को संभालने के लिए निम्नलिखित 2 विधियाँ NumPat और NumSub विधियाँ हैं

PUBSUB NUMSUB [channel [channel …]] क्रमशः कमांड।


 func (ps *PubSub) NumPat() int { ps.channelsRWMut.RLock() defer ps.channelsRWMut.RUnlock() var count int for _, channel := range ps.channels { if channel.pattern != nil && channel.IsActive() { count += 1 } } return count } func (ps *PubSub) NumSub(channels []string) []byte { ps.channelsRWMut.RLock() defer ps.channelsRWMut.RUnlock() res := fmt.Sprintf("*%d\r\n", len(channels)) for _, channel := range channels { // If it's a pattern channel, skip it chanIdx := slices.IndexFunc(ps.channels, func(c *Channel) bool { return c.name == channel }) if chanIdx == -1 { res += fmt.Sprintf("*2\r\n$%d\r\n%s\r\n:0\r\n", len(channel), channel) continue } res += fmt.Sprintf("*2\r\n$%d\r\n%s\r\n:%d\r\n", len(channel), channel, ps.channels[chanIdx].NumSubs()) } return []byte(res) }


एम्बेडेड एपीआई

पबसब मॉड्यूल को उम्मीद है कि क्लाइंट को चैनल की सदस्यता देने के लिए कनेक्शन पास किया जाएगा। हालाँकि, जब इकोवॉल्ट एम्बेड किया जाता है, तो क्लाइंट के लिए कोई TCP कनेक्शन नहीं होता है जिसका उपयोग सदस्यता के लिए किया जा सके। इससे बचने के लिए, हम कनेक्शन के दोनों सिरों को प्राप्त करने के लिए net.Pipe उपयोग करते हैं।


कनेक्शन का एक सिरा कमांड हैंडलर को दिया जाता है, जो इसका उपयोग निर्दिष्ट चैनल की सदस्यता लेने के लिए करता है। कनेक्शन का दूसरा सिरा लौटाए गए ReadPubSubMessage फ़ंक्शन में उपयोग किया जाता है।


 type conn struct { readConn *net.Conn writeConn *net.Conn } var connections map[string]conn // ReadPubSubMessage is returned by the SUBSCRIBE and PSUBSCRIBE functions. // // This function is lazy, therefore it needs to be invoked in order to read the next message. // When the message is read, the function returns a string slice with 3 elements. // Index 0 holds the event type which in this case will be "message". Index 1 holds the channel name. // Index 2 holds the actual message. type ReadPubSubMessage func() []string // SUBSCRIBE subscribes the caller to the list of provided channels. // // Parameters: // // `tag` - string - The tag used to identify this subscription instance. // // `channels` - ...string - The list of channels to subscribe to. // // Returns: ReadPubSubMessage function which reads the next message sent to the subscription instance. // This function is blocking. func (server *EchoVault) SUBSCRIBE(tag string, channels ...string) ReadPubSubMessage { // Initialize connection tracker if calling subscribe for the first time if connections == nil { connections = make(map[string]conn) } // If connection with this name does not exist, create new connection it var readConn net.Conn var writeConn net.Conn if _, ok := connections[tag]; !ok { readConn, writeConn = net.Pipe() connections[tag] = conn{ readConn: &readConn, writeConn: &writeConn, } } // Subscribe connection to the provided channels cmd := append([]string{"SUBSCRIBE"}, channels...) go func() { _, _ = server.handleCommand(server.context, internal.EncodeCommand(cmd), connections[tag].writeConn, false) }() return func() []string { r := resp.NewConn(readConn) v, _, _ := r.ReadValue() res := make([]string, len(v.Array())) for i := 0; i < len(res); i++ { res[i] = v.Array()[i].String() } return res } } // UNSUBSCRIBE unsubscribes the caller from the given channels. // // Parameters: // // `tag` - string - The tag used to identify this subscription instance. // // `channels` - ...string - The list of channels to unsubscribe from. func (server *EchoVault) UNSUBSCRIBE(tag string, channels ...string) { if connections == nil { return } if _, ok := connections[tag]; !ok { return } cmd := append([]string{"UNSUBSCRIBE"}, channels...) _, _ = server.handleCommand(server.context, internal.EncodeCommand(cmd), connections[tag].writeConn, false) } // PSUBSCRIBE subscribes the caller to the list of provided glob patterns. // // Parameters: // // `tag` - string - The tag used to identify this subscription instance. // // `patterns` - ...string - The list of glob patterns to subscribe to. // // Returns: ReadPubSubMessage function which reads the next message sent to the subscription instance. // This function is blocking. func (server *EchoVault) PSUBSCRIBE(tag string, patterns ...string) ReadPubSubMessage { // Initialize connection tracker if calling subscribe for the first time if connections == nil { connections = make(map[string]conn) } // If connection with this name does not exist, create new connection it var readConn net.Conn var writeConn net.Conn if _, ok := connections[tag]; !ok { readConn, writeConn = net.Pipe() connections[tag] = conn{ readConn: &readConn, writeConn: &writeConn, } } // Subscribe connection to the provided channels cmd := append([]string{"PSUBSCRIBE"}, patterns...) go func() { _, _ = server.handleCommand(server.context, internal.EncodeCommand(cmd), connections[tag].writeConn, false) }() return func() []string { r := resp.NewConn(readConn) v, _, _ := r.ReadValue() res := make([]string, len(v.Array())) for i := 0; i < len(res); i++ { res[i] = v.Array()[i].String() } return res } } // PUNSUBSCRIBE unsubscribes the caller from the given glob patterns. // // Parameters: // // `tag` - string - The tag used to identify this subscription instance. // // `patterns` - ...string - The list of glob patterns to unsubscribe from. func (server *EchoVault) PUNSUBSCRIBE(tag string, patterns ...string) { if connections == nil { return } if _, ok := connections[tag]; !ok { return } cmd := append([]string{"PUNSUBSCRIBE"}, patterns...) _, _ = server.handleCommand(server.context, internal.EncodeCommand(cmd), connections[tag].writeConn, false) }


SUBSCRIBE और PUBLISH API का उपयोग करने का एक उदाहरण यहां दिया गया है:


 // Subscribe to multiple EchoVault channels. readMessage := server.SUBSCRIBE("subscriber1", "channel_1", "channel_2", "channel_3") wg.Add(1) go func() { wg.Done() for { message := readMessage() fmt.Printf("EVENT: %s, CHANNEL: %s, MESSAGE: %s\n", message[0], message[1], message[2]) } }() wg.Wait() wg.Add(1) go func() { for i := 1; i <= 3; i++ { // Simulating delay. <-time.After(1 * time.Second) // Publish message to each EchoVault channel. _, _ = server.PUBLISH(fmt.Sprintf("channel_%d", i), "Hello!") } wg.Done() }() wg.Wait()

निष्कर्ष

यह लेख EchoVault में Pub/Sub कार्यान्वयन पर एक संक्षिप्त विवरण था। बेशक, यहाँ साझा किए गए कोड के इर्द-गिर्द बहुत सारे संदर्भ हैं जो एक लेख में समाहित नहीं हो सकते। यदि आप संदर्भ में रुचि रखते हैं, तो EchoVault के GitHub रिपॉजिटरी को देखें और देखें कि हम क्या बना रहे हैं। यदि आपको जो दिख रहा है वह पसंद है, तो एक Github स्टार की बहुत सराहना की जाएगी!