Access Blackborad With Timeout
Operations that access a blackboard to retrieve information, i.e. operations read and take, have a second parameter that defines how long should this operation wait until there will be a matching n-tuple or, simply, timeout. A value of the parameter specifies number of second.
- To read a tuple and if there is no such tuple, to wait 4 seconds for it:
ts.read [nil, nil], 4
- Or to take a tuple and if there is no such tuple, to wait 1 minute for it:
ts.take [nil, nil], 60
There are two special values that you may use as a timeout value:
- 0 (zero) - an operation would not wait at all. It is useful for checking, if a n-tuple is stored in the blackboard or not.
- nil - an operation would wait endlessly. Of course, there is not difference if you would call an operation without a specified timeout value or with nil.
Actually, to be more precise, an operation would wait 231 - 1 seconds, that is approximately 30 years. ;)
But what would happen, if we specify a timeout value and an n-tuple would not appear on the blackboard in the specified time(out)?
Rinda throws the Rinda::RequestExpiredError exception.
The handling exceptions in Ruby is very comfortable (esp. with the command retry). An example follows, where a tuple is taken with a timeout 5 seconds. This allows to inform an user about the state, that the application is still waiting for a message:
puts "Waiting for a message." begin t = ts.take [:message, nil], 5 rescue Rinda::RequestExpiredError puts "Still waiting for a message." retry end
The timeout functionality allows to:
- create a responsiveness application - to inform other components or users about its state (e.g. waiting for a message - processing a message).
- end an application normally - to break waiting for a n-tuple every so often and check if the application should end.
- check for different n-tuples - it is possible to wait in deferent intervals for different n-tuples. Of course, a better solution is to create for each n-tuple an own thread that performs a blackboard access operation for this particular n-tuple.
- create an application that does not access a blackboard all the time (e.g. only every hour for 1 minute).