Cloud Native应用交付

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

2个连接数控制的irule

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

Warning: Array to string conversion in /home/wwwroot/myf5/wp-content/plugins/wpjam-basic/public/wpjam-utils.php on line 496
rule HTTP_session_limit {
  when RULE_INIT {
   set ::total_active_clients 0
   set ::max_active_clients 100
   log local0. "rule session_limit initialized: total/max: $::total_active_clients/$::max_active_clients"
  }
  when HTTP_REQUEST {
   ;# test cookie presence
   if {[HTTP::cookie exists "ClientID"]} {
     set need_cookie 0
     set client_id [HTTP::cookie "ClientID"]
     ;# if cookie not present & connection limit not reached, set up client_id
   } else {
     if {$::total_active_clients < $::max_active_clients} {
       set need_cookie 1
       set client_id [format "%08d" [expr { int(100000000 * rand()) }]]
       incr ::total_active_clients
       ;# otherwise redirect
     } else {
       HTTP::redirect "http://sorry.domain.com/"
       return
     }
   }
  }
  when HTTP_RESPONSE {
   ;# insert cookie if needed
   if {$need_cookie == 1} {
     HTTP::cookie insert name "ClientID" value $client_id path "/"
   }
  }
  when CLIENT_CLOSED {
   ;# decrement current connection counter for this client_id
   if {$::total_active_clients > 0} {
     incr ::total_active_clients -1
   }
  }
}
这个rule主要是限制http并发总量,一个连接产生的计数在访问结束时自动被清0,因而可用来计算并发。
当并发总数达到限制后,没有cookie的新建链接则无法正常访问。一次访问只产生一个cookie,只要浏览器不关闭
cookie就没有失效,对这个人而言他后续的点击不再累加到连接数里。看下面日志:
18:40 192.168.162.254 warnings tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接0 
18:40 192.168.162.254 warnings tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接0 
18:39 192.168.162.254 informational 

这是首次打开首页,最终是为0。上面黑色日志,是后来刷新页面的,不产生

计数。

tmm tmm[1045]: Rule test_length: 关闭了减少一个连接,当前0 

18:39 192.168.162.254 warnings tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接1 
18:39 192.168.162.254 warnings tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接1 
18:39 192.168.162.254 warnings tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接1 
18:39 192.168.162.254 warnings tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接1 
18:39 192.168.162.254 warnings tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接1 
18:39 192.168.162.254 warnings tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接1 
18:39 192.168.162.254 warnings tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接1 
分析下面这个代码:
when RULE_INIT {
触发建立一个数组,irule是什么时候被触发??!第一次访问触发,后面再刷新浏览器会触发吗。重新访问会触发吗?
  array set ::active_clients { }
}
when CLIENT_ACCEPTED {
  set client_ip [IP::remote_addr]
  puts "starting client---------$client_ip"
判断,如果数组元素变量::active_clients($client_ip)存在(第一次访问的时候这个数组肯定不存在,没人给这个数组元素赋值)
  if {
} {
    puts "origin connection is ==== $::active_clients($client_ip)"
如果存在,判断这个数组元素变量,如果大于指定的值则拒绝,否则加+1
    if {$::active_clients($client_ip) > 3 } {
      reject
      puts "client connection is reject"
      return
    } else {
      incr ::active_clients($client_ip)
    puts "bynow connection is ==== $::active_clients($client_ip)"
    }
  } else {
   第一次,给数组元素赋值
   puts "client connection is the first one"
    set ::active_clients($client_ip) 1
  }
}
在事件最后,这个总被触发This event is fired at the end of any client connection, regardless of protocol
when CLIENT_CLOSED {
  puts "closing_________[IP::remote_addr]"
  if {
} {
    incr ::active_clients($client_ip) -1
    if { $::active_clients($client_ip) <= 0 } {
      unset ::active_clients($client_ip)
    }
  }
}
这

段代码,是分析一

Array
本作品采用 知识共享署名-非商业性使用 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
    • 我的工作
    • 我的生活
    • 网站技术
    • 路由器技术
    • 项目案例
    标签聚合
    F5 k8s nginx DNS irule neutron network istio docker gtm bigip openstack envoy api flannel
    最近评论
    Abeny 发布于 8 个月前(02月20日) Very good!
    lz 发布于 2 年前(02月26日) 66666666666666
    汤姆 发布于 2 年前(09月10日) 嗨,楼主,里面的json怎么下载啊,怎么收费啊?
    汤姆 发布于 2 年前(09月09日) 大佬,kib的页面可以分享下吗?谢谢
    zhangsha 发布于 2 年前(05月12日) 资料发给我下,谢谢纳米同志!!!!lyx895@qq.com
    浏览次数
    • Downloads - 196,400 views
    • 联系我 - 137,475 views
    • 迄今为止最全最深入的BIGIP-DNS/GTM原理及培训资料 - 127,058 views
    • Github - 115,533 views
    • Sniffer Pro 4 70 530抓包软件 中文版+视频教程 - 91,715 views
    • F5常见log日志解释 - 90,878 views
    • 迄今为止最全最深入的BIGIP-DNS/GTM原理及培训资料 - 89,118 views
    • 从传统ADC迈向CLOUD NATIVE ADC - 下载 - 87,907 views
    • 关于本站 - 76,742 views
    • F5利用Elastic stack(ELK)进行应用数据挖掘系列(2)-DNS - 65,107 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号