Tpl Dataflow (ITargetBlock) – Part 2


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.

ISourceBlock, ITargetBlock, Tpl Dataflow

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)
Target Block:

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:

Code Snippet
  1. interface ITargetBlock<in TInput> : IDataflowBlock
  2. {
  3.     DataflowMessageStatus OfferMessage(
  4.         DataflowMessageHeader messageHeader,
  5.         TInput messageValue,
  6.         ISourceBlock<TInput> source,
  7.         bool consumeToAccept);
  8. }

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.

Shout it

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>