2022-07-21 13:21:44 +00:00
|
|
|
package rpcsrv
|
2020-05-10 22:00:19 +00:00
|
|
|
|
|
|
|
import (
|
2023-10-11 10:24:16 +00:00
|
|
|
"sync/atomic"
|
|
|
|
|
2020-05-10 22:00:19 +00:00
|
|
|
"github.com/gorilla/websocket"
|
2022-07-22 16:09:29 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc"
|
2020-05-10 22:00:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2023-02-15 07:07:47 +00:00
|
|
|
// intEvent is an internal event that has both a proper structure and
|
|
|
|
// a websocket-ready message. It's used to serve websocket-based clients
|
|
|
|
// as well as internal ones.
|
|
|
|
intEvent struct {
|
|
|
|
msg *websocket.PreparedMessage
|
|
|
|
ntf *neorpc.Notification
|
|
|
|
}
|
2020-05-10 22:00:19 +00:00
|
|
|
// subscriber is an event subscriber.
|
|
|
|
subscriber struct {
|
2023-02-15 07:07:47 +00:00
|
|
|
writer chan<- intEvent
|
2020-05-12 19:38:29 +00:00
|
|
|
overflown atomic.Bool
|
2020-05-10 22:00:19 +00:00
|
|
|
// These work like slots as there is not a lot of them (it's
|
|
|
|
// cheaper doing it this way rather than creating a map),
|
2022-04-20 18:30:09 +00:00
|
|
|
// pointing to an EventID is an obvious overkill at the moment, but
|
2020-05-10 22:00:19 +00:00
|
|
|
// that's not for long.
|
2020-05-13 14:13:33 +00:00
|
|
|
feeds [maxFeeds]feed
|
|
|
|
}
|
2022-10-17 10:31:24 +00:00
|
|
|
// feed stores subscriber's desired event ID with filter.
|
2020-05-13 14:13:33 +00:00
|
|
|
feed struct {
|
2022-07-22 16:09:29 +00:00
|
|
|
event neorpc.EventID
|
2023-12-25 11:14:44 +00:00
|
|
|
filter neorpc.SubscriptionFilter
|
2020-05-10 22:00:19 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-10-17 10:31:24 +00:00
|
|
|
// EventID implements neorpc.EventComparator interface and returns notification ID.
|
|
|
|
func (f feed) EventID() neorpc.EventID {
|
|
|
|
return f.event
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter implements neorpc.EventComparator interface and returns notification filter.
|
2023-12-25 11:14:44 +00:00
|
|
|
func (f feed) Filter() neorpc.SubscriptionFilter {
|
2022-10-17 10:31:24 +00:00
|
|
|
return f.filter
|
|
|
|
}
|
|
|
|
|
2020-05-10 22:00:19 +00:00
|
|
|
const (
|
|
|
|
// Maximum number of subscriptions per one client.
|
|
|
|
maxFeeds = 16
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// This sets notification messages buffer depth. It may seem to be quite
|
2020-05-10 22:00:19 +00:00
|
|
|
// big, but there is a big gap in speed between internal event processing
|
|
|
|
// and networking communication that is combined with spiky nature of our
|
|
|
|
// event generation process, which leads to lots of events generated in
|
2022-04-20 18:30:09 +00:00
|
|
|
// a short time and they will put some pressure to this buffer (consider
|
2020-05-10 22:00:19 +00:00
|
|
|
// ~500 invocation txs in one block with some notifications). At the same
|
2022-04-20 18:30:09 +00:00
|
|
|
// time, this channel is about sending pointers, so it's doesn't cost
|
2020-05-10 22:00:19 +00:00
|
|
|
// a lot in terms of memory used.
|
|
|
|
notificationBufSize = 1024
|
|
|
|
)
|