学习记录(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
}