Skip to content

Commit

Permalink
feat(evpn-bridge): netlink watcher for configuration
Browse files Browse the repository at this point in the history
Co-authored-by: Vemula Venkatesh <venkatesh.vemula@intel.com>
Co-authored-by: Saikumar Banoth <banoth.saikumar@intel.com>
Co-authored-by: Jambekar Vishakha <vishakha.jambekar@intel.com>
Co-authored-by: Dimitrios Markou <dimitrios.markou@ericsson.com>
Signed-off-by: atulpatel261194 <Atul.Patel@intel.com>
  • Loading branch information
5 people committed May 23, 2024
1 parent 2d1d2ba commit 9839323
Show file tree
Hide file tree
Showing 2 changed files with 2,029 additions and 0 deletions.
63 changes: 63 additions & 0 deletions pkg/netlink/eventbus/eventbus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2022-2023 Intel Corporation, or its subsidiaries.

// Package eventbus handles pub sub
package eventbus

import (
"sync"
)

// EventBus holds the event bus info
type EventBus struct {
subscribers map[string][]*Subscriber
mutex sync.RWMutex
}

// Subscriber holds the info for each subscriber
type Subscriber struct {
Ch chan interface{}
Quit chan struct{}
}

// NewEventBus initializes an EventBus object
func NewEventBus() *EventBus {
return &EventBus{
subscribers: make(map[string][]*Subscriber),
}
}

// Subscribe api provides registration of a subscriber to the given eventType
func (e *EventBus) Subscribe(eventType string) *Subscriber {
e.mutex.Lock()
defer e.mutex.Unlock()

subscriber := &Subscriber{
Ch: make(chan interface{}),
Quit: make(chan struct{}),
}

e.subscribers[eventType] = append(e.subscribers[eventType], subscriber)

return subscriber
}

// Publish api notifies the subscribers with certain eventType
func (e *EventBus) Publish(eventType string, data interface{}) {
e.mutex.RLock()
defer e.mutex.RUnlock()

subscribers, ok := e.subscribers[eventType]
if !ok {
return
}

for _, sub := range subscribers {
sub.Ch <- data
}
}

// Unsubscribe the subscriber, which delete the subscriber(all resources will be washed out)
func (s *Subscriber) Unsubscribe() {
close(s.Ch)
}
Loading

0 comments on commit 9839323

Please sign in to comment.