Lucene.Net.Store.RateLimiter.SimpleRateLimiter.Pause C# (CSharp) Method

Pause() public method

Pauses, if necessary, to keep the instantaneous IO rate at or below the target. NOTE: multiple threads may safely use this, however the implementation is not perfectly thread safe but likely in practice this is harmless (just means in some rare cases the rate might exceed the target). It's best to call this with a biggish count, not one byte at a time.
public Pause ( long bytes ) : long
bytes long
return long
            public override long Pause(long bytes)
            {
                if (bytes == 1)
                {
                    return 0;
                }

                // TODO: this is purely instantaneous rate; maybe we
                // should also offer decayed recent history one?
                long targetNS = LastNS = LastNS + ((long)(bytes * NsPerByte));
                long startNS;
                long curNS = startNS = DateTime.UtcNow.Ticks * 100 /* ns */;
                if (LastNS < curNS)
                {
                    LastNS = curNS;
                }

                // While loop because Thread.sleep doesn't always sleep
                // enough:
                while (true)
                {
                    long pauseNS = targetNS - curNS;
                    if (pauseNS > 0)
                    {
                        try
                        {
                            Thread.Sleep((int)(pauseNS / 1000000));
                        }
                        catch (ThreadInterruptedException ie)
                        {
                            throw new ThreadInterruptedException(ie);
                        }
                        curNS = DateTime.UtcNow.Ticks * 100;
                        continue;
                    }
                    break;
                }
                return curNS - startNS;
            }
        }
RateLimiter.SimpleRateLimiter