-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
456 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// This source code is dual-licensed under the Apache License, version | ||
// 2.0, and the Mozilla Public License, version 2.0. | ||
// Copyright (c) 2007-2023 VMware, Inc. | ||
|
||
using System.Collections.Generic; | ||
using RabbitMQ.Stream.Client.Reliable; | ||
|
||
namespace RabbitMQ.Stream.Client.EventBus; | ||
|
||
public abstract class ClientEvent : IStreamEvent | ||
{ | ||
protected ClientEvent(IDictionary<string, string> connectionProperties, ClientParameters parameters, | ||
EventTypes eventType, EventSeverity eventSeverity) | ||
{ | ||
ConnectionProperties = connectionProperties; | ||
Parameters = parameters; | ||
EventType = eventType; | ||
EventSeverity = eventSeverity; | ||
} | ||
|
||
public EventTypes EventType { get; internal set; } | ||
public EventSeverity EventSeverity { get; internal set; } | ||
|
||
public ClientParameters Parameters { get; } | ||
public IDictionary<string, string> ConnectionProperties { get; } | ||
} | ||
|
||
public class RawProducerConnected : ClientEvent | ||
{ | ||
public RawProducerConnected(IDictionary<string, string> connectionProperties, ClientParameters parameters, | ||
RawProducer instance) | ||
: base(connectionProperties, parameters, EventTypes.Connection, EventSeverity.Info) | ||
{ | ||
Instance = instance; | ||
} | ||
|
||
public RawProducer Instance { get; } | ||
} | ||
|
||
public class RawProducerDisconnected : ClientEvent | ||
{ | ||
public RawProducerDisconnected(IDictionary<string, string> connectionProperties, | ||
ClientParameters parameters, RawProducer instance) | ||
: base(connectionProperties, parameters, EventTypes.Disconnection, EventSeverity.Info) | ||
{ | ||
Instance = instance; | ||
} | ||
|
||
public RawProducer Instance { get; } | ||
} | ||
|
||
public class ReliableBaseReconnected : IStreamEvent | ||
{ | ||
public ReliableBaseReconnected(bool isReconnection, EventSeverity eventSeverity) | ||
{ | ||
IsReconnection = isReconnection; | ||
EventSeverity = eventSeverity; | ||
} | ||
|
||
public bool IsReconnection { get; } | ||
public EventTypes EventType { get; } = EventTypes.Reconnection; | ||
public EventSeverity EventSeverity { get; } | ||
} | ||
|
||
public class ProducerReconnected : ReliableBaseReconnected | ||
{ | ||
public Producer Instance { get; } | ||
|
||
public ProducerReconnected(bool isReconnection, EventSeverity eventSeverity, Producer instance) : base( | ||
isReconnection, eventSeverity) | ||
{ | ||
Instance = instance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// This source code is dual-licensed under the Apache License, version | ||
// 2.0, and the Mozilla Public License, version 2.0. | ||
// Copyright (c) 2007-2023 VMware, Inc. | ||
|
||
using System.Threading.Tasks; | ||
|
||
namespace RabbitMQ.Stream.Client.EventBus; | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
|
||
public enum EventTypes | ||
{ | ||
Connection, | ||
Reconnection, | ||
Disconnection, | ||
} | ||
|
||
public enum EventSeverity | ||
{ | ||
Info, | ||
Warning, | ||
Error, | ||
} | ||
|
||
public interface IStreamEvent | ||
{ | ||
EventTypes EventType { get; } | ||
EventSeverity EventSeverity { get; } | ||
} | ||
|
||
public interface IEventBus | ||
{ | ||
void Publish<T>(T v) where T : IStreamEvent; | ||
void Subscribe<T>(Func<T, Task> func) where T : IStreamEvent; | ||
} | ||
|
||
public class StreamEventsBus : IEventBus | ||
{ | ||
private readonly ConcurrentDictionary<Type, List<Func<IStreamEvent, Task>>> _subscriptions = new(); | ||
|
||
public void Publish<T>(T v) where T : IStreamEvent | ||
{ | ||
var type = typeof(T); | ||
if (_subscriptions.TryGetValue(type, out var funcs)) | ||
{ | ||
foreach (var func in funcs) | ||
{ | ||
func(v); | ||
} | ||
} | ||
} | ||
|
||
public void Subscribe<T>(Func<T, Task> func) where T : IStreamEvent | ||
{ | ||
var type = typeof(T); | ||
if (!_subscriptions.TryGetValue(type, out var funcs)) | ||
{ | ||
funcs = new List<Func<IStreamEvent, Task>>(); | ||
_subscriptions.TryAdd(type, funcs); | ||
} | ||
|
||
funcs.Add(e => func((T)e)); | ||
} | ||
} | ||
|
||
public static class StreamEventsBusSingleton | ||
{ | ||
private static readonly Lazy<StreamEventsBus> s_lazy = new(() => new StreamEventsBus()); | ||
public static StreamEventsBus Instance => s_lazy.Value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.