Skip to content

Commit c6d5381

Browse files
authored
add TryPublish() (#144)
1 parent 2287b9d commit c6d5381

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

server.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,24 @@ func (s *Server) Publish(id string, event *Event) {
122122
}
123123
}
124124

125+
// TryPublish is the same as Publish except that when the operation would cause
126+
// the call to be blocked, it simply drops the message and returns false.
127+
// Together with a small BufferSize, it can be useful when publishing the
128+
// latest message ASAP is more important than reliable delivery.
129+
func (s *Server) TryPublish(id string, event *Event) bool {
130+
stream := s.getStream(id)
131+
if stream == nil {
132+
return false
133+
}
134+
135+
select {
136+
case stream.event <- s.process(event):
137+
return true
138+
default:
139+
return false
140+
}
141+
}
142+
125143
func (s *Server) getStream(id string) *Stream {
126144
s.muStreams.RLock()
127145
defer s.muStreams.RUnlock()

0 commit comments

Comments
 (0)