-
Notifications
You must be signed in to change notification settings - Fork 118
Add Event Bus #184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Event Bus #184
Changes from 4 commits
4c7ec9f
6f2f6a0
4b97168
5b5138a
749731d
6bfce87
def78f9
bc7ef22
35068c3
ffd0f4a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package proxy | ||
|
|
||
| // package level registry of the different event types | ||
|
|
||
| const ProcessStateChangeEventID = 0x01 | ||
| const ChatCompletionStatsEventID = 0x02 | ||
| const ConfigFileChangedEventID = 0x03 | ||
|
|
||
| type ProcessStateChangeEvent struct { | ||
| ProcessName string | ||
| NewState ProcessState | ||
| OldState ProcessState | ||
| } | ||
|
|
||
| func (e ProcessStateChangeEvent) Type() uint32 { | ||
| return ProcessStateChangeEventID | ||
| } | ||
|
|
||
| type ChatCompletionStats struct { | ||
| TokensGenerated int | ||
| } | ||
|
|
||
| func (e ChatCompletionStats) Type() uint32 { | ||
| return ChatCompletionStatsEventID | ||
| } | ||
|
|
||
| type ConfigFileChangedEvent struct{} | ||
|
|
||
| func (e ConfigFileChangedEvent) Type() uint32 { | ||
| return ConfigFileChangedEventID | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,14 +2,25 @@ package proxy | |
|
|
||
| import ( | ||
| "container/ring" | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "sync" | ||
|
|
||
| "github.com/kelindar/event" | ||
| ) | ||
|
|
||
| type LogLevel int | ||
|
|
||
| type LogDataEvent struct { | ||
| Data []byte | ||
| } | ||
|
|
||
| func (e LogDataEvent) Type() uint32 { | ||
| return 0x01 | ||
| } | ||
|
||
|
|
||
| const ( | ||
| LevelDebug LogLevel = iota | ||
| LevelInfo | ||
|
|
@@ -18,7 +29,7 @@ const ( | |
| ) | ||
|
|
||
| type LogMonitor struct { | ||
| clients map[chan []byte]bool | ||
| eventbus *event.Dispatcher | ||
| mu sync.RWMutex | ||
| buffer *ring.Ring | ||
| bufferMu sync.RWMutex | ||
|
|
@@ -37,11 +48,11 @@ func NewLogMonitor() *LogMonitor { | |
|
|
||
| func NewLogMonitorWriter(stdout io.Writer) *LogMonitor { | ||
| return &LogMonitor{ | ||
| clients: make(map[chan []byte]bool), | ||
| buffer: ring.New(10 * 1024), // keep 10KB of buffered logs | ||
| stdout: stdout, | ||
| level: LevelInfo, | ||
| prefix: "", | ||
| eventbus: event.NewDispatcher(), | ||
| buffer: ring.New(10 * 1024), // keep 10KB of buffered logs | ||
| stdout: stdout, | ||
| level: LevelInfo, | ||
| prefix: "", | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -81,34 +92,14 @@ func (w *LogMonitor) GetHistory() []byte { | |
| return history | ||
| } | ||
|
|
||
| func (w *LogMonitor) Subscribe() chan []byte { | ||
| w.mu.Lock() | ||
| defer w.mu.Unlock() | ||
|
|
||
| ch := make(chan []byte, 100) | ||
| w.clients[ch] = true | ||
| return ch | ||
| } | ||
|
|
||
| func (w *LogMonitor) Unsubscribe(ch chan []byte) { | ||
| w.mu.Lock() | ||
| defer w.mu.Unlock() | ||
|
|
||
| delete(w.clients, ch) | ||
| close(ch) | ||
| func (w *LogMonitor) OnLogData(callback func(data []byte)) context.CancelFunc { | ||
| return event.Subscribe(w.eventbus, func(e LogDataEvent) { | ||
| callback(e.Data) | ||
| }) | ||
| } | ||
|
|
||
| func (w *LogMonitor) broadcast(msg []byte) { | ||
| w.mu.RLock() | ||
| defer w.mu.RUnlock() | ||
|
|
||
| for client := range w.clients { | ||
| select { | ||
| case client <- msg: | ||
| default: | ||
| // If client buffer is full, skip | ||
| } | ||
| } | ||
| event.Publish(w.eventbus, LogDataEvent{Data: msg}) | ||
| } | ||
|
|
||
| func (w *LogMonitor) SetPrefix(prefix string) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.