Go 发布订阅模型

学习记录(Go语言高级编程)

package pubsub

import (
	"fmt"
	"runtime/debug"
	"sync"
	"time"
)

type (
	subscriber chan interface{} //订阅者为一个管道
	filterFunc func(v interface{}) bool
)

type Publisher struct {
	sync.RWMutex                           //读写锁
	buffer       int                       //订阅队列的缓存大小
	timeout      time.Duration             //发布超时时间
	subscribers  map[subscriber]filterFunc //订阅者信息
}

func CreatePublisher(publishTimeout time.Duration, buffer int) *Publisher {
	return &Publisher{
		buffer:      buffer,
		timeout:     publishTimeout,
		subscribers: make(map[subscriber]filterFunc),
	}
}

func (this *Publisher) Subscribe() chan interface{} {
	return this.SubscribeTopic(nil)
}

func (this *Publisher) SubscribeTopic(filter filterFunc) chan interface{} {
	ch := make(chan interface{}, 1)
	this.Lock()
	this.subscribers[ch] = filter
	this.Unlock()
	return ch
}

func (this *Publisher) Evict(sub chan interface{}) {
	this.Lock()
	defer this.Unlock()
	delete(this.subscribers, sub)
	close(sub)
}

func (this *Publisher) Publish(v interface{}) {
	this.Lock()
	defer this.Unlock()
	var wg sync.WaitGroup
	for sub, filter := range this.subscribers {
		wg.Add(1)
		go this.callFilter(sub, filter, v, &wg)
	}
	wg.Wait()
}

func (this *Publisher) callFilter(sub subscriber, filter filterFunc, v interface{}, wg *sync.WaitGroup) {
	defer func() {
		wg.Done()
		if err := recover(); err != nil {
			fmt.Printf("Publisher callback err: %s", err)
			fmt.Printf("Pubblisher err stack: %s", debug.Stack())
		}
	}()
	if filter != nil && !filter(v) {
		return
	}

	select {
	case sub <- v:
	case <-time.After(this.timeout):
	}
}

func (this *Publisher) Close() {
	this.Lock()
	defer this.Unlock()
	for sub := range this.subscribers {
		delete(this.subscribers, sub)
		close(sub)
	}
}
package main

import (
	"fmt"
	"os"
	"os/signal"
	"pubsub"
	"strings"
	"time"
)

func main() {
	p := pubsub.CreatePublisher(100*time.Millisecond, 10)
	defer p.Close()
	all := p.Subscribe()

	golang := p.SubscribeTopic(func(v interface{}) bool {
		if s, ok := v.(string); ok {
			return strings.Contains(s, "golang")
		}
		return false
	})

	p.Publish("hello, world")
	p.Publish("hello, golang!")

	go func() {
		for msg := range all {
			fmt.Println("all:", msg)
		}
	}()

	go func() {
		for msg := range golang {
			fmt.Println("golang:", msg)
		}
	}()

	sig := make(chan os.Signal, 1)
	signal.Notify(sig, os.Interrupt, os.Kill)
	<-sig
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值