prysm-pulse/io/logs/stream.go
Raul Jordan d077483577
Add V3 Suffix to All Prysm Packages (#11083)
* v3 import renamings

* tidy

* fmt

* rev

* Update beacon-chain/core/epoch/precompute/reward_penalty_test.go

* Update beacon-chain/core/helpers/validators_test.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/iface/BUILD.bazel

* Update beacon-chain/db/kv/kv.go

* Update beacon-chain/db/kv/state.go

* Update beacon-chain/rpc/prysm/v1alpha1/validator/attester_test.go

* Update beacon-chain/rpc/prysm/v1alpha1/validator/attester_test.go

* Update beacon-chain/sync/initial-sync/service.go

* fix deps

* fix bad replacements

* fix bad replacements

* change back

* gohashtree version

* fix deps

Co-authored-by: Nishant Das <nishdas93@gmail.com>
Co-authored-by: Potuz <potuz@prysmaticlabs.com>
2022-08-16 12:20:13 +00:00

70 lines
1.6 KiB
Go

package logs
import (
"io"
lru "github.com/hashicorp/golang-lru"
"github.com/prysmaticlabs/prysm/v3/async/event"
lruwrpr "github.com/prysmaticlabs/prysm/v3/cache/lru"
"github.com/prysmaticlabs/prysm/v3/crypto/rand"
)
const (
// The number of log entries to keep in memory.
logCacheSize = 20
)
var (
// Compile time interface checks.
_ = io.Writer(&StreamServer{})
_ = Streamer(&StreamServer{})
)
// Streamer defines a struct which can retrieve and stream process logs.
type Streamer interface {
GetLastFewLogs() [][]byte
LogsFeed() *event.Feed
}
// StreamServer defines a a websocket server which can receive events from
// a feed and write them to open websocket connections.
type StreamServer struct {
feed *event.Feed
cache *lru.Cache
}
// NewStreamServer initializes a new stream server capable of
// streaming log events.
func NewStreamServer() *StreamServer {
ss := &StreamServer{
feed: new(event.Feed),
cache: lruwrpr.New(logCacheSize),
}
addLogWriter(ss)
return ss
}
// GetLastFewLogs returns the last few entries of logs stored in an LRU cache.
func (ss *StreamServer) GetLastFewLogs() [][]byte {
messages := make([][]byte, 0)
for _, k := range ss.cache.Keys() {
d, ok := ss.cache.Get(k)
if ok {
messages = append(messages, d.([]byte))
}
}
return messages
}
// LogsFeed returns a feed callers can subscribe to to receive logs via a channel.
func (ss *StreamServer) LogsFeed() *event.Feed {
return ss.feed
}
// Write a binary message and send over the event feed.
func (ss *StreamServer) Write(p []byte) (n int, err error) {
ss.feed.Send(p)
ss.cache.Add(rand.NewGenerator().Uint64(), p)
return len(p), nil
}