mirror of
https://github.com/logos-messaging/go-libp2p-pubsub.git
synced 2026-02-05 13:13:07 +00:00
- make validators time out after 100ms - add context param to validator functions - add type Validator func(context.Context, *Message) bool - drop message if more than 10 messages are already being validated
35 lines
516 B
Go
35 lines
516 B
Go
package floodsub
|
|
|
|
import (
|
|
"context"
|
|
)
|
|
|
|
type Subscription struct {
|
|
topic string
|
|
ch chan *Message
|
|
cancelCh chan<- *Subscription
|
|
err error
|
|
validate Validator
|
|
}
|
|
|
|
func (sub *Subscription) Topic() string {
|
|
return sub.topic
|
|
}
|
|
|
|
func (sub *Subscription) Next(ctx context.Context) (*Message, error) {
|
|
select {
|
|
case msg, ok := <-sub.ch:
|
|
if !ok {
|
|
return msg, sub.err
|
|
}
|
|
|
|
return msg, nil
|
|
case <-ctx.Done():
|
|
return nil, ctx.Err()
|
|
}
|
|
}
|
|
|
|
func (sub *Subscription) Cancel() {
|
|
sub.cancelCh <- sub
|
|
}
|