Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/weiwenchen2022/pubsub
Pubsub design pattern implements for Go
https://github.com/weiwenchen2022/pubsub
design-pattern go library pubsub
Last synced: about 1 month ago
JSON representation
Pubsub design pattern implements for Go
- Host: GitHub
- URL: https://github.com/weiwenchen2022/pubsub
- Owner: weiwenchen2022
- License: bsd-3-clause
- Created: 2023-07-25T08:10:09.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-07-26T15:21:57.000Z (over 1 year ago)
- Last Synced: 2024-04-21T17:17:09.018Z (8 months ago)
- Topics: design-pattern, go, library, pubsub
- Language: Go
- Homepage: https://godoc.org/github.com/weiwenchen2022/pubsub
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pubsub
package pubsub implements the pubsub design pattern.
## Install
```sh
go get github.com/weiwenchen2022/pubsub
```## Example
```go
package mainimport (
"fmt"
"strings"
"sync"
"time""github.com/weiwenchen2022/pubsub"
)func main() {
p := pubsub.NewPublisher(100*time.Millisecond, 10)
defer p.Close()protobuf, _ := p.SubscribeSubject(func(subject any) bool {
s, ok := subject.(string)
if !ok {
return false
}
return strings.Contains(s, "protobuf")
})
grpc, _ := p.SubscribeSubject(func(subject any) bool {
s, ok := subject.(string)
if !ok {
return false
}
return strings.Contains(s, "grpc")
})go func() {
p.Publish("world")
time.Sleep(100 * time.Millisecond)
p.Publish("https://grpc.io")
}()go func() {
p.Publish("hello")
time.Sleep(100 * time.Millisecond)
p.Publish("https://protobuf.dev")
}()var wg sync.WaitGroup
wg.Add(2)
go func() {
fmt.Println("protobuf subject:", <-protobuf)
wg.Done()
}()go func() {
fmt.Println("grpc subject:", <-grpc)
wg.Done()
}()wg.Wait()
// Unordered output:
// protobuf subject: https://protobuf.dev
// grpc subject: https://grpc.io
}
```## Reference
GoDoc: [https://godoc.org/github.com/weiwenchen2022/pubsub](https://godoc.org/github.com/weiwenchen2022/pubsub)