Giter Club home page Giter Club logo

flow-control's Introduction

flow-control

流量控制器

实现原理

使用了令牌桶算法:每隔一段时间生成一个令牌,能拿到令牌即可发送

具体算法描述参考:

令牌桶算法

令牌桶算法和漏桶算法以及流量控制浅谈

信号量改进

JDK中的Semaphore有个问题,没有对容量做限制,通过release方法,可以增加permits的数量

Semaphore semaphore = new Semaphore(1);
semaphore.release();
System.out.println(semaphore.availablePermits());
// 输出为2,信号量变多了

实现了一个MaximumLimitedSemaphore,限制信号量的数量最多只能是一开始设定的容量数

protected final boolean tryReleaseShared(int releases) {
	for (; ; ) {
		int current = getState();
		int next = current + releases;

		if (next < current) // overflow
			throw new Error("Maximum permit count exceeded");

		// 如果释放的信号量超过容量,则设定为容量的大小
		if (next > capacity)
			next = capacity;

		if (compareAndSetState(current, next))
			return true;
    }
}

flow-control's People

Contributors

pennywong avatar

Stargazers

mrlonely avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.