Skip to content

go-cook/pubsub

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

核心功能来自 github.com/docker/docker/pkg/pubsub

一个简单的订阅

运行

package main

func main() {
    p := NewPublisher(100*time.Microsecond, 10)
    golang := p.SubscribeTopic(func(v interface{}) bool {
    if key, ok := v.(string); ok {
        if strings.HasPrefix(key, "golang:") {
            return true
        }
    }
        return false
    })
    
    docker := p.SubscribeTopic(func(v interface{}) bool {
    if key, ok := v.(string); ok {
        if strings.HasPrefix(key, "docker:") {
          return true
        }
    }
        return false
    })
    
    go p.Publish("abc")
    go p.Publish("golang: https://golang.org")
    go p.Publish("docker: https://www.docker.com")
    
    time.Sleep(time.Second * 2)
    
    go func() {
        fmt.Println("golang topic:", <-golang)
    }()
    
    go func() {
        fmt.Println("docker topic:", <-docker)
    }()
    
    time.Sleep(time.Second * 3)
    fmt.Println("end")
}

License

Apache License Version 2.0, http://www.apache.org/licenses/

Releases

No releases published

Packages

No packages published

Languages