Skip to content

Commit

Permalink
add TryPublish() (r3labs#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinran authored Jan 18, 2023
1 parent 2287b9d commit c6d5381
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,24 @@ func (s *Server) Publish(id string, event *Event) {
}
}

// TryPublish is the same as Publish except that when the operation would cause
// the call to be blocked, it simply drops the message and returns false.
// Together with a small BufferSize, it can be useful when publishing the
// latest message ASAP is more important than reliable delivery.
func (s *Server) TryPublish(id string, event *Event) bool {
stream := s.getStream(id)
if stream == nil {
return false
}

select {
case stream.event <- s.process(event):
return true
default:
return false
}
}

func (s *Server) getStream(id string) *Stream {
s.muStreams.RLock()
defer s.muStreams.RUnlock()
Expand Down

0 comments on commit c6d5381

Please sign in to comment.