Skip to content

Commit 7410a9c

Browse files
authored
Revert "event: move type fixation logic into Feed.init (ethereum#27249)"
This reverts commit b2a95a2.
1 parent 7cc5d3c commit 7410a9c

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

event/feed.go

+22-12
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ func (e feedTypeError) Error() string {
5757
return "event: wrong type in " + e.op + " got " + e.got.String() + ", want " + e.want.String()
5858
}
5959

60-
func (f *Feed) init(etype reflect.Type) {
61-
f.etype = etype
60+
func (f *Feed) init() {
6261
f.removeSub = make(chan interface{})
6362
f.sendLock = make(chan struct{}, 1)
6463
f.sendLock <- struct{}{}
@@ -71,27 +70,36 @@ func (f *Feed) init(etype reflect.Type) {
7170
// The channel should have ample buffer space to avoid blocking other subscribers.
7271
// Slow subscribers are not dropped.
7372
func (f *Feed) Subscribe(channel interface{}) Subscription {
73+
f.once.Do(f.init)
74+
7475
chanval := reflect.ValueOf(channel)
7576
chantyp := chanval.Type()
7677
if chantyp.Kind() != reflect.Chan || chantyp.ChanDir()&reflect.SendDir == 0 {
7778
panic(errBadChannel)
7879
}
7980
sub := &feedSub{feed: f, channel: chanval, err: make(chan error, 1)}
8081

81-
f.once.Do(func() { f.init(chantyp.Elem()) })
82-
if f.etype != chantyp.Elem() {
83-
panic(feedTypeError{op: "Subscribe", got: chantyp, want: reflect.ChanOf(reflect.SendDir, f.etype)})
84-
}
85-
8682
f.mu.Lock()
8783
defer f.mu.Unlock()
84+
if !f.typecheck(chantyp.Elem()) {
85+
panic(feedTypeError{op: "Subscribe", got: chantyp, want: reflect.ChanOf(reflect.SendDir, f.etype)})
86+
}
8887
// Add the select case to the inbox.
8988
// The next Send will add it to f.sendCases.
9089
cas := reflect.SelectCase{Dir: reflect.SelectSend, Chan: chanval}
9190
f.inbox = append(f.inbox, cas)
9291
return sub
9392
}
9493

94+
// note: callers must hold f.mu
95+
func (f *Feed) typecheck(typ reflect.Type) bool {
96+
if f.etype == nil {
97+
f.etype = typ
98+
return true
99+
}
100+
return f.etype == typ
101+
}
102+
95103
func (f *Feed) remove(sub *feedSub) {
96104
// Delete from inbox first, which covers channels
97105
// that have not been added to f.sendCases yet.
@@ -120,17 +128,19 @@ func (f *Feed) remove(sub *feedSub) {
120128
func (f *Feed) Send(value interface{}) (nsent int) {
121129
rvalue := reflect.ValueOf(value)
122130

123-
f.once.Do(func() { f.init(rvalue.Type()) })
124-
if f.etype != rvalue.Type() {
125-
panic(feedTypeError{op: "Send", got: rvalue.Type(), want: f.etype})
126-
}
127-
131+
f.once.Do(f.init)
128132
<-f.sendLock
129133

130134
// Add new cases from the inbox after taking the send lock.
131135
f.mu.Lock()
132136
f.sendCases = append(f.sendCases, f.inbox...)
133137
f.inbox = nil
138+
139+
if !f.typecheck(rvalue.Type()) {
140+
f.sendLock <- struct{}{}
141+
f.mu.Unlock()
142+
panic(feedTypeError{op: "Send", got: rvalue.Type(), want: f.etype})
143+
}
134144
f.mu.Unlock()
135145

136146
// Set the sent value on all channels.

0 commit comments

Comments
 (0)