2024-07-23 23:21:19 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
// wave pubsub system
|
|
|
|
package wps
|
|
|
|
|
|
|
|
import (
|
2024-07-26 22:30:11 +02:00
|
|
|
"strings"
|
2024-07-23 23:21:19 +02:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/wavetermdev/thenextwave/pkg/util/utilfn"
|
2024-07-26 22:30:11 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/wshrpc"
|
2024-07-23 23:21:19 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// this broker interface is mostly generic
|
|
|
|
// strong typing and event types can be defined elsewhere
|
|
|
|
|
|
|
|
type Client interface {
|
|
|
|
ClientId() string
|
2024-07-26 22:30:11 +02:00
|
|
|
SendEvent(event wshrpc.WaveEvent)
|
2024-07-23 23:21:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type BrokerSubscription struct {
|
|
|
|
AllSubs []string // clientids of client subscribed to "all" events
|
|
|
|
ScopeSubs map[string][]string // clientids of client subscribed to specific scopes
|
2024-07-26 22:30:11 +02:00
|
|
|
StarSubs map[string][]string // clientids of client subscribed to star scope (scopes with "*" or "**" in them)
|
2024-07-23 23:21:19 +02:00
|
|
|
}
|
|
|
|
|
2024-07-26 22:30:11 +02:00
|
|
|
type BrokerType struct {
|
2024-07-23 23:21:19 +02:00
|
|
|
Lock *sync.Mutex
|
|
|
|
ClientMap map[string]Client
|
|
|
|
SubMap map[string]*BrokerSubscription
|
|
|
|
}
|
|
|
|
|
2024-07-26 22:30:11 +02:00
|
|
|
var Broker = &BrokerType{
|
|
|
|
Lock: &sync.Mutex{},
|
|
|
|
ClientMap: make(map[string]Client),
|
|
|
|
SubMap: make(map[string]*BrokerSubscription),
|
|
|
|
}
|
|
|
|
|
|
|
|
func scopeHasStarMatch(scope string) bool {
|
|
|
|
parts := strings.Split(scope, ":")
|
|
|
|
for _, part := range parts {
|
|
|
|
if part == "*" || part == "**" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BrokerType) Subscribe(subscriber Client, sub wshrpc.SubscriptionRequest) {
|
2024-07-23 23:21:19 +02:00
|
|
|
b.Lock.Lock()
|
|
|
|
defer b.Lock.Unlock()
|
|
|
|
clientId := subscriber.ClientId()
|
|
|
|
bs := b.SubMap[sub.Event]
|
|
|
|
if bs == nil {
|
|
|
|
bs = &BrokerSubscription{
|
|
|
|
AllSubs: []string{},
|
|
|
|
ScopeSubs: make(map[string][]string),
|
2024-07-26 22:30:11 +02:00
|
|
|
StarSubs: make(map[string][]string),
|
2024-07-23 23:21:19 +02:00
|
|
|
}
|
|
|
|
b.SubMap[sub.Event] = bs
|
|
|
|
}
|
|
|
|
if sub.AllScopes {
|
|
|
|
bs.AllSubs = utilfn.AddElemToSliceUniq(bs.AllSubs, clientId)
|
|
|
|
}
|
|
|
|
for _, scope := range sub.Scopes {
|
2024-07-26 22:30:11 +02:00
|
|
|
starMatch := scopeHasStarMatch(scope)
|
|
|
|
if starMatch {
|
|
|
|
addStrToScopeMap(bs.StarSubs, scope, clientId)
|
|
|
|
} else {
|
|
|
|
addStrToScopeMap(bs.ScopeSubs, scope, clientId)
|
|
|
|
}
|
2024-07-23 23:21:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *BrokerSubscription) IsEmpty() bool {
|
2024-07-26 22:30:11 +02:00
|
|
|
return len(bs.AllSubs) == 0 && len(bs.ScopeSubs) == 0 && len(bs.StarSubs) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeStrFromScopeMap(scopeMap map[string][]string, scope string, clientId string) {
|
|
|
|
scopeSubs := scopeMap[scope]
|
|
|
|
scopeSubs = utilfn.RemoveElemFromSlice(scopeSubs, clientId)
|
|
|
|
if len(scopeSubs) == 0 {
|
|
|
|
delete(scopeMap, scope)
|
|
|
|
} else {
|
|
|
|
scopeMap[scope] = scopeSubs
|
|
|
|
}
|
2024-07-23 23:21:19 +02:00
|
|
|
}
|
|
|
|
|
2024-07-26 22:30:11 +02:00
|
|
|
func removeStrFromScopeMapAll(scopeMap map[string][]string, clientId string) {
|
|
|
|
for scope, scopeSubs := range scopeMap {
|
|
|
|
scopeSubs = utilfn.RemoveElemFromSlice(scopeSubs, clientId)
|
|
|
|
if len(scopeSubs) == 0 {
|
|
|
|
delete(scopeMap, scope)
|
|
|
|
} else {
|
|
|
|
scopeMap[scope] = scopeSubs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func addStrToScopeMap(scopeMap map[string][]string, scope string, clientId string) {
|
|
|
|
scopeSubs := scopeMap[scope]
|
|
|
|
scopeSubs = utilfn.AddElemToSliceUniq(scopeSubs, clientId)
|
|
|
|
scopeMap[scope] = scopeSubs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BrokerType) Unsubscribe(subscriber Client, sub wshrpc.SubscriptionRequest) {
|
2024-07-23 23:21:19 +02:00
|
|
|
b.Lock.Lock()
|
|
|
|
defer b.Lock.Unlock()
|
|
|
|
clientId := subscriber.ClientId()
|
|
|
|
bs := b.SubMap[sub.Event]
|
|
|
|
if bs == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if sub.AllScopes {
|
|
|
|
bs.AllSubs = utilfn.RemoveElemFromSlice(bs.AllSubs, clientId)
|
|
|
|
}
|
|
|
|
for _, scope := range sub.Scopes {
|
2024-07-26 22:30:11 +02:00
|
|
|
starMatch := scopeHasStarMatch(scope)
|
|
|
|
if starMatch {
|
|
|
|
removeStrFromScopeMap(bs.StarSubs, scope, clientId)
|
2024-07-23 23:21:19 +02:00
|
|
|
} else {
|
2024-07-26 22:30:11 +02:00
|
|
|
removeStrFromScopeMap(bs.ScopeSubs, scope, clientId)
|
2024-07-23 23:21:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if bs.IsEmpty() {
|
|
|
|
delete(b.SubMap, sub.Event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-26 22:30:11 +02:00
|
|
|
func (b *BrokerType) UnsubscribeAll(subscriber Client) {
|
2024-07-23 23:21:19 +02:00
|
|
|
b.Lock.Lock()
|
|
|
|
defer b.Lock.Unlock()
|
|
|
|
clientId := subscriber.ClientId()
|
|
|
|
delete(b.ClientMap, clientId)
|
|
|
|
for eventType, bs := range b.SubMap {
|
|
|
|
bs.AllSubs = utilfn.RemoveElemFromSlice(bs.AllSubs, clientId)
|
2024-07-26 22:30:11 +02:00
|
|
|
removeStrFromScopeMapAll(bs.StarSubs, clientId)
|
|
|
|
removeStrFromScopeMapAll(bs.ScopeSubs, clientId)
|
2024-07-23 23:21:19 +02:00
|
|
|
if bs.IsEmpty() {
|
|
|
|
delete(b.SubMap, eventType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-26 22:30:11 +02:00
|
|
|
func (b *BrokerType) Publish(event wshrpc.WaveEvent) {
|
2024-07-23 23:21:19 +02:00
|
|
|
clientIds := b.getMatchingClientIds(event)
|
|
|
|
for _, clientId := range clientIds {
|
|
|
|
client := b.ClientMap[clientId]
|
|
|
|
if client != nil {
|
|
|
|
client.SendEvent(event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-26 22:30:11 +02:00
|
|
|
func (b *BrokerType) getMatchingClientIds(event wshrpc.WaveEvent) []string {
|
2024-07-23 23:21:19 +02:00
|
|
|
b.Lock.Lock()
|
|
|
|
defer b.Lock.Unlock()
|
|
|
|
bs := b.SubMap[event.Event]
|
|
|
|
if bs == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
clientIds := make(map[string]bool)
|
|
|
|
for _, clientId := range bs.AllSubs {
|
|
|
|
clientIds[clientId] = true
|
|
|
|
}
|
|
|
|
for _, scope := range event.Scopes {
|
|
|
|
for _, clientId := range bs.ScopeSubs[scope] {
|
|
|
|
clientIds[clientId] = true
|
|
|
|
}
|
2024-07-26 22:30:11 +02:00
|
|
|
for starScope := range bs.StarSubs {
|
|
|
|
if utilfn.StarMatchString(starScope, scope, ":") {
|
|
|
|
for _, clientId := range bs.StarSubs[starScope] {
|
|
|
|
clientIds[clientId] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-23 23:21:19 +02:00
|
|
|
}
|
|
|
|
var rtn []string
|
|
|
|
for clientId := range clientIds {
|
|
|
|
rtn = append(rtn, clientId)
|
|
|
|
}
|
|
|
|
return rtn
|
|
|
|
}
|