mirror of
https://github.com/logos-messaging/go-libp2p-pubsub.git
synced 2026-01-02 12:53:09 +00:00
Fix race when calling Preprocess and msg ID generator(#627)
Closes #624
This commit is contained in:
parent
ae65ce484e
commit
e38c340f93
@ -9,6 +9,7 @@ import (
|
|||||||
// msgIDGenerator handles computing IDs for msgs
|
// msgIDGenerator handles computing IDs for msgs
|
||||||
// It allows setting custom generators(MsgIdFunction) per topic
|
// It allows setting custom generators(MsgIdFunction) per topic
|
||||||
type msgIDGenerator struct {
|
type msgIDGenerator struct {
|
||||||
|
sync.Mutex
|
||||||
Default MsgIdFunction
|
Default MsgIdFunction
|
||||||
|
|
||||||
topicGensLk sync.RWMutex
|
topicGensLk sync.RWMutex
|
||||||
@ -31,6 +32,8 @@ func (m *msgIDGenerator) Set(topic string, gen MsgIdFunction) {
|
|||||||
|
|
||||||
// ID computes ID for the msg or short-circuits with the cached value.
|
// ID computes ID for the msg or short-circuits with the cached value.
|
||||||
func (m *msgIDGenerator) ID(msg *Message) string {
|
func (m *msgIDGenerator) ID(msg *Message) string {
|
||||||
|
m.Lock()
|
||||||
|
defer m.Unlock()
|
||||||
if msg.ID != "" {
|
if msg.ID != "" {
|
||||||
return msg.ID
|
return msg.ID
|
||||||
}
|
}
|
||||||
|
|||||||
4
topic.go
4
topic.go
@ -349,7 +349,9 @@ func (t *Topic) validate(ctx context.Context, data []byte, opts ...PubOpt) (*Mes
|
|||||||
}
|
}
|
||||||
|
|
||||||
msg := &Message{m, "", t.p.host.ID(), pub.validatorData, pub.local}
|
msg := &Message{m, "", t.p.host.ID(), pub.validatorData, pub.local}
|
||||||
t.p.rt.Preprocess(t.p.host.ID(), []*Message{msg})
|
t.p.eval <- func() {
|
||||||
|
t.p.rt.Preprocess(t.p.host.ID(), []*Message{msg})
|
||||||
|
}
|
||||||
err := t.p.val.ValidateLocal(msg)
|
err := t.p.val.ValidateLocal(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user