public Announcement Read(ReadAnnouncement request) { using (var connection = Connections.ConnectToInitializedTopic(request.TopicName)) { var topicSubscriber = connection.FirstOrDefault <Subscriber>(subscriber => subscriber.Id == request.SubscriberId); if (topicSubscriber == null) { throw new ArgumentException("Unknown subscriber"); } var announcement = ReadFromDb(connection, topicSubscriber, request); return(announcement); } }
Announcement ReadFromDb(IDbConnection connection, Subscriber subscriber, ReadAnnouncement request) { var topicLock = Locks.TakeTopicLock(request.TopicName); lock (topicLock) { var query = NextAnnouncement.make(connection, subscriber); var announcement = connection.FirstOrDefault(query); while (announcement == null) { Monitor.Wait(topicLock); announcement = connection.FirstOrDefault(query); } return(announcement); } }