chctl.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. {
  61. if($item->opened()) {
  62. $usable_items[] = $item;
  63. }
  64. }
  65. //不过载的排在前面
  66. $ascending = function ($l, $r)
  67. {
  68. $lproity = $l->priority();
  69. $rproity = $r->priority();
  70. $lover = $l->speed_overload() ? 1 : 0;
  71. $rover = $r->speed_overload() ? 1 : 0;
  72. if($lover == $rover)
  73. {
  74. if($lover) {
  75. return $lproity > $rproity ? -1 : 1; //如果都过载保优先级高的
  76. }
  77. else {
  78. return $lproity < $rproity ? -1 : 1;
  79. }
  80. }
  81. else {
  82. return $lover < $rover ? -1 : 1;
  83. }
  84. };
  85. usort($usable_items, $ascending);
  86. $result = [];
  87. foreach ($usable_items as $item) {
  88. $name = $item->name();
  89. $result[$name] = $item->speed_overload();
  90. }
  91. return $result;
  92. }
  93. private function prefix($name,$spec,$card_type,$quality)
  94. {
  95. return "{$name}-{$spec}-{$card_type}-{$quality}";
  96. }
  97. }