- 浏览: 2488153 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Improve the Throttle for Akka(1)Local Cache
1 Basic Idea of Akka
Start the Akka System, send messages.
package com.sillycat.throttle.demo
import akka.actor.{Actor, ActorSystem, Props}
class HelloActor extends Actor{
def receive = {
case "hello" => {
println("hello!")
}
case _ => {
println("huh?")
}
}
}
object HelloApp extends App {
val system = ActorSystem("HelloSystem")
system.actorOf(Props[HelloActor], name="test1")
system.actorOf(Props[HelloActor], name="test2")
system.actorOf(Props[HelloActor], name="test3")
// implicit val resolveTimeout = Timeout(5 seconds)
// system.actorSelection("/user/test*").resolveOne().map { helloActor=>
// println(helloActor)
// helloActor ! "hello"
// helloActor ! "bye"
// }
val helloActor = system.actorSelection("/user/test1")
helloActor ! "hello"
helloActor ! "bye"
system.awaitTermination()
}
Simple Example with Router
package com.sillycat.throttle.demo
import akka.actor._
import akka.routing.FromConfig
import com.typesafe.config.ConfigFactory
object RoutingApp extends App{
// A simple actor that prints whatever it receives
class Printer extends Actor {
def receive = {
case x => println(self.path + " saying " + x)
}
}
class Shooter extends Actor {
def receive = {
case x => println(self.path + " shouting " + x)
}
}
val system = ActorSystem("RoutingSystem", ConfigFactory.load())
val router1: ActorRef = system.actorOf(Props[Printer].withRouter(FromConfig()), name = "Router1")
// These three messages will be sent to the printer immediately
router1 ! "11"
router1 ! "12"
router1 ! "13"
// These two will wait at least until 1 second has passed
router1 ! "14"
router1 ! "15"
println(" Router 1 " + router1.path)
val router2: ActorRef = system.actorOf(Props[Shooter].withRouter(FromConfig()), name = "Router2")
router2 ! "21"
router2 ! "22"
val router3: ActorSelection = system.actorSelection("/user/Router2")
router3 ! "23"
println(" Router 2 " + router2.path)
system.shutdown()
}
2 Throttler Based on Local Cache
Try to implement this solution.
//FUNCTION LIMIT_API_CALL(ip)
// ts = CURRENT_UNIX_TIME()
// keyname = ip+":"+ts
// current = GET(keyname)
// IF current != NULL AND current > 10 THEN
// ERROR "too many requests per second"
// ELSE
// MULTI
// INCR(keyname,1)
// EXPIRE(keyname,10)
// EXEC
// PERFORM_API_CALL()
//END
I first build one implementation on top of guava local cache.
package actors.throttle
import akka.actor.{Actor, ActorRef}
import com.sillycat.util.IncludeLogger
import services.LocalCache
import utils.IncludeDateTimeUtil
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
class LocalCacheBasedThrottler(var rate: Rate,var target: ActorRef) extends Actor with IncludeLogger with IncludeDateTimeUtil{
val throttleKey = "THROTTLE_"
def receive = {
case msg: MessageTick => {
val msgKey = msg.key
val realMsg = msg.msg
val counter = msg.count
val limitCalls = rate.numberOfCalls
val timeWindows = rate.duration
val timeKey = convertCurrentTime2Key(timeWindows)
val key = throttleKey + timeKey + msgKey
LocalCache.throttleBucket.getIfPresent(key) match {
case count:java.lang.Integer if count >= limitCalls => {
//delay random and tick self
LocalCache.throttleBucket.put(key, count + counter)
val delay = calculateDelay(count + counter, limitCalls, timeWindows)
//tick to self within the delay
context.system.scheduler.scheduleOnce(delay second, self, msg)
}
case count:java.lang.Integer => {
//count + 1
LocalCache.throttleBucket.put(key, count + counter)
//pass the ticket
target ! realMsg
}
case _ => {
//init the count
LocalCache.throttleBucket.put(key, new Integer(counter))
//pass the ticket
target ! realMsg
}
}
}
case _ => {
logger.error("Received a message I don't understand.")
}
}
}
case class Rate(val numberOfCalls: Int, val duration: Int)
case class MessageTick( key:String, msg:Any, count: Int = 1)
The Local Cache Classes
package services
import java.util.concurrent.TimeUnit
import com.google.common.cache.CacheBuilder
import models.CountStep
object LocalCache {
val builderThrottle = CacheBuilder.newBuilder().expireAfterWrite(60, TimeUnit.SECONDS)
val throttleBucket = builderThrottle.build[java.lang.String, java.lang.Integer]()
val builderMsg = CacheBuilder.newBuilder().expireAfterWrite(30, TimeUnit.MINUTES)
val msgBucket = builderMsg.build[java.lang.String, CountStep]()
}
Here is how to use it.
Akka.system.actorOf(Props(classOf[LocalCacheBasedThrottler],
Rate(4, 15),
contextIOActor),
name = "context-io-throttler")
References:
http://sillycat.iteye.com/blog/2258226
http://letitcrash.com/post/28901663062/throttling-messages-in-akka-2
https://github.com/hbf/akka-throttler
http://sillycat.iteye.com/blog/1553508
http://doc.akka.io/api/akka/2.3.4/index.html#akka.contrib.throttle.TimerBasedThrottler
http://redis.io/commands/incr
http://doc.akka.io/docs/akka/snapshot/scala/routing.html
1 Basic Idea of Akka
Start the Akka System, send messages.
package com.sillycat.throttle.demo
import akka.actor.{Actor, ActorSystem, Props}
class HelloActor extends Actor{
def receive = {
case "hello" => {
println("hello!")
}
case _ => {
println("huh?")
}
}
}
object HelloApp extends App {
val system = ActorSystem("HelloSystem")
system.actorOf(Props[HelloActor], name="test1")
system.actorOf(Props[HelloActor], name="test2")
system.actorOf(Props[HelloActor], name="test3")
// implicit val resolveTimeout = Timeout(5 seconds)
// system.actorSelection("/user/test*").resolveOne().map { helloActor=>
// println(helloActor)
// helloActor ! "hello"
// helloActor ! "bye"
// }
val helloActor = system.actorSelection("/user/test1")
helloActor ! "hello"
helloActor ! "bye"
system.awaitTermination()
}
Simple Example with Router
package com.sillycat.throttle.demo
import akka.actor._
import akka.routing.FromConfig
import com.typesafe.config.ConfigFactory
object RoutingApp extends App{
// A simple actor that prints whatever it receives
class Printer extends Actor {
def receive = {
case x => println(self.path + " saying " + x)
}
}
class Shooter extends Actor {
def receive = {
case x => println(self.path + " shouting " + x)
}
}
val system = ActorSystem("RoutingSystem", ConfigFactory.load())
val router1: ActorRef = system.actorOf(Props[Printer].withRouter(FromConfig()), name = "Router1")
// These three messages will be sent to the printer immediately
router1 ! "11"
router1 ! "12"
router1 ! "13"
// These two will wait at least until 1 second has passed
router1 ! "14"
router1 ! "15"
println(" Router 1 " + router1.path)
val router2: ActorRef = system.actorOf(Props[Shooter].withRouter(FromConfig()), name = "Router2")
router2 ! "21"
router2 ! "22"
val router3: ActorSelection = system.actorSelection("/user/Router2")
router3 ! "23"
println(" Router 2 " + router2.path)
system.shutdown()
}
2 Throttler Based on Local Cache
Try to implement this solution.
//FUNCTION LIMIT_API_CALL(ip)
// ts = CURRENT_UNIX_TIME()
// keyname = ip+":"+ts
// current = GET(keyname)
// IF current != NULL AND current > 10 THEN
// ERROR "too many requests per second"
// ELSE
// MULTI
// INCR(keyname,1)
// EXPIRE(keyname,10)
// EXEC
// PERFORM_API_CALL()
//END
I first build one implementation on top of guava local cache.
package actors.throttle
import akka.actor.{Actor, ActorRef}
import com.sillycat.util.IncludeLogger
import services.LocalCache
import utils.IncludeDateTimeUtil
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
class LocalCacheBasedThrottler(var rate: Rate,var target: ActorRef) extends Actor with IncludeLogger with IncludeDateTimeUtil{
val throttleKey = "THROTTLE_"
def receive = {
case msg: MessageTick => {
val msgKey = msg.key
val realMsg = msg.msg
val counter = msg.count
val limitCalls = rate.numberOfCalls
val timeWindows = rate.duration
val timeKey = convertCurrentTime2Key(timeWindows)
val key = throttleKey + timeKey + msgKey
LocalCache.throttleBucket.getIfPresent(key) match {
case count:java.lang.Integer if count >= limitCalls => {
//delay random and tick self
LocalCache.throttleBucket.put(key, count + counter)
val delay = calculateDelay(count + counter, limitCalls, timeWindows)
//tick to self within the delay
context.system.scheduler.scheduleOnce(delay second, self, msg)
}
case count:java.lang.Integer => {
//count + 1
LocalCache.throttleBucket.put(key, count + counter)
//pass the ticket
target ! realMsg
}
case _ => {
//init the count
LocalCache.throttleBucket.put(key, new Integer(counter))
//pass the ticket
target ! realMsg
}
}
}
case _ => {
logger.error("Received a message I don't understand.")
}
}
}
case class Rate(val numberOfCalls: Int, val duration: Int)
case class MessageTick( key:String, msg:Any, count: Int = 1)
The Local Cache Classes
package services
import java.util.concurrent.TimeUnit
import com.google.common.cache.CacheBuilder
import models.CountStep
object LocalCache {
val builderThrottle = CacheBuilder.newBuilder().expireAfterWrite(60, TimeUnit.SECONDS)
val throttleBucket = builderThrottle.build[java.lang.String, java.lang.Integer]()
val builderMsg = CacheBuilder.newBuilder().expireAfterWrite(30, TimeUnit.MINUTES)
val msgBucket = builderMsg.build[java.lang.String, CountStep]()
}
Here is how to use it.
Akka.system.actorOf(Props(classOf[LocalCacheBasedThrottler],
Rate(4, 15),
contextIOActor),
name = "context-io-throttler")
References:
http://sillycat.iteye.com/blog/2258226
http://letitcrash.com/post/28901663062/throttling-messages-in-akka-2
https://github.com/hbf/akka-throttler
http://sillycat.iteye.com/blog/1553508
http://doc.akka.io/api/akka/2.3.4/index.html#akka.contrib.throttle.TimerBasedThrottler
http://redis.io/commands/incr
http://doc.akka.io/docs/akka/snapshot/scala/routing.html
发表评论
-
Stop Update Here
2020-04-28 09:00 264I will stop update here, and mo ... -
NodeJS12 and Zlib
2020-04-01 07:44 433NodeJS12 and Zlib It works as ... -
Docker Swarm 2020(2)Docker Swarm and Portainer
2020-03-31 23:18 313Docker Swarm 2020(2)Docker Swar ... -
Docker Swarm 2020(1)Simply Install and Use Swarm
2020-03-31 07:58 323Docker Swarm 2020(1)Simply Inst ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 294Traefik 2020(1)Introduction and ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 381Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 378Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 329Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 403VPN Server 2020(2)Docker on Cen ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 337Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 419NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 364Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 294Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 211GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 393GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 278GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 266Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 266Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 254Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 240Serverless with NodeJS and Tenc ...
相关推荐
derived for controlling the throttle plate dynamics. The adaptive control law provides an on-line estimate of the friction, which can identify a throttle body with excessive friction. Besides the ...
ThrottleStop 免费版,尽情使用,永不过期!ThrottleStop 让电脑为性能发烧锁定主频,气绝降频,更可以调节至尊系列处理器的倍频、电压、外频,ThrottleStop 在手,强悍恒持久!
throttlestop是电脑cpu性能检测软件。throttlestop主要是为用户实时监测cpu运行性能,并解决笔记玩游戏时温度一高就降频运行迟钝歇菜难题,让玩家能尽情畅玩游戏。 throttlestop功能介绍: 1、用于笔记本调节cpu; 2...
ThrottleStop_9.5 笔记本处理器intel超频软件
ThrottleStop是一款能让你的Intel Core i或者Core 2处理器运行更为流畅的应用程序。此程序提供4种预设配置文件,提高你电脑运行的体验。 “Performance”模式可以提高一般性能,“Game”模式可为游戏者完善功能,...
BeamNG.drive Remote Control Remote Control app for the PC game BeamNG.drive. Communication functionality ...Throttle 1 for pushed otherwise 0 Breaks 1 for pushed otherwise 0 App needs
上传资源的目的是因为之前下载的ThrottleStop8.40版本过期了(打开后提示This beta version has now expired.),内网也没有新版,寻找到这个最新版本,上传后以便大家使用。 保证原版资源无任何修改添加。 throttle...
今天小编要给大家介绍一款专业好用的网络加速软件——Throttle。Throttle的特点是不用查找用户所用的路由或猫的型号,只要输入连网的方式和操作系统,软件即会自动实现网络加速功能。软件功能强大,支持4.4/28.8/...
免除硬超烦恼,好评如潮的软超软件。Throttlestop_CPU鸡血
官方原版ThrottleStop_8.70.6 笔记本cpu超频降频睿频工具
ThrottleStop 8.50版本是Intel Core 2和Core i系列CPU的性能监视和修改工具。它可能以“不计代价”的方式最大化发挥CPU的性能,并允许系统忽视温度的影响。
ThrottleStop是一个酷睿2/酷睿i处理器性能调整工具,以监测和正确的CPU节流,许多笔记本电脑上正在使用的3个主要。左侧的ThrottleStop包含多种选项可以用来绕过CPU节流
解决CPU过热降频问题 throttlestop功能介绍: 1、非常使用的cup功能。 2、能够很好的帮助用户解决cpu问题。 3、可设置cpu温度。
ThrottleStop6.0汉化版一款拥有强大的 CPU硬件控制功能的软件,分享给需要的朋友!!!
上传资源的目的是因为之前下载的ThrottleStop8.10beta2版本过期了(打开后提示This beta version has now expired.),内网也没有新版,寻找到这个最新版本,上传后以便大家使用。 throttlestop是款防cpu降频软件。...
前端项目-jquery-throttle-debounce,jquery throttle/debounce允许您以多种有用的方式对函数进行速率限制。
throttlestop6汉化版是一款笔记本电脑CPU频率调节软件;是市面上非常优秀的一款笔记本cpu调频软件,界面简洁、操作方便,能绕过BD PROCHOT技术,让CPU稳定运行在默认频率或者之上,具有监视CPU状态以及锁定CPU频率等...
hrottlestop是款专业的、强大的、易用的cpu性能测试软件。throttlestop使用简单,功能实用,能够很好的帮助用户实时监测cpu的运行...1、非常使用的cup功能。 2、能够很好的帮助用户解决cpu问题。 3、可设置cpu温度。
ThrottleStop_9.4 笔记本处理器intel超频软件
ThrottleStop8.48是当前最新版本,可以解决8.40版本在使用时提示This beta version has now expired的问题。throttlestop用于防止cpu降频运行,解决电脑cpu达不到最高性能的问题。现在的CPU很智能化,当温度...