├── go.mod ├── .gitignore ├── codecov.yml ├── message_test.go ├── .github └── workflows │ ├── ci.yml │ └── pull_request.yml ├── go.sum ├── message.go ├── CONTRIBUTING.md ├── hub_example_test.go ├── Makefile ├── hub_benchmark_test.go ├── throughput_test.go ├── subscriber.go ├── matching.go ├── hub.go ├── utils_test.go ├── CODE_OF_CONDUCT.md ├── .golangci.yml ├── hub_test.go ├── README.md ├── matching_cstrie_test.go ├── LICENSE └── matching_cstrie.go /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/leandro-lugaresi/hub 2 | 3 | go 1.13 4 | 5 | require github.com/stretchr/testify v1.4.0 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | *.exe 6 | *.dll 7 | *.prof 8 | debug 9 | 10 | 11 | # Folders 12 | _obj 13 | _test 14 | dist 15 | 16 | *.cgo1.go 17 | *.cgo2.c 18 | _cgo_defun.c 19 | _cgo_gotypes.go 20 | _cgo_export.* 21 | 22 | _testmain.go 23 | 24 | 25 | # Test binary, build with `go test -c` 26 | *.test 27 | 28 | # Output of the go coverage tool, specifically when used with LiteIDE 29 | *.out 30 | coverage.txt 31 | 32 | # ignored until version 1.0 33 | vendor 34 | 35 | -------------------------------------------------------------------------------- /codecov.yml: -------------------------------------------------------------------------------- 1 | codecov: 2 | notify: 3 | require_ci_to_pass: no 4 | 5 | coverage: 6 | precision: 2 7 | round: down 8 | range: "70...100" 9 | 10 | status: 11 | project: 12 | default: 13 | # basic 14 | target: auto 15 | threshold: 5 16 | base: auto 17 | patch: yes 18 | 19 | parsers: 20 | gcov: 21 | branch_detection: 22 | conditional: yes 23 | loop: yes 24 | method: no 25 | macro: no 26 | 27 | comment: 28 | layout: "reach, diff, flags, files, footer" 29 | behavior: default 30 | require_changes: no 31 | 32 | ignore: 33 | - "**/*_test.go" 34 | -------------------------------------------------------------------------------- /message_test.go: -------------------------------------------------------------------------------- 1 | package hub 2 | 3 | import "testing" 4 | 5 | func TestFields_String(t *testing.T) { 6 | tests := []struct { 7 | name string 8 | f Fields 9 | want string 10 | }{ 11 | {"empty fields", Fields{}, "Fields()"}, 12 | {"one field", Fields{"foo": "bar"}, "Fields( [foo]bar )"}, 13 | {"two fields", Fields{"foo": "bar", "baz": true}, "Fields( [baz]true, [foo]bar )"}, 14 | } 15 | for _, tt := range tests { 16 | tt := tt 17 | t.Run(tt.name, func(t *testing.T) { 18 | if got := tt.f.String(); got != tt.want { 19 | t.Errorf("Fields.String() = %v, want %v", got, tt.want) 20 | } 21 | }) 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: Pull Request CI Check 2 | on: [push, pull_request] 3 | jobs: 4 | build: 5 | name: Run Tests 6 | runs-on: ubuntu-latest 7 | steps: 8 | - name: Set up Go 1.15 9 | uses: actions/setup-go@v1 10 | with: 11 | go-version: 1.15 12 | id: go 13 | 14 | - name: Go env 15 | run: go env 16 | 17 | - name: Check out code into the Go module directory 18 | uses: actions/checkout@v1 19 | 20 | - name: Setup 21 | run: make setup 22 | 23 | - name: Tests 24 | run: make test 25 | 26 | - name: Send coverage 27 | uses: codecov/codecov-action@v1 28 | with: 29 | file: ./coverage.txt 30 | flags: unittests 31 | name: codecov-umbrella 32 | fail_ci_if_error: false 33 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= 2 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 3 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 4 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 5 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 6 | github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= 7 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 8 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 9 | gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= 10 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 11 | -------------------------------------------------------------------------------- /message.go: -------------------------------------------------------------------------------- 1 | package hub 2 | 3 | import ( 4 | "fmt" 5 | "sort" 6 | "strings" 7 | ) 8 | 9 | type ( 10 | // Fields is a [key]value storage for Messages values. 11 | Fields map[string]interface{} 12 | 13 | // Message represent some message/event passed into the hub 14 | // It also contain some helper functions to convert the fields to primitive types. 15 | Message struct { 16 | Name string 17 | Body []byte 18 | Fields Fields 19 | } 20 | ) 21 | 22 | // Topic return the message topic used when the message was sended. 23 | func (m *Message) Topic() string { 24 | return m.Name 25 | } 26 | 27 | func (f Fields) String() string { 28 | if len(f) == 0 { 29 | return "Fields()" 30 | } 31 | 32 | fields := make([]string, 0, len(f)) 33 | for k, v := range f { 34 | fields = append(fields, fmt.Sprintf("[%s]%v", k, v)) 35 | } 36 | 37 | sort.Strings(fields) 38 | 39 | return "Fields( " + strings.Join(fields, ", ") + " )" 40 | } 41 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | By participating to this project, you agree to abide our [code of 4 | conduct](/CODE_OF_CONDUCT.md). 5 | 6 | ## Setup your machine 7 | 8 | `hub` is written in [Go](https://golang.org/). 9 | 10 | Prerequisites are: 11 | 12 | - Build: 13 | - `make` 14 | - [Go 1.9+](http://golang.org/doc/install) 15 | 16 | Clone `hub` from source into some path: 17 | 18 | ```sh 19 | git clone git@github.com:leandro-lugaresi/hub.git 20 | cd hub 21 | ``` 22 | 23 | If you created a fork clone your fork and add my repository as a upstream remote: 24 | 25 | ```sh 26 | git clone git@github.com:{your-name}/hub.git 27 | cd hub 28 | git remote add upstream git@github.com:leandro-lugaresi/hub.git 29 | ``` 30 | 31 | ### Install 32 | 33 | Install the build and lint dependencies: 34 | 35 | ```sh 36 | $ make setup 37 | ``` 38 | 39 | A good way of making sure everything is all right is running the test suite: 40 | 41 | ```sh 42 | $ make test 43 | ``` 44 | 45 | ## Test your change 46 | 47 | When you are satisfied with the changes, we suggest you run: 48 | 49 | ```sh 50 | $ make ci 51 | ``` 52 | 53 | Which runs all the linters and tests. 54 | 55 | ## Submit a pull request 56 | 57 | Push your branch to your `example` fork and open a pull request against the 58 | main branch. 59 | -------------------------------------------------------------------------------- /hub_example_test.go: -------------------------------------------------------------------------------- 1 | package hub_test 2 | 3 | import ( 4 | "fmt" 5 | "sync" 6 | 7 | "github.com/leandro-lugaresi/hub" 8 | ) 9 | 10 | func ExampleHub() { 11 | h := hub.New() 12 | var wg sync.WaitGroup 13 | // the cap param is used to create one buffered channel with cap = 10 14 | // If you wan an unbuferred channel use the 0 cap 15 | sub := h.Subscribe(10, "account.login.*", "account.changepassword.*") 16 | 17 | wg.Add(1) 18 | 19 | go func(s hub.Subscription) { 20 | for msg := range s.Receiver { 21 | fmt.Printf("receive msg with topic %s and id %d\n", msg.Name, msg.Fields["id"]) 22 | } 23 | 24 | wg.Done() 25 | }(sub) 26 | 27 | h.Publish(hub.Message{ 28 | Name: "account.login.failed", 29 | Fields: hub.Fields{"id": 123}, 30 | }) 31 | h.Publish(hub.Message{ 32 | Name: "account.changepassword.failed", 33 | Fields: hub.Fields{"id": 456}, 34 | }) 35 | h.Publish(hub.Message{ 36 | Name: "account.login.success", 37 | Fields: hub.Fields{"id": 123}, 38 | }) 39 | // message not routed to this subscriber 40 | h.Publish(hub.Message{ 41 | Name: "account.foo.failed", 42 | Fields: hub.Fields{"id": 789}, 43 | }) 44 | 45 | // close all the subscribers 46 | h.Close() 47 | // wait until finish all the messages on buffer 48 | wg.Wait() 49 | 50 | // Output: 51 | // receive msg with topic account.login.failed and id 123 52 | // receive msg with topic account.changepassword.failed and id 456 53 | // receive msg with topic account.login.success and id 123 54 | } 55 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | SOURCE_FILES?=$$(go list ./... | grep -v /vendor/) 2 | TEST_PATTERN?=./... 3 | TEST_OPTIONS?=-race 4 | 5 | setup: ## Install all the build and lint dependencies 6 | sudo curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $$(go env GOPATH)/bin v1.22.2 7 | GO111MODULE=off go get github.com/mfridman/tparse 8 | GO111MODULE=off go get -u golang.org/x/tools/cmd/cover 9 | go get -v -t ./... 10 | 11 | test: ## Run all the tests 12 | go test $(TEST_OPTIONS) -covermode=atomic -coverprofile=coverage.txt -timeout=1m -cover -json $(SOURCE_FILES) | $$(go env GOPATH)/bin/tparse -all 13 | 14 | throughput: ## Run the throughput tests 15 | go test -v -timeout 60s github.com/leandro-lugaresi/hub -run ^TestThroughput -args -throughput 16 | 17 | bench: ## Run the benchmark tests 18 | go test -bench=. $(TEST_PATTERN) 19 | 20 | cover: test ## Run all the tests and opens the coverage report 21 | go tool cover -html=coverage.txt 22 | 23 | fmt: ## gofmt and goimports all go files 24 | find . -name '*.go' -not -wholename './vendor/*' | while read -r file; do gofmt -w -s "$$file"; goimports -w "$$file"; done 25 | 26 | lint: ## Run all the linters 27 | $$(go env GOPATH)/bin/golangci-lint run 28 | 29 | ci: lint test ## Run all the tests and code checks 30 | 31 | # Absolutely awesome: http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html 32 | help: 33 | @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' 34 | 35 | .DEFAULT_GOAL := help 36 | -------------------------------------------------------------------------------- /hub_benchmark_test.go: -------------------------------------------------------------------------------- 1 | package hub 2 | 3 | import ( 4 | "math/rand" 5 | "strconv" 6 | "sync" 7 | "testing" 8 | ) 9 | 10 | func BenchmarkPublishOnNonBlockingSubscribers(b *testing.B) { 11 | runBenchmark(b, 100, 4, 30, false) 12 | } 13 | 14 | func BenchmarkPublishOnBlockingSubscribers(b *testing.B) { 15 | runBenchmark(b, 100, 4, 30, true) 16 | } 17 | 18 | func runBenchmark(b *testing.B, numItems, numThreads, numSubscribers int, blocking bool) { 19 | h := New() 20 | subs := createSubscribers(h, numSubscribers, blocking) 21 | itemsToInsert := generateTopics(numThreads, numItems) 22 | var wgPub, wgSub sync.WaitGroup 23 | 24 | wgSub.Add(len(subs)) 25 | processSubscriptionsForBench(subs, &wgSub) 26 | b.ResetTimer() 27 | 28 | for i := 0; i < b.N; i++ { 29 | wgPub.Add(numThreads) 30 | 31 | for j := 0; j < numThreads; j++ { 32 | go func(j int) { 33 | for _, key := range itemsToInsert[j] { 34 | h.Publish(Message{Name: key}) 35 | } 36 | 37 | wgPub.Done() 38 | }(j) 39 | } 40 | wgPub.Wait() 41 | } 42 | h.Close() 43 | wgSub.Wait() 44 | b.StopTimer() 45 | } 46 | 47 | func createSubscribers(h *Hub, qtd int, blocking bool) []Subscription { 48 | subs := make([]Subscription, 0, qtd) 49 | 50 | for i := 0; i < qtd; i++ { 51 | topic := strconv.Itoa(rand.Intn(10)) + "." + strconv.Itoa(rand.Intn(50)) + ".*" 52 | var sub Subscription 53 | 54 | if blocking { 55 | sub = h.Subscribe(20, topic) 56 | } else { 57 | sub = h.NonBlockingSubscribe(20, topic) 58 | } 59 | 60 | subs = append(subs, sub) 61 | } 62 | 63 | return subs 64 | } 65 | 66 | func processSubscriptionsForBench(subs []Subscription, wg *sync.WaitGroup) { 67 | for _, sub := range subs { 68 | go func(s Subscription) { 69 | for range s.Receiver { 70 | } 71 | 72 | wg.Done() 73 | }(sub) 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /throughput_test.go: -------------------------------------------------------------------------------- 1 | package hub 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "math/rand" 7 | "strconv" 8 | "strings" 9 | "sync" 10 | "testing" 11 | "time" 12 | ) 13 | 14 | const ( 15 | numSubs = 1000 16 | numMsgs = 1000000 17 | numPublishers = 4 18 | ) 19 | 20 | var ( 21 | throughputTest = flag.Bool("throughput", false, "execute throughput tests") 22 | 23 | topics = make([]string, numSubs) 24 | msgs = make([]Message, numMsgs) 25 | ) 26 | 27 | func setupTopics() { 28 | for i := 0; i < numSubs; i++ { 29 | switch { 30 | case i%10 == 0: 31 | topics[i] = fmt.Sprintf("*.%d.%d", rand.Intn(10), rand.Intn(10)) 32 | case i%25 == 0: 33 | topics[i] = fmt.Sprintf("%d.*.%d", rand.Intn(10), rand.Intn(10)) 34 | case i%45 == 0: 35 | topics[i] = fmt.Sprintf("%d.%d.*", rand.Intn(10), rand.Intn(10)) 36 | default: 37 | topics[i] = fmt.Sprintf("%d.%d.%d", rand.Intn(10), rand.Intn(10), rand.Intn(10)) 38 | } 39 | } 40 | 41 | for i := 0; i < numMsgs; i++ { 42 | topic := topics[i%numSubs] 43 | msgs[i] = Message{ 44 | Name: strings.Replace(topic, "*", strconv.Itoa(rand.Intn(10)), -1), 45 | } 46 | } 47 | } 48 | 49 | func TestThroughput(t *testing.T) { 50 | setupTopics() 51 | 52 | h := New() 53 | var wg sync.WaitGroup 54 | 55 | if !*throughputTest { 56 | t.Skip("throughputTests skipped") 57 | } 58 | 59 | for _, topic := range topics { 60 | sub := h.Subscribe(200, topic) 61 | go func(s Subscription) { 62 | for range s.Receiver { 63 | } 64 | 65 | wg.Done() 66 | }(sub) 67 | } 68 | 69 | before := time.Now() 70 | 71 | wg.Add(numPublishers) 72 | 73 | for i := 0; i < numPublishers; i++ { 74 | go func() { 75 | for _, msg := range msgs { 76 | h.Publish(msg) 77 | } 78 | 79 | wg.Done() 80 | }() 81 | } 82 | wg.Wait() 83 | wg.Add(numSubs) 84 | h.Close() 85 | wg.Wait() 86 | 87 | dur := time.Since(before) 88 | throughput := numMsgs * numPublishers / dur.Seconds() 89 | fmt.Printf("%f msg/sec\n", throughput) 90 | } 91 | -------------------------------------------------------------------------------- /subscriber.go: -------------------------------------------------------------------------------- 1 | package hub 2 | 3 | import ( 4 | "sync" 5 | ) 6 | 7 | type ( 8 | alertFunc func(missed int) 9 | 10 | nonBlockingSubscriber struct { 11 | ch chan Message 12 | alert alertFunc 13 | onceClose sync.Once 14 | } 15 | // blockingSubscriber uses an channel to receive events. 16 | blockingSubscriber struct { 17 | ch chan Message 18 | onceClose sync.Once 19 | } 20 | ) 21 | 22 | // newNonBlockingSubscriber returns a new nonBlockingSubscriber 23 | // this subscriber will never block when sending an message, if the capacity is full 24 | // we will ignore the message and call the Alert function from the Alerter. 25 | func newNonBlockingSubscriber(cap int, alerter alertFunc) *nonBlockingSubscriber { 26 | if cap <= 0 { 27 | cap = 10 28 | } 29 | 30 | return &nonBlockingSubscriber{ 31 | ch: make(chan Message, cap), 32 | alert: alerter, 33 | } 34 | } 35 | 36 | // Set inserts the given Event into the diode. 37 | func (s *nonBlockingSubscriber) Set(msg Message) { 38 | select { 39 | case s.ch <- msg: 40 | default: 41 | s.alert(1) 42 | } 43 | } 44 | 45 | // Ch return the channel used by subscriptions to consume messages. 46 | func (s *nonBlockingSubscriber) Ch() <-chan Message { 47 | return s.ch 48 | } 49 | 50 | // Close will close the internal channel and stop receiving messages. 51 | func (s *nonBlockingSubscriber) Close() { 52 | s.onceClose.Do(func() { 53 | close(s.ch) 54 | }) 55 | } 56 | 57 | // newBlockingSubscriber returns a blocking subscriber using chanels imternally. 58 | func newBlockingSubscriber(cap int) *blockingSubscriber { 59 | if cap < 0 { 60 | cap = 0 61 | } 62 | 63 | return &blockingSubscriber{ 64 | ch: make(chan Message, cap), 65 | } 66 | } 67 | 68 | // Set will send the message using the channel. 69 | func (s *blockingSubscriber) Set(msg Message) { 70 | s.ch <- msg 71 | } 72 | 73 | // Ch return the channel used by subscriptions to consume messages. 74 | func (s *blockingSubscriber) Ch() <-chan Message { 75 | return s.ch 76 | } 77 | 78 | // Close will close the internal channel and stop receiving messages. 79 | func (s *blockingSubscriber) Close() { 80 | s.onceClose.Do(func() { 81 | close(s.ch) 82 | }) 83 | } 84 | -------------------------------------------------------------------------------- /matching.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2018 Tyler Treat 2 | // Licensed under the Apache License, Version 2.0 (the "License"); 3 | // you may not use this file except in compliance with the License. 4 | // You may obtain a copy of the License at 5 | 6 | // http://www.apache.org/licenses/LICENSE-2.0 7 | 8 | // Unless required by applicable law or agreed to in writing, software 9 | // distributed under the License is distributed on an "AS IS" BASIS, 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | // See the License for the specific language governing permissions and 12 | // limitations under the License. 13 | 14 | // Modifications copyright (C) 2018 Leandro Lugaresi 15 | 16 | package hub 17 | 18 | const ( 19 | delimiter = "." 20 | wildcard = "*" 21 | ) 22 | 23 | type ( 24 | // Subscription represents a topic subscription. 25 | Subscription struct { 26 | Topics []string 27 | Receiver <-chan Message 28 | subscriber subscriber 29 | } 30 | 31 | // subscriber is the interface used internally to send values and get the channel used by subscribers. 32 | // This is used to override the behavior of channel and support nonBlocking operations 33 | subscriber interface { 34 | // Set send the given Event to be processed by the subscriber 35 | Set(Message) 36 | // Ch return the channel used to consume messages inside the subscription. 37 | // This func MUST always return the same channel. 38 | Ch() <-chan Message 39 | // Close will close the internal state and the subscriber will not receive more messages 40 | // WARN: This function can be executed more than one time so the code MUST take care of this situation and 41 | // avoid problems like close a closed channel. 42 | Close() 43 | } 44 | ) 45 | 46 | // matcher contains topic subscriptions and performs matches on them. 47 | type matcher interface { 48 | // Subscribe adds the Subscriber to the topics and returns a Subscription. 49 | Subscribe(topics []string, sub subscriber) Subscription 50 | 51 | // Unsubscribe removes the Subscription. 52 | Unsubscribe(sub Subscription) 53 | 54 | // Lookup returns the subscribers for the given topic. 55 | Lookup(topic string) []subscriber 56 | 57 | Subscriptions() []Subscription 58 | } 59 | -------------------------------------------------------------------------------- /.github/workflows/pull_request.yml: -------------------------------------------------------------------------------- 1 | name: reviewdog 2 | on: [pull_request] 3 | jobs: 4 | golangci-lint: 5 | name: runner / golangci-lint 6 | runs-on: ubuntu-latest 7 | steps: 8 | - name: Check out code into the Go module directory 9 | uses: actions/checkout@v1 10 | - name: golangci-lint 11 | uses: docker://reviewdog/action-golangci-lint:v1.6 # Pre-built image 12 | with: 13 | github_token: ${{ secrets.github_token }} 14 | golangci_lint_flags: "--config=.golangci.yml -D golint -D errcheck" 15 | reporter: github-pr-review 16 | 17 | # Use golint via golangci-lint binary with "warning" level. 18 | golint: 19 | name: runner / golint 20 | runs-on: ubuntu-latest 21 | steps: 22 | - name: Check out code into the Go module directory 23 | uses: actions/checkout@v1 24 | - name: golint 25 | uses: reviewdog/action-golangci-lint@v1 26 | with: 27 | github_token: ${{ secrets.github_token }} 28 | golangci_lint_flags: "--disable-all -E golint" 29 | tool_name: golint # Change reporter name. 30 | level: error 31 | reporter: github-pr-review 32 | 33 | errcheck: 34 | name: runner / errcheck 35 | runs-on: ubuntu-latest 36 | steps: 37 | - name: Check out code into the Go module directory 38 | uses: actions/checkout@v1 39 | - name: errcheck 40 | uses: reviewdog/action-golangci-lint@v1 41 | with: 42 | github_token: ${{ secrets.github_token }} 43 | golangci_lint_flags: "--disable-all -E errcheck" 44 | tool_name: errcheck 45 | level: warning 46 | reporter: github-pr-review 47 | 48 | staticcheck: 49 | name: runner / staticcheck 50 | runs-on: ubuntu-latest 51 | steps: 52 | - uses: actions/checkout@v2 53 | - uses: reviewdog/action-staticcheck@v1 54 | with: 55 | github_token: ${{ secrets.github_token }} 56 | reporter: github-pr-review 57 | filter_mode: nofilter 58 | fail_on_error: true 59 | 60 | misspell: 61 | name: runner / misspell 62 | runs-on: ubuntu-latest 63 | steps: 64 | - uses: actions/checkout@v2 65 | - uses: reviewdog/action-misspell@v1 66 | with: 67 | github_token: ${{ secrets.github_token }} 68 | locale: "US" 69 | reporter: github-pr-review 70 | 71 | languagetool: 72 | name: runner / languagetool 73 | runs-on: ubuntu-latest 74 | steps: 75 | - uses: actions/checkout@v2 76 | - uses: reviewdog/action-languagetool@v1 77 | with: 78 | github_token: ${{ secrets.github_token }} 79 | reporter: github-pr-review 80 | level: info 81 | patterns: | 82 | **/*.md 83 | !**/testdata/** 84 | -------------------------------------------------------------------------------- /hub.go: -------------------------------------------------------------------------------- 1 | package hub 2 | 3 | // AlertTopic is used to notify when a nonblocking subscriber loose one message 4 | // You can subscribe on this topic and log or send metrics. 5 | const AlertTopic = "hub.subscription.messageslost" 6 | 7 | type ( 8 | //Hub is a component that provides publish and subscribe capabilities for messages. 9 | // Every message has a Name used to route them to subscribers and this can be used like RabbitMQ topics exchanges. 10 | // Where every word is separated by dots `.` and you can use `*` as a wildcard. 11 | Hub struct { 12 | matcher matcher 13 | fields Fields 14 | } 15 | ) 16 | 17 | // New create and return a new empty hub. 18 | func New() *Hub { 19 | return &Hub{ 20 | matcher: newCSTrieMatcher(), 21 | fields: Fields{}, 22 | } 23 | } 24 | 25 | // Publish will send an event to all the subscribers matching the event name. 26 | func (h *Hub) Publish(m Message) { 27 | for k, v := range h.fields { 28 | m.Fields[k] = v 29 | } 30 | 31 | for _, sub := range h.matcher.Lookup(m.Topic()) { 32 | sub.Set(m) 33 | } 34 | } 35 | 36 | // With creates a child Hub with the fields added to it. 37 | // When someone call Publish, this Fields will be added automatically into the message. 38 | func (h *Hub) With(f Fields) *Hub { 39 | hub := Hub{ 40 | matcher: h.matcher, 41 | fields: Fields{}, 42 | } 43 | for k, v := range h.fields { 44 | hub.fields[k] = v 45 | } 46 | 47 | for k, v := range f { 48 | hub.fields[k] = v 49 | } 50 | 51 | return &hub 52 | } 53 | 54 | // Subscribe create a blocking subscription to receive events for a given topic. 55 | // The cap param is used inside the subscriber and in this case used to create a channel. 56 | // cap(1) = unbuffered channel. 57 | func (h *Hub) Subscribe(cap int, topics ...string) Subscription { 58 | return h.matcher.Subscribe(topics, newBlockingSubscriber(cap)) 59 | } 60 | 61 | // NonBlockingSubscribe create a nonblocking subscription to receive events for a given topic. 62 | // This subscriber will loose messages if the buffer reaches the max capability. 63 | func (h *Hub) NonBlockingSubscribe(cap int, topics ...string) Subscription { 64 | return h.matcher.Subscribe( 65 | topics, 66 | newNonBlockingSubscriber( 67 | cap, 68 | alertFunc(func(missed int) { 69 | h.alert(missed, topics) 70 | }), 71 | )) 72 | } 73 | 74 | // Unsubscribe remove and close the Subscription. 75 | func (h *Hub) Unsubscribe(sub Subscription) { 76 | h.matcher.Unsubscribe(sub) 77 | sub.subscriber.Close() 78 | } 79 | 80 | // Close will unsubscribe all the subscriptions and close them all. 81 | func (h *Hub) Close() { 82 | subs := h.matcher.Subscriptions() 83 | for _, s := range subs { 84 | h.matcher.Unsubscribe(s) 85 | } 86 | 87 | for _, s := range subs { 88 | s.subscriber.Close() 89 | } 90 | } 91 | 92 | func (h *Hub) alert(missed int, topics []string) { 93 | h.Publish(Message{ 94 | Name: AlertTopic, 95 | Fields: Fields{ 96 | "missed": missed, 97 | "topic": topics, 98 | }, 99 | }) 100 | } 101 | -------------------------------------------------------------------------------- /utils_test.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2018 Tyler Treat 2 | // Licensed under the Apache License, Version 2.0 (the "License"); 3 | // you may not use this file except in compliance with the License. 4 | // You may obtain a copy of the License at 5 | 6 | // http://www.apache.org/licenses/LICENSE-2.0 7 | 8 | // Unless required by applicable law or agreed to in writing, software 9 | // distributed under the License is distributed on an "AS IS" BASIS, 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | // See the License for the specific language governing permissions and 12 | // limitations under the License. 13 | 14 | // Modifications copyright (C) 2018 Leandro Lugaresi 15 | 16 | package hub 17 | 18 | import ( 19 | "math/rand" 20 | "strconv" 21 | "sync" 22 | "testing" 23 | 24 | "github.com/stretchr/testify/assert" 25 | ) 26 | 27 | type discardSubscriber int 28 | 29 | func (d discardSubscriber) Set(msg Message) {} 30 | func (d discardSubscriber) Ch() <-chan Message { return make(chan Message) } 31 | func (d discardSubscriber) Close() {} 32 | 33 | var result []subscriber 34 | 35 | func benchmarkMatcher(b *testing.B, numThreads int, m matcher, doSubs func(n int) bool) { 36 | numItems := 1000 37 | itemsToInsert := generateTopics(numThreads, numItems) 38 | sub := discardSubscriber(0) 39 | 40 | var wg sync.WaitGroup 41 | 42 | populateMatcher(m, 3) 43 | b.ResetTimer() 44 | 45 | for i := 0; i < b.N; i++ { 46 | wg.Add(numThreads) 47 | 48 | for j := 0; j < numThreads; j++ { 49 | go func(j int) { 50 | var r []subscriber 51 | 52 | for n, key := range itemsToInsert[j] { 53 | if doSubs(n) { 54 | m.Subscribe([]string{key}, sub) 55 | continue 56 | } 57 | 58 | r = m.Lookup(key) 59 | } 60 | 61 | result = r 62 | 63 | wg.Done() 64 | }(j) 65 | } 66 | wg.Wait() 67 | } 68 | } 69 | 70 | func percentual5050(n int) bool { 71 | return n%2 == 0 72 | } 73 | 74 | func percentual9010(n int) bool { 75 | return n%10 == 0 76 | } 77 | 78 | func assertEqual(assert *assert.Assertions, expected, actual []subscriber) { 79 | assert.Len(actual, len(expected)) 80 | 81 | for _, sub := range expected { 82 | assert.Contains(actual, sub) 83 | } 84 | } 85 | 86 | func generateTopics(numThreads, numItems int) [][]string { 87 | itemsToInsert := make([][]string, 0, numThreads) 88 | 89 | for i := 0; i < numThreads; i++ { 90 | items := make([]string, 0, numItems) 91 | 92 | for j := 0; j < numItems; j++ { 93 | topic := strconv.Itoa(j%10) + "." + strconv.Itoa(j%50) + "." + strconv.Itoa(j) 94 | items = append(items, topic) 95 | } 96 | 97 | itemsToInsert = append(itemsToInsert, items) 98 | } 99 | 100 | return itemsToInsert 101 | } 102 | 103 | func populateMatcher(m matcher, topicSize int) { 104 | num := 1000 105 | 106 | for i := 0; i < num; i++ { 107 | prefix := "" 108 | topic := "" 109 | 110 | for j := 0; j < topicSize; j++ { 111 | topic += prefix + strconv.Itoa(rand.Int()) 112 | prefix = "." 113 | } 114 | m.Subscribe([]string{topic}, discardSubscriber(0)) 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | ## Our Standards 8 | 9 | Examples of behavior that contributes to creating a positive environment include: 10 | 11 | * Using welcoming and inclusive language 12 | * Being respectful of differing viewpoints and experiences 13 | * Gracefully accepting constructive criticism 14 | * Focusing on what is best for the community 15 | * Showing empathy towards other community members 16 | 17 | Examples of unacceptable behavior by participants include: 18 | 19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances 20 | * Trolling, insulting/derogatory comments, and personal or political attacks 21 | * Public or private harassment 22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission 23 | * Other conduct which could reasonably be considered inappropriate in a professional setting 24 | 25 | ## Our Responsibilities 26 | 27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. 28 | 29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. 30 | 31 | ## Scope 32 | 33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. 34 | 35 | ## Enforcement 36 | 37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at leandrolugaresi92@gmail.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. 38 | 39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. 40 | 41 | ## Attribution 42 | 43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] 44 | 45 | [homepage]: http://contributor-covenant.org 46 | [version]: http://contributor-covenant.org/version/1/4/ 47 | -------------------------------------------------------------------------------- /.golangci.yml: -------------------------------------------------------------------------------- 1 | # This file contains all available configuration options 2 | # with their default values. 3 | 4 | # options for analysis running 5 | run: 6 | # timeout for analysis, e.g. 30s, 5m, default is 1m 7 | deadline: 10m 8 | 9 | # output configuration options 10 | output: 11 | # colored-line-number|line-number|json|tab|checkstyle, default is "colored-line-number" 12 | format: colored-line-number 13 | 14 | # print lines of code with issue, default is true 15 | print-issued-lnes: true 16 | 17 | # print linter name in the end of issue text, default is true 18 | print-linter-name: true 19 | 20 | # all available settings of specific linters 21 | linters-settings: 22 | golint: 23 | # minimal confidence for issues, default is 0.8 24 | min-confidence: 0.8 25 | gocyclo: 26 | # minimal code complexity to report, 30 by default (but we recommend 10-20) 27 | min-complexity: 20 28 | maligned: 29 | # print struct with more effective memory layout or not, false by default 30 | suggest-new: true 31 | goimports: 32 | local-prefixes: github.com/empregoligado/rabbids 33 | depguard: 34 | list-type: blacklist 35 | include-go-root: false 36 | packages: 37 | - github.com/davecgh/go-spew/spew 38 | lll: 39 | # max line length, lines longer will be reported. Default is 120. 40 | # '\t' is counted as 1 character by default, and can be changed with the tab-width option 41 | line-length: 130 42 | # tab width in spaces. Default to 1. 43 | tab-width: 4 44 | unused: 45 | # treat code as a program (not a library) and report unused exported identifiers; default is false. 46 | # XXX: if you enable this setting, unused will report a lot of false-positives in text editors: 47 | # if it's called for subdir of a project it can't find funcs usages. All text editor integrations 48 | # with golangci-lint call it on a directory with the changed file. 49 | check-exported: false 50 | unparam: 51 | # Inspect exported functions, default is false. Set to true if no external program/library imports your code. 52 | # XXX: if you enable this setting, unparam will report a lot of false-positives in text editors: 53 | # if it's called for subdir of a project it can't find external interfaces. All text editor integrations 54 | # with golangci-lint call it on a directory with the changed file. 55 | check-exported: false 56 | wsl: 57 | # If true append is only allowed to be cuddled if appending value is 58 | # matching variables, fields or types on line above. Default is true. 59 | strict-append: true 60 | # Allow calls and assignments to be cuddled as long as the lines have any 61 | # matching variables, fields or types. Default is true. 62 | allow-assign-and-call: true 63 | # Allow multiline assignments to be cuddled. Default is true. 64 | allow-multiline-assign: true 65 | # Allow declarations (var) to be cuddled. 66 | allow-cuddle-declarations: true 67 | # Allow trailing comments in ending of blocks 68 | allow-trailing-comment: false 69 | # Force newlines in end of case at this limit (0 = never). 70 | force-case-trailing-whitespace: 0 71 | 72 | linters: 73 | enable-all: true 74 | disable: 75 | - maligned 76 | - prealloc 77 | - gosec 78 | - gochecknoglobals 79 | - goimports 80 | - gomnd 81 | 82 | issues: 83 | # List of regexps of issue texts to exclude, empty list by default. 84 | # But independently from this option we use default exclude patterns, 85 | # it can be disabled by `exclude-use-default: false`. To list all 86 | # excluded by default patterns execute `golangci-lint run --help` 87 | # exclude: 88 | # - newHTTP - result 1 (error) is always nil 89 | -------------------------------------------------------------------------------- /hub_test.go: -------------------------------------------------------------------------------- 1 | package hub 2 | 3 | import ( 4 | "sync/atomic" 5 | "testing" 6 | "time" 7 | 8 | "github.com/stretchr/testify/require" 9 | ) 10 | 11 | type messageCounter struct { 12 | c int64 13 | sub Subscription 14 | } 15 | 16 | // nolint:funlen 17 | func TestHub(t *testing.T) { 18 | h := New() 19 | defaultMessages := []Message{ 20 | {Name: "forex.eur"}, 21 | {Name: "forex"}, 22 | {Name: "trade.jpy"}, 23 | {Name: "forex.jpy"}, 24 | {Name: "trade"}, 25 | {Name: "trade.usd"}, 26 | {Name: "forex.usd"}, 27 | {Name: "trade.eur"}, 28 | } 29 | testCases := []struct { 30 | name string 31 | messages []Message 32 | subFN func(h *Hub) Subscription 33 | ExpectedCount int 34 | }{ 35 | { 36 | name: "simple subscription unbuffered", 37 | messages: defaultMessages, 38 | subFN: func(h *Hub) Subscription { return h.Subscribe(0, "forex.*") }, 39 | ExpectedCount: 3, 40 | }, 41 | { 42 | name: "simple subscription buffered", 43 | messages: defaultMessages, 44 | subFN: func(h *Hub) Subscription { return h.Subscribe(2, "*.usd") }, 45 | ExpectedCount: 2, 46 | }, 47 | { 48 | name: "simple subscription with an invalid cap buffer", 49 | messages: defaultMessages, 50 | subFN: func(h *Hub) Subscription { return h.Subscribe(-1, "forex", "forex.eur", "forex.*") }, 51 | ExpectedCount: 4, 52 | }, 53 | { 54 | name: "non blocking subscription unbuffered", 55 | messages: defaultMessages, 56 | subFN: func(h *Hub) Subscription { return h.NonBlockingSubscribe(0, "*.eur", "trade") }, 57 | ExpectedCount: 3, 58 | }, 59 | { 60 | name: "non blocking subscription buffered", 61 | messages: defaultMessages, 62 | subFN: func(h *Hub) Subscription { return h.NonBlockingSubscribe(2, "forex", "forex.eur", "forex.*") }, 63 | ExpectedCount: 4, 64 | }, 65 | { 66 | name: "non blocking subscription with an invalid cap buffer", 67 | messages: defaultMessages, 68 | subFN: func(h *Hub) Subscription { return h.NonBlockingSubscribe(-1, "trade") }, 69 | ExpectedCount: 1, 70 | }, 71 | { 72 | name: "get all the messages", 73 | messages: defaultMessages, 74 | subFN: func(h *Hub) Subscription { return h.NonBlockingSubscribe(0, "*", "*.*") }, 75 | ExpectedCount: 8, 76 | }, 77 | } 78 | 79 | for _, tc := range testCases { 80 | tc := tc 81 | t.Run(tc.name, func(t *testing.T) { 82 | sub := tc.subFN(h) 83 | counter := newMessageCounter(sub) 84 | for _, m := range tc.messages { 85 | time.Sleep(time.Millisecond) 86 | h.Publish(m) 87 | } 88 | 89 | time.Sleep(time.Second) 90 | require.EqualValues(t, tc.ExpectedCount, counter.count()) 91 | 92 | counter.reset() 93 | h.Close() 94 | 95 | for _, m := range tc.messages { 96 | h.Publish(m) 97 | } 98 | require.EqualValues(t, 0, counter.count(), "after close the hub all the subsctibers MUST not get more events") 99 | }) 100 | } 101 | } 102 | 103 | func TestNonBlockingSubscriberShouldAlertIfLoseMessages(t *testing.T) { 104 | h := New() 105 | sub := h.NonBlockingSubscribe(10, "a.*.c") 106 | 107 | defer h.Unsubscribe(sub) 108 | 109 | subsAlert := h.Subscribe(1, AlertTopic) 110 | // send messages without a working subscriber 111 | for i := 0; i < 11; i++ { 112 | h.Publish(Message{Name: "a.c.c", Fields: Fields{"i": i}}) 113 | } 114 | 115 | msg := <-subsAlert.Receiver 116 | require.Equal(t, 1, msg.Fields["missed"]) 117 | require.Equal(t, []string{"a.*.c"}, msg.Fields["topic"]) 118 | } 119 | 120 | func TestWith(t *testing.T) { 121 | h := New() 122 | subH1 := h.With(Fields{"hub": "subH1", "something": 123}) 123 | subH11 := subH1.With(Fields{"hub": "subH11", "field": 456}) 124 | subH2 := h.With(Fields{"hub": "subH2", "something": 789}) 125 | 126 | subs := h.Subscribe(5, "*") 127 | 128 | h.Publish(Message{Name: "foo", Fields: Fields{"msg": 1}}) 129 | subH1.Publish(Message{Name: "foo", Fields: Fields{"msg": 2}}) 130 | subH11.Publish(Message{Name: "foo", Fields: Fields{"msg": 3}}) 131 | subH2.Publish(Message{Name: "foo", Fields: Fields{"msg": 4, "something": 1234}}) 132 | 133 | msg := <-subs.Receiver 134 | require.Equal(t, Fields{"msg": 1}, msg.Fields) 135 | msg = <-subs.Receiver 136 | require.Equal(t, Fields{"msg": 2, "hub": "subH1", "something": 123}, msg.Fields) 137 | msg = <-subs.Receiver 138 | require.Equal(t, Fields{ 139 | "msg": 3, 140 | "hub": "subH11", 141 | "something": 123, 142 | "field": 456, 143 | }, msg.Fields) 144 | 145 | msg = <-subs.Receiver 146 | require.Equal(t, Fields{"msg": 4, "hub": "subH2", "something": 789}, msg.Fields) 147 | 148 | h.Unsubscribe(subs) 149 | v, ok := <-subs.Receiver 150 | require.Falsef(t, ok, "Unsubscribe should close the internal channel, received fields %v", v) 151 | } 152 | 153 | func newMessageCounter(s Subscription) *messageCounter { 154 | ms := &messageCounter{sub: s, c: 0} 155 | go func(ms *messageCounter) { 156 | for range ms.sub.Receiver { 157 | atomic.AddInt64(&ms.c, 1) 158 | } 159 | }(ms) 160 | 161 | return ms 162 | } 163 | 164 | func (ms *messageCounter) count() int64 { 165 | return atomic.LoadInt64(&ms.c) 166 | } 167 | 168 | func (ms *messageCounter) reset() { 169 | atomic.StoreInt64(&ms.c, int64(0)) 170 | } 171 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Hub 2 | 3 | :incoming_envelope: A fast enough Event Hub for go applications using publish/subscribe with support patterns on topics like rabbitMQ exchanges. 4 | 5 | [![Release](https://img.shields.io/github/release/leandro-lugaresi/hub.svg?style=flat-square)](https://github.com/leandro-lugaresi/hub/releases/latest) 6 | [![Software License](https://img.shields.io/github/license/leandro-lugaresi/hub.svg?style=flat-square)](LICENSE.md) 7 | [![Actions Status](https://github.com/leandro-lugaresi/hub/workflows/Go/badge.svg)](https://github.com/leandro-lugaresi/hub/actions) 8 | [![Coverage Status](https://img.shields.io/codecov/c/github/leandro-lugaresi/hub/main.svg?style=flat-square)](https://codecov.io/gh/leandro-lugaresi/hub) 9 | [![Go Doc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat-square)](http://godoc.org/github.com/leandro-lugaresi/hub) 10 | [![Go Report Card](https://goreportcard.com/badge/github.com/leandro-lugaresi/hub?style=flat-square)](https://goreportcard.com/report/github.com/leandro-lugaresi/hub) 11 | [![Say Thanks!](https://img.shields.io/badge/Say%20Thanks-!-1EAEDB.svg)](https://saythanks.io/to/leandro-lugaresi) 12 | 13 | --- 14 | 15 | ## Table of Contents 16 | 17 | - [Install](#install) 18 | - [Usage](#usage) 19 | - [Examples & Demos](#examples--demos) 20 | - [Benchmarks](#benchmarks) 21 | - [CSTrie](#cstrie) 22 | - [Contribute](CONTRIBUTING.md) 23 | - [Code of conduct](CODE_OF_CONDUCT.md) 24 | 25 | ## Install 26 | 27 | To install this library you can `go get` it but I encourage you to always vendor your dependencies or use one of the version tags of this project. 28 | 29 | ```sh 30 | go get -u github.com/leandro-lugaresi/hub 31 | ``` 32 | 33 | ```sh 34 | dep ensure --add github.com/leandro-lugaresi/hub 35 | ``` 36 | 37 | ## Usage 38 | 39 | ### Subscribers 40 | 41 | Hub provides subscribers as buffered (cap > `0`) and unbuffered (cap = 0) channels but we have two different types of subscribers: 42 | 43 | - `Subscriber` this is the default subscriber and it's a blocking subscriber so if the channel is full and you try to send another message the send operation will block until the subscriber consumes some message. 44 | - `NonBlockingSubscriber` this subscriber will never block on the publish side but if the capacity of the channel is reached the publish operation will be lost and an alert will be trigged. 45 | This should be used only if loose data is acceptable. ie: metrics, logs 46 | 47 | ### Topics 48 | 49 | This library uses the same concept of topic exchanges on rabbiMQ, so the message name is used to find all the subscribers that match the topic, like a route. 50 | The topic must be a list of words delimited by dots (`.`) however, there is one important special case for binding keys: 51 | `*` (star) can substitute for exactly one word. 52 | 53 | ## Examples & Demos 54 | 55 | ```go 56 | package main 57 | 58 | import ( 59 | "fmt" 60 | "sync" 61 | 62 | "github.com/leandro-lugaresi/hub" 63 | ) 64 | func main() { 65 | h := hub.New() 66 | var wg sync.WaitGroup 67 | // the cap param is used to create one buffered channel with cap = 10 68 | // If you wan an unbuferred channel use the 0 cap 69 | sub := h.Subscribe(10, "account.login.*", "account.changepassword.*") 70 | wg.Add(1) 71 | go func(s hub.Subscription) { 72 | for msg := range s.Receiver { 73 | fmt.Printf("receive msg with topic %s and id %d\n", msg.Name, msg.Fields["id"]) 74 | } 75 | wg.Done() 76 | }(sub) 77 | 78 | h.Publish(hub.Message{ 79 | Name: "account.login.failed", 80 | Fields: hub.Fields{"id": 123}, 81 | }) 82 | h.Publish(hub.Message{ 83 | Name: "account.changepassword.failed", 84 | Fields: hub.Fields{"id": 456}, 85 | }) 86 | h.Publish(hub.Message{ 87 | Name: "account.login.success", 88 | Fields: hub.Fields{"id": 123}, 89 | }) 90 | // message not routed to this subscriber 91 | h.Publish(hub.Message{ 92 | Name: "account.foo.failed", 93 | Fields: hub.Fields{"id": 789}, 94 | }) 95 | 96 | // close all the subscribers 97 | h.Close() 98 | // wait until finish all the messages on buffer 99 | wg.Wait() 100 | 101 | // Output: 102 | // receive msg with topic account.login.failed and id 123 103 | // receive msg with topic account.changepassword.failed and id 456 104 | // receive msg with topic account.login.success and id 123 105 | } 106 | ``` 107 | 108 | See more [here](https://godoc.org/github.com/leandro-lugaresi/hub#example-Hub)! 109 | 110 | ## Benchmarks 111 | 112 | To run the benchmarks you can execute: 113 | 114 | ```bash 115 | make bench 116 | ``` 117 | 118 | Currently, I only have the benchmarks of the CSTrie used internally. I will provide more benchmarks. 119 | 120 | ## Throughput 121 | 122 | The project have one test for throughput, just execute: 123 | 124 | ```bash 125 | make throughput 126 | ``` 127 | 128 | In a intel(R) core(TM) i5-4460 CPU @ 3.20GHz x4 we got this results: 129 | 130 | ``` 131 | go test -v -timeout 60s github.com/leandro-lugaresi/hub -run ^TestThroughput -args -throughput 132 | === RUN TestThroughput 133 | 1317530.091292 msg/sec 134 | --- PASS: TestThroughput (3.04s) 135 | PASS 136 | ok github.com/leandro-lugaresi/hub 3.192s 137 | ``` 138 | 139 | ## CSTrie 140 | 141 | This project uses internally an awesome Concurrent Subscription Trie done by [@tylertreat](https://github.com/tylertreat). If you want to learn more about see this [blog post](http://bravenewgeek.com/fast-topic-matching/) and the code is [here](https://github.com/tylertreat/fast-topic-matching) 142 | 143 | --- 144 | 145 | This project adheres to the Contributor Covenant [code of conduct](CODE_OF_CONDUCT.md). By participating, you are expected to uphold this code. 146 | We appreciate your contribution. Please refer to our [contributing guidelines](CONTRIBUTING.md) for further information 147 | -------------------------------------------------------------------------------- /matching_cstrie_test.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2018 Tyler Treat 2 | // Licensed under the Apache License, Version 2.0 (the "License"); 3 | // you may not use this file except in compliance with the License. 4 | // You may obtain a copy of the License at 5 | 6 | // http://www.apache.org/licenses/LICENSE-2.0 7 | 8 | // Unless required by applicable law or agreed to in writing, software 9 | // distributed under the License is distributed on an "AS IS" BASIS, 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | // See the License for the specific language governing permissions and 12 | // limitations under the License. 13 | 14 | // Modifications copyright (C) 2018 Leandro Lugaresi 15 | 16 | package hub 17 | 18 | import ( 19 | "testing" 20 | 21 | "github.com/stretchr/testify/assert" 22 | ) 23 | 24 | func TestCSTrieMatcher(t *testing.T) { 25 | assert := assert.New(t) 26 | var ( 27 | m = newCSTrieMatcher() 28 | s0 = discardSubscriber(0) 29 | s1 = discardSubscriber(1) 30 | s2 = discardSubscriber(2) 31 | ) 32 | 33 | sub0 := m.Subscribe([]string{"forex.*"}, s0) 34 | sub1 := m.Subscribe([]string{"*.usd"}, s0) 35 | sub2 := m.Subscribe([]string{"forex.eur"}, s0) 36 | sub3 := m.Subscribe([]string{"*.eur"}, s1) 37 | sub4 := m.Subscribe([]string{"forex.*"}, s1) 38 | sub5 := m.Subscribe([]string{"trade"}, s1) 39 | sub6 := m.Subscribe([]string{"*"}, s2) 40 | assert.Len(m.Subscriptions(), 7) 41 | 42 | assertEqual(assert, []subscriber{s0, s1}, m.Lookup("forex.eur")) 43 | assertEqual(assert, []subscriber{s2}, m.Lookup("forex")) 44 | assertEqual(assert, []subscriber{}, m.Lookup("trade.jpy")) 45 | assertEqual(assert, []subscriber{s0, s1}, m.Lookup("forex.jpy")) 46 | assertEqual(assert, []subscriber{s1, s2}, m.Lookup("trade")) 47 | 48 | m.Unsubscribe(sub0) 49 | m.Unsubscribe(sub1) 50 | m.Unsubscribe(sub2) 51 | m.Unsubscribe(sub3) 52 | m.Unsubscribe(sub4) 53 | m.Unsubscribe(sub5) 54 | m.Unsubscribe(sub6) 55 | 56 | assertEqual(assert, []subscriber{}, m.Lookup("forex.eur")) 57 | assertEqual(assert, []subscriber{}, m.Lookup("forex")) 58 | assertEqual(assert, []subscriber{}, m.Lookup("trade.jpy")) 59 | assertEqual(assert, []subscriber{}, m.Lookup("forex.jpy")) 60 | assertEqual(assert, []subscriber{}, m.Lookup("trade")) 61 | } 62 | 63 | func BenchmarkCSTrieMatcherSubscribe(b *testing.B) { 64 | var ( 65 | m = newCSTrieMatcher() 66 | s0 = discardSubscriber(0) 67 | ) 68 | 69 | populateMatcher(m, 5) 70 | 71 | b.ResetTimer() 72 | 73 | for i := 0; i < b.N; i++ { 74 | m.Subscribe([]string{"foo.*.baz.qux.quux"}, s0) 75 | } 76 | } 77 | 78 | func BenchmarkCSTrieMatcherUnsubscribe(b *testing.B) { 79 | var ( 80 | m = newCSTrieMatcher() 81 | s0 = discardSubscriber(0) 82 | id = m.Subscribe([]string{"foo.*.baz.qux.quux"}, s0) 83 | ) 84 | 85 | populateMatcher(m, 5) 86 | b.ResetTimer() 87 | 88 | for i := 0; i < b.N; i++ { 89 | m.Unsubscribe(id) 90 | } 91 | } 92 | 93 | func BenchmarkCSTrieMatcherLookup(b *testing.B) { 94 | var ( 95 | m = newCSTrieMatcher() 96 | s0 = discardSubscriber(0) 97 | ) 98 | 99 | m.Subscribe([]string{"foo.*.baz.qux.quux"}, s0) 100 | populateMatcher(m, 5) 101 | b.ResetTimer() 102 | 103 | for i := 0; i < b.N; i++ { 104 | m.Lookup("foo.bar.baz.qux.quux") 105 | } 106 | } 107 | 108 | func BenchmarkCSTrieMatcherSubscribeCold(b *testing.B) { 109 | var ( 110 | m = newCSTrieMatcher() 111 | s0 = discardSubscriber(0) 112 | ) 113 | 114 | b.ResetTimer() 115 | 116 | for i := 0; i < b.N; i++ { 117 | m.Subscribe([]string{"foo.*.baz.qux.quux"}, s0) 118 | } 119 | } 120 | 121 | func BenchmarkCSTrieMatcherUnsubscribeCold(b *testing.B) { 122 | var ( 123 | m = newCSTrieMatcher() 124 | s0 = discardSubscriber(0) 125 | id = m.Subscribe([]string{"foo.*.baz.qux.quux"}, s0) 126 | ) 127 | 128 | b.ResetTimer() 129 | 130 | for i := 0; i < b.N; i++ { 131 | m.Unsubscribe(id) 132 | } 133 | } 134 | 135 | func BenchmarkCSTrieMatcherLookupCold(b *testing.B) { 136 | var ( 137 | m = newCSTrieMatcher() 138 | s0 = discardSubscriber(0) 139 | ) 140 | 141 | m.Subscribe([]string{"foo.*.baz.qux.quux"}, s0) 142 | b.ResetTimer() 143 | 144 | for i := 0; i < b.N; i++ { 145 | m.Lookup("foo.bar.baz.qux.quux") 146 | } 147 | } 148 | 149 | func BenchmarkMultithreaded1Thread5050CSTrie(b *testing.B) { 150 | numThreads := 1 151 | benchmarkMatcher(b, numThreads, newCSTrieMatcher(), percentual5050) 152 | } 153 | 154 | func BenchmarkMultithreaded2Thread5050CSTrie(b *testing.B) { 155 | numThreads := 2 156 | benchmarkMatcher(b, numThreads, newCSTrieMatcher(), percentual5050) 157 | } 158 | 159 | func BenchmarkMultithreaded4Thread5050CSTrie(b *testing.B) { 160 | numThreads := 4 161 | benchmarkMatcher(b, numThreads, newCSTrieMatcher(), percentual5050) 162 | } 163 | 164 | func BenchmarkMultithreaded8Thread5050CSTrie(b *testing.B) { 165 | numThreads := 8 166 | benchmarkMatcher(b, numThreads, newCSTrieMatcher(), percentual5050) 167 | } 168 | 169 | func BenchmarkMultithreaded12Thread5050CSTrie(b *testing.B) { 170 | numThreads := 12 171 | benchmarkMatcher(b, numThreads, newCSTrieMatcher(), percentual5050) 172 | } 173 | 174 | func BenchmarkMultithreaded16Thread5050CSTrie(b *testing.B) { 175 | numThreads := 16 176 | benchmarkMatcher(b, numThreads, newCSTrieMatcher(), percentual5050) 177 | } 178 | 179 | func BenchmarkMultithreaded1Thread9010CSTrie(b *testing.B) { 180 | numThreads := 1 181 | benchmarkMatcher(b, numThreads, newCSTrieMatcher(), percentual9010) 182 | } 183 | 184 | func BenchmarkMultithreaded2Thread9010CSTrie(b *testing.B) { 185 | numThreads := 2 186 | benchmarkMatcher(b, numThreads, newCSTrieMatcher(), percentual9010) 187 | } 188 | 189 | func BenchmarkMultithreaded4Thread9010CSTrie(b *testing.B) { 190 | numThreads := 4 191 | benchmarkMatcher(b, numThreads, newCSTrieMatcher(), percentual9010) 192 | } 193 | 194 | func BenchmarkMultithreaded8Thread9010CSTrie(b *testing.B) { 195 | numThreads := 8 196 | benchmarkMatcher(b, numThreads, newCSTrieMatcher(), percentual9010) 197 | } 198 | 199 | func BenchmarkMultithreaded12Thread9010CSTrie(b *testing.B) { 200 | numThreads := 12 201 | benchmarkMatcher(b, numThreads, newCSTrieMatcher(), percentual9010) 202 | } 203 | 204 | func BenchmarkMultithreaded16Thread9010CSTrie(b *testing.B) { 205 | numThreads := 16 206 | benchmarkMatcher(b, numThreads, newCSTrieMatcher(), percentual9010) 207 | } 208 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [2018] [Leandro Lugaresi] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /matching_cstrie.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2018 Tyler Treat 2 | // Licensed under the Apache License, Version 2.0 (the "License"); 3 | // you may not use this file except in compliance with the License. 4 | // You may obtain a copy of the License at 5 | 6 | // http://www.apache.org/licenses/LICENSE-2.0 7 | 8 | // Unless required by applicable law or agreed to in writing, software 9 | // distributed under the License is distributed on an "AS IS" BASIS, 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | // See the License for the specific language governing permissions and 12 | // limitations under the License. 13 | 14 | // Modifications copyright (C) 2018 Leandro Lugaresi 15 | 16 | package hub 17 | 18 | import ( 19 | "strings" 20 | "sync/atomic" 21 | "unsafe" 22 | ) 23 | 24 | type iNode struct { 25 | main *mainNode 26 | } 27 | 28 | type mainNode struct { 29 | cNode *cNode 30 | tNode *tNode 31 | } 32 | 33 | type cNode struct { 34 | branches map[string]*branch 35 | } 36 | 37 | // newCNode creates a new C-node with the given subscription path. 38 | func newCNode(words []string, sub subscriber) *cNode { 39 | if len(words) == 1 { 40 | return &cNode{ 41 | branches: map[string]*branch{ 42 | words[0]: {subs: map[subscriber]struct{}{sub: {}}}, 43 | }, 44 | } 45 | } 46 | 47 | nin := &iNode{main: &mainNode{cNode: newCNode(words[1:], sub)}} 48 | 49 | return &cNode{ 50 | branches: map[string]*branch{ 51 | words[0]: {subs: map[subscriber]struct{}{}, iNode: nin}, 52 | }, 53 | } 54 | } 55 | 56 | // inserted returns a copy of this C-node with the specified subscriber 57 | // inserted. 58 | func (c *cNode) inserted(words []string, sub subscriber) *cNode { 59 | branches := make(map[string]*branch, len(c.branches)+1) 60 | for key, branch := range c.branches { 61 | branches[key] = branch 62 | } 63 | 64 | var br *branch 65 | if len(words) == 1 { 66 | br = &branch{subs: map[subscriber]struct{}{sub: {}}} 67 | } else { 68 | br = &branch{ 69 | subs: make(map[subscriber]struct{}), 70 | iNode: &iNode{main: &mainNode{cNode: newCNode(words[1:], sub)}}, 71 | } 72 | } 73 | 74 | branches[words[0]] = br 75 | 76 | return &cNode{branches: branches} 77 | } 78 | 79 | // updated returns a copy of this C-node with the specified branch updated. 80 | func (c *cNode) updated(word string, sub subscriber) *cNode { 81 | branches := make(map[string]*branch, len(c.branches)) 82 | for word, branch := range c.branches { 83 | branches[word] = branch 84 | } 85 | 86 | newBranch := &branch{subs: map[subscriber]struct{}{sub: {}}} 87 | br, ok := branches[word] 88 | 89 | if ok { 90 | for id, sub := range br.subs { 91 | newBranch.subs[id] = sub 92 | } 93 | 94 | newBranch.iNode = br.iNode 95 | } 96 | 97 | branches[word] = newBranch 98 | 99 | return &cNode{branches: branches} 100 | } 101 | 102 | // updatedBranch returns a copy of this C-node with the specified branch 103 | // updated. 104 | func (c *cNode) updatedBranch(word string, in *iNode, br *branch) *cNode { 105 | branches := make(map[string]*branch, len(c.branches)) 106 | for key, branch := range c.branches { 107 | branches[key] = branch 108 | } 109 | 110 | branches[word] = br.updated(in) 111 | 112 | return &cNode{branches: branches} 113 | } 114 | 115 | // removed returns a copy of this C-node with the subscriber removed from the 116 | // corresponding branch. 117 | func (c *cNode) removed(word string, sub subscriber) *cNode { 118 | branches := make(map[string]*branch, len(c.branches)) 119 | for word, branch := range c.branches { 120 | branches[word] = branch 121 | } 122 | 123 | br, ok := branches[word] 124 | if ok { 125 | br = br.removed(sub) 126 | if len(br.subs) == 0 && br.iNode == nil { 127 | // Remove the branch if it contains no subscribers and doesn't 128 | // point anywhere. 129 | delete(branches, word) 130 | } else { 131 | branches[word] = br 132 | } 133 | } 134 | 135 | return &cNode{branches: branches} 136 | } 137 | 138 | // getBranches returns the branches for the given word. There are two possible 139 | // branches: exact match and single wildcard. 140 | func (c *cNode) getBranches(word string) (*branch, *branch) { 141 | return c.branches[word], c.branches[wildcard] 142 | } 143 | 144 | type branch struct { 145 | iNode *iNode 146 | subs map[subscriber]struct{} 147 | } 148 | 149 | // updated returns a copy of this branch updated with the given I-node. 150 | func (b *branch) updated(in *iNode) *branch { 151 | subs := make(map[subscriber]struct{}, len(b.subs)) 152 | for id, sub := range b.subs { 153 | subs[id] = sub 154 | } 155 | 156 | return &branch{subs: subs, iNode: in} 157 | } 158 | 159 | // removed returns a copy of this branch with the given subscriber removed. 160 | func (b *branch) removed(sub subscriber) *branch { 161 | subs := make(map[subscriber]struct{}, len(b.subs)) 162 | for id, sub := range b.subs { 163 | subs[id] = sub 164 | } 165 | 166 | delete(subs, sub) 167 | 168 | return &branch{subs: subs, iNode: b.iNode} 169 | } 170 | 171 | // subscribers returns the Subscribers for this branch. 172 | func (b *branch) subscribers() []subscriber { 173 | subs := make([]subscriber, len(b.subs)) 174 | i := 0 175 | 176 | for sub := range b.subs { 177 | subs[i] = sub 178 | i++ 179 | } 180 | 181 | return subs 182 | } 183 | 184 | type tNode struct{} 185 | 186 | type csTrieMatcher struct { 187 | root *iNode 188 | } 189 | 190 | func newCSTrieMatcher() matcher { 191 | root := &iNode{main: &mainNode{cNode: &cNode{}}} 192 | return &csTrieMatcher{root: root} 193 | } 194 | 195 | // Subscribe adds the subscriber to the topic and returns a Subscription. 196 | func (c *csTrieMatcher) Subscribe(topics []string, sub subscriber) Subscription { 197 | var ( 198 | rootPtr = (*unsafe.Pointer)(unsafe.Pointer(&c.root)) 199 | root = (*iNode)(atomic.LoadPointer(rootPtr)) 200 | ) 201 | 202 | for _, topic := range topics { 203 | words := strings.Split(topic, delimiter) 204 | if !c.iinsert(root, nil, words, sub) { 205 | return c.Subscribe(topics, sub) 206 | } 207 | } 208 | 209 | return Subscription{Topics: topics, Receiver: sub.Ch(), subscriber: sub} 210 | } 211 | 212 | func (c *csTrieMatcher) iinsert(i, parent *iNode, words []string, sub subscriber) bool { 213 | // Linearization point. 214 | mainPtr := (*unsafe.Pointer)(unsafe.Pointer(&i.main)) 215 | main := (*mainNode)(atomic.LoadPointer(mainPtr)) 216 | 217 | switch { 218 | case main.cNode != nil: 219 | cn := main.cNode 220 | br := cn.branches[words[0]] 221 | 222 | if br == nil { 223 | // If the relevant branch is not in the map, a copy of the C-node 224 | // with the new entry is created. The linearization point is a 225 | // successful CAS. 226 | ncn := &mainNode{cNode: cn.inserted(words, sub)} 227 | return atomic.CompareAndSwapPointer( 228 | mainPtr, unsafe.Pointer(main), unsafe.Pointer(ncn)) 229 | } 230 | // If the relevant key is present in the map, its corresponding 231 | // branch is read. 232 | if len(words) > 1 { 233 | // If more than 1 word is present in the path, the tree must be 234 | // traversed deeper. 235 | if br.iNode != nil { 236 | // If the branch has an I-node, iinsert is called 237 | // recursively. 238 | return c.iinsert(br.iNode, i, words[1:], sub) 239 | } 240 | // Otherwise, an I-node which points to a new C-node must be 241 | // added. The linearization point is a successful CAS. 242 | nin := &iNode{main: &mainNode{cNode: newCNode(words[1:], sub)}} 243 | ncn := &mainNode{cNode: cn.updatedBranch(words[0], nin, br)} 244 | 245 | return atomic.CompareAndSwapPointer( 246 | mainPtr, unsafe.Pointer(main), unsafe.Pointer(ncn)) 247 | } 248 | 249 | if _, ok := br.subs[sub]; ok { 250 | // Already subscribed. 251 | return true 252 | } 253 | // Insert the subscriber by copying the C-node and updating the 254 | // respective branch. The linearization point is a successful CAS. 255 | ncn := &mainNode{cNode: cn.updated(words[0], sub)} 256 | 257 | return atomic.CompareAndSwapPointer(mainPtr, unsafe.Pointer(main), unsafe.Pointer(ncn)) 258 | case main.tNode != nil: 259 | clean(parent) 260 | return false 261 | default: 262 | panic("csTrie is in an invalid state") 263 | } 264 | } 265 | 266 | // Unsubscribe removes the Subscription. 267 | func (c *csTrieMatcher) Unsubscribe(sub Subscription) { 268 | var ( 269 | rootPtr = (*unsafe.Pointer)(unsafe.Pointer(&c.root)) 270 | root = (*iNode)(atomic.LoadPointer(rootPtr)) 271 | ) 272 | 273 | for _, topic := range sub.Topics { 274 | words := strings.Split(topic, delimiter) 275 | if !c.iremove(root, nil, nil, words, 0, sub.subscriber) { 276 | c.Unsubscribe(sub) 277 | } 278 | } 279 | } 280 | 281 | func (c *csTrieMatcher) iremove(i, parent, parentsParent *iNode, words []string, wordIdx int, sub subscriber) bool { 282 | // Linearization point. 283 | mainPtr := (*unsafe.Pointer)(unsafe.Pointer(&i.main)) 284 | main := (*mainNode)(atomic.LoadPointer(mainPtr)) 285 | 286 | switch { 287 | case main.cNode != nil: 288 | cn := main.cNode 289 | br := cn.branches[words[wordIdx]] 290 | 291 | if br == nil { 292 | // If the relevant word is not in the map, the subscription doesn't 293 | // exist. 294 | return true 295 | } 296 | // If the relevant word is present in the map, its corresponding 297 | // branch is read. 298 | if wordIdx+1 < len(words) { 299 | // If more than 1 word is present in the path, the tree must be 300 | // traversed deeper. 301 | if br.iNode != nil { 302 | // If the branch has an I-node, iremove is called 303 | // recursively. 304 | return c.iremove(br.iNode, i, parent, words, wordIdx+1, sub) 305 | } 306 | // Otherwise, the subscription doesn't exist. 307 | return true 308 | } 309 | 310 | if _, ok := br.subs[sub]; !ok { 311 | // Not subscribed. 312 | return true 313 | } 314 | // Remove the subscriber by copying the C-node without it. A 315 | // contraction of the copy is then created. A successful CAS will 316 | // substitute the old C-node with the copied C-node, thus removing 317 | // the subscriber from the trie - this is the linearization point. 318 | ncn := cn.removed(words[wordIdx], sub) 319 | cntr := c.toContracted(ncn, i) 320 | 321 | if atomic.CompareAndSwapPointer( 322 | mainPtr, unsafe.Pointer(main), unsafe.Pointer(cntr)) { 323 | if parent != nil { 324 | mainPtr = (*unsafe.Pointer)(unsafe.Pointer(&i.main)) 325 | main = (*mainNode)(atomic.LoadPointer(mainPtr)) 326 | 327 | if main.tNode != nil { 328 | cleanParent(i, parent, parentsParent, c, words[wordIdx-1]) 329 | } 330 | } 331 | 332 | return true 333 | } 334 | 335 | return false 336 | case main.tNode != nil: 337 | clean(parent) 338 | return false 339 | default: 340 | panic("csTrie is in an invalid state") 341 | } 342 | } 343 | 344 | // Lookup returns the Subscribers for the given topic. 345 | func (c *csTrieMatcher) Lookup(topic string) []subscriber { 346 | var ( 347 | words = strings.Split(topic, delimiter) 348 | rootPtr = (*unsafe.Pointer)(unsafe.Pointer(&c.root)) 349 | root = (*iNode)(atomic.LoadPointer(rootPtr)) 350 | ) 351 | 352 | result, ok := c.ilookup(root, nil, words) 353 | if !ok { 354 | return c.Lookup(topic) 355 | } 356 | 357 | return result 358 | } 359 | 360 | // ilookup attempts to retrieve the Subscribers for the word path. True is 361 | // returned if the Subscribers were retrieved, false if the operation needs to 362 | // be retried. 363 | func (c *csTrieMatcher) ilookup(i, parent *iNode, words []string) ([]subscriber, bool) { 364 | // Linearization point. 365 | mainPtr := (*unsafe.Pointer)(unsafe.Pointer(&i.main)) 366 | main := (*mainNode)(atomic.LoadPointer(mainPtr)) 367 | 368 | switch { 369 | case main.cNode != nil: 370 | // Traverse exact-match branch and single-word-wildcard branch. 371 | exact, singleWC := main.cNode.getBranches(words[0]) 372 | subs := make(map[subscriber]struct{}) 373 | 374 | if exact != nil { 375 | s, ok := c.bLookup(i, exact, words) 376 | if !ok { 377 | return nil, false 378 | } 379 | 380 | for _, sub := range s { 381 | subs[sub] = struct{}{} 382 | } 383 | } 384 | 385 | if singleWC != nil { 386 | s, ok := c.bLookup(i, singleWC, words) 387 | if !ok { 388 | return nil, false 389 | } 390 | 391 | for _, sub := range s { 392 | subs[sub] = struct{}{} 393 | } 394 | } 395 | 396 | s := make([]subscriber, len(subs)) 397 | i := 0 398 | 399 | for sub := range subs { 400 | s[i] = sub 401 | i++ 402 | } 403 | 404 | return s, true 405 | case main.tNode != nil: 406 | clean(parent) 407 | return nil, false 408 | default: 409 | panic("csTrie is in an invalid state") 410 | } 411 | } 412 | 413 | // bLookup attempts to retrieve the Subscribers from the word path along the 414 | // given branch. True is returned if the Subscribers were retrieved, false if 415 | // the operation needs to be retried. 416 | func (c *csTrieMatcher) bLookup(i *iNode, b *branch, words []string) ([]subscriber, bool) { 417 | if len(words) > 1 { 418 | // If more than 1 key is present in the path, the tree must be 419 | // traversed deeper. 420 | if b.iNode == nil { 421 | // If the branch doesn't point to an I-node, no subscribers 422 | // exist. 423 | return make([]subscriber, 0), true 424 | } 425 | // If the branch has an I-node, ilookup is called recursively. 426 | return c.ilookup(b.iNode, i, words[1:]) 427 | } 428 | 429 | // Retrieve the subscribers from the branch. 430 | return b.subscribers(), true 431 | } 432 | 433 | // Subscriptions return all the subscriptions inside the cstrie. 434 | func (c *csTrieMatcher) Subscriptions() []Subscription { 435 | var ( 436 | rootPtr = (*unsafe.Pointer)(unsafe.Pointer(&c.root)) 437 | root = (*iNode)(atomic.LoadPointer(rootPtr)) 438 | ) 439 | 440 | result, ok := c.isubscriptions(root, nil, []string{}) 441 | if !ok { 442 | return c.Subscriptions() 443 | } 444 | 445 | return result 446 | } 447 | 448 | func (c *csTrieMatcher) isubscriptions(i, parent *iNode, words []string) ([]Subscription, bool) { 449 | // Linearization point. 450 | mainPtr := (*unsafe.Pointer)(unsafe.Pointer(&i.main)) 451 | main := (*mainNode)(atomic.LoadPointer(mainPtr)) 452 | subs := []Subscription{} 453 | 454 | switch { 455 | case main.cNode != nil: 456 | // Traverse all branches. 457 | for word, br := range main.cNode.branches { 458 | cwords := append([]string{}, words...) 459 | cwords = append(cwords, word) 460 | 461 | if br.iNode != nil { 462 | // If the branch has an I-node, isubscriptions is called recursively. 463 | s, ok := c.isubscriptions(br.iNode, i, cwords) 464 | if !ok { 465 | return nil, false 466 | } 467 | 468 | subs = append(subs, s...) 469 | } 470 | 471 | for s := range br.subs { 472 | subs = append(subs, Subscription{ 473 | Topics: []string{strings.Join(cwords, delimiter)}, 474 | subscriber: s, 475 | Receiver: s.Ch(), 476 | }) 477 | } 478 | } 479 | 480 | return subs, true 481 | case main.tNode != nil: 482 | clean(parent) 483 | return nil, false 484 | default: 485 | panic("csTrie is in an invalid state") 486 | } 487 | } 488 | 489 | // toContracted ensures that every I-node except the root points to a C-node 490 | // with at least one branch or a T-node. If a given C-node has no branches and 491 | // is not at the root level, a T-node is returned. 492 | func (c *csTrieMatcher) toContracted(cn *cNode, parent *iNode) *mainNode { 493 | if c.root != parent && len(cn.branches) == 0 { 494 | return &mainNode{tNode: &tNode{}} 495 | } 496 | 497 | return &mainNode{cNode: cn} 498 | } 499 | 500 | // clean replaces an I-node's C-node with a copy that has any tombed I-nodes 501 | // resurrected. 502 | func clean(i *iNode) { 503 | mainPtr := (*unsafe.Pointer)(unsafe.Pointer(&i.main)) 504 | main := (*mainNode)(atomic.LoadPointer(mainPtr)) 505 | 506 | if main.cNode != nil { 507 | atomic.CompareAndSwapPointer(mainPtr, 508 | unsafe.Pointer(main), unsafe.Pointer(toCompressed(main.cNode))) 509 | } 510 | } 511 | 512 | // cleanParent reads the main node of the parent I-node p and the current 513 | // I-node i and checks if the T-node below i is reachable from p. If i is no 514 | // longer reachable, some other thread has already completed the contraction. 515 | // If it is reachable, the C-node below p is replaced with its contraction. 516 | func cleanParent(i, parent, parentsParent *iNode, c *csTrieMatcher, word string) { 517 | var ( 518 | mainPtr = (*unsafe.Pointer)(unsafe.Pointer(&i.main)) 519 | main = (*mainNode)(atomic.LoadPointer(mainPtr)) 520 | pMainPtr = (*unsafe.Pointer)(unsafe.Pointer(&parent.main)) 521 | pMain = (*mainNode)(atomic.LoadPointer(pMainPtr)) 522 | ) 523 | 524 | if pMain.cNode != nil { 525 | if br, ok := pMain.cNode.branches[word]; ok { 526 | if br.iNode != i { 527 | return 528 | } 529 | 530 | if main.tNode != nil { 531 | if !contract(parentsParent, parent, c, pMain) { 532 | cleanParent(parentsParent, parent, i, c, word) 533 | } 534 | } 535 | } 536 | } 537 | } 538 | 539 | // contract performs a contraction of the parent's C-node if possible. Returns 540 | // true if the contraction succeeded, false if it needs to be retried. 541 | func contract(parentsParent, parent *iNode, c *csTrieMatcher, pMain *mainNode) bool { 542 | ncn := toCompressed(pMain.cNode) 543 | if len(ncn.cNode.branches) == 0 && parentsParent != nil { 544 | // If the compressed C-node has no branches, it and the I-node above it 545 | // should be removed. To do this, a CAS must occur on the parent I-node 546 | // of the parent to update the respective branch of the C-node below it 547 | // to point to nil. 548 | ppMainPtr := (*unsafe.Pointer)(unsafe.Pointer(&parentsParent.main)) 549 | ppMain := (*mainNode)(atomic.LoadPointer(ppMainPtr)) 550 | 551 | for pKey, pBranch := range ppMain.cNode.branches { 552 | // Find the branch pointing to the parent. 553 | if pBranch.iNode == parent { 554 | // Update the branch to point to nil. 555 | updated := ppMain.cNode.updatedBranch(pKey, nil, pBranch) 556 | 557 | if len(pBranch.subs) == 0 { 558 | // If the branch has no subscribers, simply prune it. 559 | delete(updated.branches, pKey) 560 | } 561 | 562 | // Replace the main node of the parent's parent. 563 | return atomic.CompareAndSwapPointer(ppMainPtr, 564 | unsafe.Pointer(ppMain), unsafe.Pointer(toCompressed(updated))) 565 | } 566 | } 567 | } else { 568 | // Otherwise, perform a simple contraction to a T-node. 569 | cntr := c.toContracted(ncn.cNode, parent) 570 | pMainPtr := (*unsafe.Pointer)(unsafe.Pointer(&parent.main)) 571 | pMain := (*mainNode)(atomic.LoadPointer(pMainPtr)) 572 | if !atomic.CompareAndSwapPointer(pMainPtr, unsafe.Pointer(pMain), 573 | unsafe.Pointer(cntr)) { 574 | return false 575 | } 576 | } 577 | 578 | return true 579 | } 580 | 581 | // toCompressed prunes any branches to tombed I-nodes and returns the 582 | // compressed main node. 583 | func toCompressed(cn *cNode) *mainNode { 584 | branches := make(map[string]*branch, len(cn.branches)) 585 | for key, br := range cn.branches { 586 | if !prunable(br) { 587 | branches[key] = br 588 | } 589 | } 590 | 591 | return &mainNode{cNode: &cNode{branches: branches}} 592 | } 593 | 594 | // prunable indicates if the branch can be pruned. A branch can be pruned if 595 | // it has no subscribers and points to nowhere or it has no subscribers and 596 | // points to a tombed I-node. 597 | func prunable(br *branch) bool { 598 | if len(br.subs) > 0 { 599 | return false 600 | } 601 | 602 | if br.iNode == nil { 603 | return true 604 | } 605 | 606 | mainPtr := (*unsafe.Pointer)(unsafe.Pointer(&br.iNode.main)) 607 | main := (*mainNode)(atomic.LoadPointer(mainPtr)) 608 | 609 | return main.tNode != nil 610 | } 611 | --------------------------------------------------------------------------------