Cloud Native应用交付

  • 首页
  • 关于本站
  • 个人介绍
  • Downloads
  • Repo
    • Github
    • Container
  • F5
    • F5 Python SDK
    • F5-container
    • F5-LBaaS
  • 社交
    • 联系我
    • 微信/微博
    • 公众号
    • 打赏赞助
行至水穷处 坐看云起时
Cloud Native Application Services: cnadn.net
  1. 首页
  2. F5技术
  3. 正文

2个连接数控制的irule

2008年03月29日 7612点热度 1人点赞 0条评论

1
rule HTTP_session_limit {<br />&nbsp; when RULE_INIT {<br />&nbsp;&nbsp; set ::total_active_clients 0<br />&nbsp;&nbsp; set ::max_active_clients 100<br />&nbsp;&nbsp; log local0. &quot;rule session_limit initialized: total/max: $::total_active_clients/$::max_active_clients&quot;<br />&nbsp; }<br />&nbsp; when HTTP_REQUEST {<br />&nbsp;&nbsp; ;# test cookie presence<br />&nbsp;&nbsp; if {[HTTP::cookie exists &quot;ClientID&quot;]} {<br />&nbsp;&nbsp;&nbsp;&nbsp; set need_cookie 0<br />&nbsp;&nbsp;&nbsp;&nbsp; set client_id [HTTP::cookie &quot;ClientID&quot;]<br />&nbsp;&nbsp;&nbsp;&nbsp; ;# if cookie not present &amp; connection limit not reached, set up client_id<br />&nbsp;&nbsp; } else {<br />&nbsp;&nbsp;&nbsp;&nbsp; if {$::total_active_clients &lt; $::max_active_clients} {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set need_cookie 1<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set client_id [format &quot;%08d&quot; [expr { int(100000000 * rand()) }]]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; incr ::total_active_clients<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;# otherwise redirect<br />&nbsp;&nbsp;&nbsp;&nbsp; } else {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HTTP::redirect &quot;http://sorry.domain.com/&quot;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return<br />&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp; }<br />&nbsp; }<br />&nbsp; when HTTP_RESPONSE {<br />&nbsp;&nbsp; ;# insert cookie if needed<br />&nbsp;&nbsp; if {$need_cookie == 1} {<br />&nbsp;&nbsp;&nbsp;&nbsp; HTTP::cookie insert name &quot;ClientID&quot; value $client_id path &quot;/&quot;<br />&nbsp;&nbsp; }<br />&nbsp; }<br />&nbsp; when CLIENT_CLOSED {<br />&nbsp;&nbsp; ;# decrement current connection counter for this client_id<br />&nbsp;&nbsp; if {$::total_active_clients &gt; 0} {<br />&nbsp;&nbsp;&nbsp;&nbsp; incr ::total_active_clients -1<br />&nbsp;&nbsp; }<br />&nbsp; }<br /> }<br />这个rule主要是限制http<strong>并发</strong>总量,一个连接产生的计数在访问结束时自动被清0,因而可用来计算并发。

1
当并发总数达到限制后,没有cookie的新建链接则无法正常访问。一次访问只产生一个cookie,只要浏览器不关闭

1
cookie就没有失效,对这个人而言他后续的点击不再累加到连接数里。看下面日志:

1
<table cellspacing="0" cellpadding="2" width="100%"><tbody><tr><td class="Severity4">18:40&nbsp;</td><td class="Severity4">192.168.162.254&nbsp;</td><td class="Severity4" nowrap="nowrap">warnings&nbsp;</td><td class="Severity4">tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接0&nbsp;</td></tr><tr><td class="Severity4">18:40&nbsp;</td><td class="Severity4">192.168.162.254&nbsp;</td><td class="Severity4" nowrap="nowrap">warnings&nbsp;</td><td class="Severity4">tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接0&nbsp;</td></tr><tr><td class="Severity6"><font color="#ff0000">18:39&nbsp;</font></td><td class="Severity6"><font color="#ff0000">192.168.162.254&nbsp;</font></td><td class="Severity6" nowrap="nowrap"><font color="#ff0000">informational&nbsp;</font></td><td class="Severity6"><p><font color="#ff0000">这是首次打开首页,最终是为0。上面黑色日志,是后来刷新页面的,不产生</font></p><p><font color="#ff0000">计数。</font></p><p><font color="#ff0000">tmm tmm[1045]: Rule test_length: 关闭了减少一个连接,当前0&nbsp;</font></p></td></tr><tr><td class="Severity4"><font color="#ff0000">18:39&nbsp;</font></td><td class="Severity4"><font color="#ff0000">192.168.162.254&nbsp;</font></td><td class="Severity4" nowrap="nowrap"><font color="#ff0000">warnings&nbsp;</font></td><td class="Severity4"><font color="#ff0000">tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接1&nbsp;</font></td></tr><tr><td class="Severity4"><font color="#ff0000">18:39&nbsp;</font></td><td class="Severity4"><font color="#ff0000">192.168.162.254&nbsp;</font></td><td class="Severity4" nowrap="nowrap"><font color="#ff0000">warnings&nbsp;</font></td><td class="Severity4"><font color="#ff0000">tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接1&nbsp;</font></td></tr><tr><td class="Severity4"><font color="#ff0000">18:39&nbsp;</font></td><td class="Severity4"><font color="#ff0000">192.168.162.254&nbsp;</font></td><td class="Severity4" nowrap="nowrap"><font color="#ff0000">warnings&nbsp;</font></td><td class="Severity4"><font color="#ff0000">tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接1&nbsp;</font></td></tr><tr><td class="Severity4"><font color="#ff0000">18:39&nbsp;</font></td><td class="Severity4"><font color="#ff0000">192.168.162.254&nbsp;</font></td><td class="Severity4" nowrap="nowrap"><font color="#ff0000">warnings&nbsp;</font></td><td class="Severity4"><font color="#ff0000">tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接1&nbsp;</font></td></tr><tr><td class="Severity4"><font color="#ff0000">18:39&nbsp;</font></td><td class="Severity4"><font color="#ff0000">192.168.162.254&nbsp;</font></td><td class="Severity4" nowrap="nowrap"><font color="#ff0000">warnings&nbsp;</font></td><td class="Severity4"><font color="#ff0000">tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接1&nbsp;</font></td></tr><tr><td class="Severity4"><font color="#ff0000">18:39&nbsp;</font></td><td class="Severity4"><font color="#ff0000">192.168.162.254&nbsp;</font></td><td class="Severity4" nowrap="nowrap"><font color="#ff0000">warnings&nbsp;</font></td><td class="Severity4"><font color="#ff0000">tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接1&nbsp;</font></td></tr><tr><td class="Severity4"><font color="#ff0000">18:39&nbsp;</font></td><td class="Severity4"><font color="#ff0000">192.168.162.254&nbsp;</font></td><td class="Severity4" nowrap="nowrap"><font color="#ff0000">warnings&nbsp;</font></td><td class="Severity4"><font color="#ff0000">tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接1</font>&nbsp;</td></tr></tbody></table>

1
<strong>分析下面这个代码:</strong>

1
when RULE_INIT {

1
触发建立一个数组,irule是什么时候被触发??!第一次访问触发,后面再刷新浏览器会触发吗。重新访问会触发吗?<br />&nbsp; array set ::active_clients { }<br />}<br />when CLIENT_ACCEPTED {<br />&nbsp; set client_ip [IP::remote_addr]<br />&nbsp; puts &quot;starting client---------$client_ip&quot;

1
判断,如果数组元素变量::active_clients($client_ip)存在(第一次访问的时候这个数组肯定不存在,没人给这个数组元素赋值)<br />&nbsp; if { [info exists ::active_clients($client_ip)] } {<br />&nbsp;&nbsp;&nbsp; puts &quot;origin connection is ==== $::active_clients($client_ip)&quot;

1
如果存在,判断这个数组元素变量,如果大于指定的值则拒绝,否则加+1<br />&nbsp;&nbsp;&nbsp; if {$::active_clients($client_ip) &gt; 3 } {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; reject<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; puts &quot;client connection is reject&quot;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return<br />&nbsp;&nbsp;&nbsp; } else {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; incr ::active_clients($client_ip)<br />&nbsp;&nbsp;&nbsp; puts &quot;bynow connection is ==== $::active_clients($client_ip)&quot;<br />&nbsp;&nbsp;&nbsp; }<br />&nbsp; } else {

1
   第一次,给数组元素赋值<br />&nbsp;&nbsp; puts &quot;client connection is the first one&quot;<br />&nbsp;&nbsp;&nbsp; set ::active_clients($client_ip) 1<br />&nbsp; }<br />}

1
在事件最后,这个总被触发<strong>This event is fired at the end of any client connection, regardless of protocol<br /></strong>when CLIENT_CLOSED {<br />&nbsp; puts &quot;closing_________[IP::remote_addr]&quot;<br />&nbsp; if { [info exists ::active_clients($client_ip)] } {<br />&nbsp;&nbsp;&nbsp; incr ::active_clients($client_ip) -1<br />&nbsp;&nbsp;&nbsp; if { $::active_clients($client_ip) &lt;= 0 } {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unset ::active_clients($client_ip)<br />&nbsp;&nbsp;&nbsp; }<br />&nbsp; }<br />}<br />

这

段代码,是分析一

相关文章

  • 让VS直接访问另一个VS
  • [irule]LB_SELECTED下的command:LB::server
  • LTM,interface failsafe!
  • [irule]LB::status
  • tcl 操作
本作品采用 知识共享署名-非商业性使用 4.0 国际许可协议 进行许可
标签: irule
最后更新:2008年03月29日

纳米

linjing.io

打赏 点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理。

页面AI聊天助手

纳米

linjing.io

☁️迈向Cloud Native ADC ☁️

认证获得:
TOGAF: ID 152743
Kubernetes: CKA #664
Microsoft: MCSE MCDBA
Cisco: CCNP
Juniper: JNCIS
F5:
F5 Certified Solution Expert, Security
F5 Certified Technology Specialist, LTM/GTM/APM/ASM
F5 Certified BIG-IP Administrator
  • 点击查看本博技术要素列表
  • 归档
    分类
    • AI
    • Automation
    • Avi Networks
    • Cisco ACI
    • CISCO资源
    • F5 with ELK
    • F5-Tech tips
    • F5技术
    • Juniper
    • Linux
    • NGINX
    • SDN
    • ServiceMesh
    • WEB编程
    • WINDOWS相关
    • 业界文章
    • 交换机技术
    • 化云为雨/Openstack
    • 协议原理
    • 容器/k8s
    • 我的工作
    • 我的生活
    • 网站技术
    • 路由器技术
    • 项目案例
    标签聚合
    openstack k8s api istio irule nginx F5 flannel docker bigip gtm DNS network neutron envoy
    最近评论
    汤姆 发布于 8 个月前(09月10日) 嗨,楼主,里面的json怎么下载啊,怎么收费啊?
    汤姆 发布于 8 个月前(09月09日) 大佬,kib的页面可以分享下吗?谢谢
    zhangsha 发布于 1 年前(05月12日) 资料发给我下,谢谢纳米同志!!!!lyx895@qq.com
    李成才 发布于 1 年前(01月02日) 麻烦了,谢谢大佬
    纳米 发布于 1 年前(01月02日) 你好。是的,因为以前下载系统插件在一次升级后将所有的下载生成信息全弄丢了。所以不少文件无法下载。DN...
    浏览次数
    • Downloads - 183,756 views
    • 联系我 - 118,966 views
    • 迄今为止最全最深入的BIGIP-DNS/GTM原理及培训资料 - 116,491 views
    • Github - 103,645 views
    • F5常见log日志解释 - 79,768 views
    • 从传统ADC迈向CLOUD NATIVE ADC - 下载 - 74,619 views
    • Sniffer Pro 4 70 530抓包软件 中文版+视频教程 - 74,320 views
    • 迄今为止最全最深入的BIGIP-DNS/GTM原理及培训资料 - 67,770 views
    • 关于本站 - 60,893 views
    • 这篇文档您是否感兴趣 - 55,491 views
    链接表
    • F5SE创新
    • Jimmy Song‘s Blog
    • SDNlab
    • Service Mesh社区
    • 三斗室
    • 个人profile
    • 云原生社区

    COPYRIGHT © 2023 Cloud Native 应用交付. ALL RIGHTS RESERVED.

    Theme Kratos Made By Seaton Jiang

    京ICP备14048088号-1

    京公网安备 11010502041506号