-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* rxgo event source to channel with drop strategy * code optimizations
- Loading branch information
1 parent
a00059c
commit 64fbdbb
Showing
6 changed files
with
106 additions
and
127 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,53 @@ | ||
package logging | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/reactivex/rxgo/v2" | ||
"context" | ||
) | ||
|
||
/* | ||
Logger implementation using the observable pattern. | ||
Implements io.Writer interface. | ||
Logger implementation using the observable pattern. | ||
Implements io.Writer interface. | ||
The observable is an event source which drops everythigng unless there's | ||
a subscriber connected. | ||
The observable is an event source which drops everythigng unless there's | ||
a subscriber connected. | ||
The observer implementatios are a http ServerSentEvents handler and a | ||
websocket one in handler.go | ||
The observer implementatios are a http ServerSentEvents handler and a | ||
websocket one in handler.go | ||
*/ | ||
|
||
var ( | ||
logsChan = make(chan rxgo.Item, 100) | ||
logsObservable = rxgo. | ||
FromEventSource(logsChan, rxgo.WithBackPressureStrategy(rxgo.Drop)). | ||
BufferWithTime(rxgo.WithDuration(time.Millisecond * 500)) | ||
) | ||
|
||
type ObservableLogger struct{} | ||
type ObservableLogger struct { | ||
logsChan chan []byte | ||
} | ||
|
||
func NewObservableLogger() *ObservableLogger { | ||
return &ObservableLogger{} | ||
return &ObservableLogger{ | ||
logsChan: make(chan []byte, 100), | ||
} | ||
} | ||
|
||
func (o *ObservableLogger) Write(p []byte) (n int, err error) { | ||
logsChan <- rxgo.Of(string(p)) | ||
|
||
n = len(p) | ||
err = nil | ||
select { | ||
case o.logsChan <- p: | ||
n = len(p) | ||
err = nil | ||
return | ||
default: | ||
return | ||
} | ||
} | ||
|
||
return | ||
func (o *ObservableLogger) Observe(ctx context.Context) <-chan string { | ||
logs := make(chan string) | ||
|
||
go func() { | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case logLine := <-o.logsChan: | ||
logs <- string(logLine) | ||
} | ||
} | ||
}() | ||
|
||
return logs | ||
} |
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