chctl.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace refill;
  3. use Log;
  4. class chctl
  5. {
  6. protected $mSpeedtable;
  7. static $cache_names = [
  8. ['quality'=> 1,'name' => 'channel-ctl-oil-common-limit'],
  9. ['quality'=> 2,'name' => 'channel-ctl-oil-fast-limit'],
  10. ['quality'=> 1,'name' => 'channel-ctl-phone-common-limit'],
  11. ['quality'=> 2,'name' => 'channel-ctl-phone-fast-limit'],
  12. ['quality'=> 3,'name' => 'channel-ctl-phone-card-limit'],
  13. ['quality'=> 4,'name' => 'channel-ctl-phone-third-limit'],
  14. ['quality'=> 5,'name' => 'channel-ctl-phone-slow-limit'],
  15. ];
  16. public function __construct()
  17. {
  18. $this->mSpeedtable = [];
  19. }
  20. public function load()
  21. {
  22. $this->mSpeedtable = [];
  23. foreach (self::$cache_names as $cache)
  24. {
  25. $quality = $cache['quality'];
  26. $cache_name = $cache['name'];
  27. $data = rcache($cache_name,"provider-");
  28. $data = unserialize($data['data']);
  29. $cfgs = empty($data) ? [] : $data;
  30. foreach ($cfgs as $items)
  31. {
  32. foreach ($items as $item) {
  33. $name = $item['name'];
  34. $amount = $item['amount'];
  35. $card_type = $item['type'];
  36. $opened = $item['opened'] == 1;
  37. $sort = $item['sort'];
  38. $speed = $item['speed'];
  39. $key = $this->prefix($name,$amount,$card_type,$quality);
  40. Log::record("Load: key={$key} name={$name} amount={$amount} type={$card_type} opened={$opened} sort={$sort} speed={$speed}",Log::DEBUG);
  41. $this->mSpeedtable[$key] = new ctl_item($name,$card_type,$amount,$speed,$sort,0,$opened,$quality);
  42. }
  43. }
  44. }
  45. }
  46. public function match($names,int $spec, int $card_type,int $quality)
  47. {
  48. $ctl_items = [];
  49. foreach ($names as $name)
  50. {
  51. $key = $this->prefix($name,$spec,$card_type,$quality);
  52. Log::record("chctl::match key={$key}",Log::DEBUG);
  53. if(array_key_exists($key,$this->mSpeedtable)) {
  54. $ctl_items[] = $this->mSpeedtable[$key];
  55. }
  56. }
  57. //去掉已经关闭通道
  58. $usable_items = [];
  59. foreach ($ctl_items as $item) {
  60. if($item->opened()) {
  61. $usable_items[] = $item;
  62. }
  63. }
  64. //不过载的排在前面
  65. $ascending = function ($l, $r) {
  66. $lproity = $l->priority();
  67. $rproity = $r->priority();
  68. $lover = $l->speed_overload() ? 1 : 0;
  69. $rover = $r->speed_overload() ? 1 : 0;
  70. if($lover == $rover)
  71. {
  72. if($lover) {
  73. return $lproity > $rproity ? -1 : 1; //如果都过载保优先级高的
  74. }
  75. else {
  76. return $lproity < $rproity ? -1 : 1;
  77. }
  78. }
  79. else {
  80. return $lover < $rover ? -1 : 1;
  81. }
  82. };
  83. usort($usable_items, $ascending);
  84. $result = [];
  85. foreach ($usable_items as $item) {
  86. $name = $item->name();
  87. $result[$name] = $item->speed_overload();
  88. }
  89. return $result;
  90. }
  91. private function prefix($name,$spec,$card_type,$quality)
  92. {
  93. return "{$name}-{$spec}-{$card_type}-{$quality}";
  94. }
  95. }