in src/ReceiverLink.cs [148:207]
public void SetCredit(int credit, CreditMode creditMode, int flowThreshold = -1)
{
lock (this.ThisLock)
{
if (this.IsDetaching)
{
return;
}
if (this.totalCredit < 0)
{
this.totalCredit = 0;
}
var sendFlow = false;
if (creditMode == CreditMode.Drain)
{
if (!this.drain)
{
// start a drain cycle.
this.pending = 0;
this.restored = 0;
this.drain = true;
this.credit = credit;
this.flowThreshold = -1;
sendFlow = true;
}
}
else if (creditMode == CreditMode.Manual)
{
this.drain = false;
this.pending = 0;
this.restored = 0;
this.flowThreshold = -1;
this.credit += credit;
sendFlow = true;
}
else
{
this.drain = false;
this.flowThreshold = flowThreshold >= 0 ? flowThreshold : credit / 2;
// Only change remaining credit if total credit was increased, to allow
// accepting incoming messages. If total credit is reduced, only update
// total so credit will be later auto-restored to the new limit.
int delta = credit - this.totalCredit + this.restored;
if (delta > 0)
{
this.credit += delta;
this.restored = 0;
sendFlow = true;
}
}
this.totalCredit = credit;
if (sendFlow)
{
this.SendFlow(this.deliveryCount, (uint)this.credit, this.drain);
}
}
}