Tpl Dataflow (ITargetBlock) – Part 2
in this post will focus on one of the TPL Dataflow main contract called target.
previous post can be found here.
TPL Dataflow is built upon the concept of Producer / Consumer pattern, which represent by 2 interfaces.
ISourceBlock<T> which is the producer and
ITargetBlock<T> which is the consumer.
sources and targets engage in a protocol for transferring messages between them.
this post is focusing on the target (the source will be discus in the following post)
the target block interface defines a single method called OfferMessage which is responsible to decide whether to consume the offered message immediately (push), fetching the message at latter time (pull) or to decline the offer.
the following snippet show the target contract:
for push scenario the source pass the message to the messageValue parameter (line 5).
for pull scenario the target gets a message header (line 4) which contain a message id,
the source parameter (line 6) contain the message’s sender, this way the target can ask for the message at latter time.
the consumeToAccept parameter (line 7) is a way for sources to propagate messages with reduced negotiation.
- false = the target should take the passed-in messageValue (push).
- true = the target should get the message by pulling the source.
the OfferMessage implementation decides whether or not to accept the message and it can project it using the return value.
the return value is a enum which have the following options:
- Accepted: accepted the message and take ownership.
- Declined: decline the offer.
- Postponed: postponed the offer
for potential consumption at a later time.
- NotAvailable: failed to consume the message .
- DecliningPermanently: declined offer and all future offers sent by the source.