github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/eventbus/event_bus.go (about) 1 /* 2 * Copyright (C) 2019 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package eventbus 19 20 import ( 21 "fmt" 22 "sync" 23 24 asaskevichEventBus "github.com/mysteriumnetwork/EventBus" 25 "github.com/rs/zerolog" 26 "github.com/rs/zerolog/log" 27 ) 28 29 // EventBus allows subscribing and publishing data by topic 30 type EventBus interface { 31 Publisher 32 Subscriber 33 } 34 35 // Publisher publishes events 36 type Publisher interface { 37 Publish(topic string, data interface{}) 38 } 39 40 // Subscriber subscribes to events. 41 type Subscriber interface { 42 Subscribe(topic string, fn interface{}) error 43 SubscribeAsync(topic string, fn interface{}) error 44 Unsubscribe(topic string, fn interface{}) error 45 UnsubscribeWithUID(topic, uid string, fn interface{}) error 46 SubscribeWithUID(topic, uid string, fn interface{}) error 47 } 48 49 type simplifiedEventBus struct { 50 bus asaskevichEventBus.Bus 51 52 mu sync.RWMutex 53 sub map[string][]string 54 } 55 56 func (b *simplifiedEventBus) Unsubscribe(topic string, fn interface{}) error { 57 return b.bus.Unsubscribe(topic, fn) 58 } 59 60 func (b *simplifiedEventBus) UnsubscribeWithUID(topic, uid string, fn interface{}) error { 61 b.mu.Lock() 62 defer b.mu.Unlock() 63 64 if len(b.sub[topic]) == 0 { 65 return fmt.Errorf("topic %s doesn't exist", topic) 66 } 67 68 for i, id := range b.sub[topic] { 69 if id == uid { 70 b.sub[topic] = append(b.sub[topic][:i], b.sub[topic][i+1:]...) 71 72 break 73 } 74 } 75 76 if len(b.sub[topic]) == 0 { 77 delete(b.sub, topic) 78 } 79 80 return b.bus.Unsubscribe(topic+uid, fn) 81 } 82 83 func (b *simplifiedEventBus) Subscribe(topic string, fn interface{}) error { 84 return b.bus.Subscribe(topic, fn) 85 } 86 87 func (b *simplifiedEventBus) SubscribeWithUID(topic, uid string, fn interface{}) error { 88 b.mu.Lock() 89 defer b.mu.Unlock() 90 91 b.sub[topic] = append(b.sub[topic], uid) 92 93 return b.bus.Subscribe(topic+uid, fn) 94 } 95 96 func (b *simplifiedEventBus) SubscribeAsync(topic string, fn interface{}) error { 97 return b.bus.SubscribeAsync(topic, fn, false) 98 } 99 100 func (b *simplifiedEventBus) Publish(topic string, data interface{}) { 101 log.WithLevel(levelFor(topic)).Msgf("Published topic=%q event=%+v", topic, data) 102 b.bus.Publish(topic, data) 103 104 b.mu.RLock() 105 ids := b.sub[topic] 106 idsCopy := make([]string, len(ids)) 107 copy(idsCopy, ids) 108 b.mu.RUnlock() 109 110 for _, id := range idsCopy { 111 b.bus.Publish(topic+id, data) 112 } 113 } 114 115 // New returns implementation of EventBus. 116 func New() *simplifiedEventBus { 117 return &simplifiedEventBus{ 118 bus: asaskevichEventBus.New(), 119 sub: make(map[string][]string), 120 } 121 } 122 123 var logLevelsByTopic = map[string]zerolog.Level{ 124 "ProposalAdded": zerolog.Disabled, 125 "ProposalUpdated": zerolog.Disabled, 126 "ProposalRemoved": zerolog.Disabled, 127 "proposalEvent": zerolog.Disabled, 128 "Statistics": zerolog.Disabled, 129 "Throughput": zerolog.Disabled, 130 "State change": zerolog.TraceLevel, 131 "Session data transferred": zerolog.TraceLevel, 132 "Session change": zerolog.TraceLevel, 133 "hermes_promise_received": zerolog.TraceLevel, 134 } 135 136 func levelFor(topic string) zerolog.Level { 137 if level, exist := logLevelsByTopic[topic]; exist { 138 return level 139 } 140 141 return zerolog.DebugLevel 142 }