ไลบรารี PHP ที่ใช้กิจวัตร Backoff (ล่าช้า) ต่าง ๆ เช่น backoff แบบเอ็กซ์โปเนนเชียล คลาสสามารถใช้สำหรับการใช้ความล่าช้าประเภทต่างๆ ( เช่นการใช้ sleep() , ความล่าช้าระหว่างคำขอ HTTP ) ดูตัวอย่างการใช้งานที่อธิบายเพิ่มเติมด้านล่าง หากต้องการดำเนินการโทร $class->backoff(); ฟังก์ชั่น backoff() ถูกประกาศว่าเป็นนามธรรมใน BackoffBase และนำไปใช้ในชั้นเรียนเด็กต่างๆ
การหน่วง เวลาย้อนกลับแบบเอ็กซ์โปเนนเชียล ที่มีเลขชี้กำลังเริ่มต้น 2 จะเพิ่มขึ้นในวิธีต่อไปนี้: 1, 2, 4, 16, 256
ความล่าช้า backoff ที่เพิ่มขึ้น ด้วยการเพิ่มขึ้นเริ่มต้น 1 จะเพิ่มขึ้นในวิธีต่อไปนี้: 1, 2, 3, 4, 5, 6, 7
การหน่วง เวลาย้อนกลับแบบทวีคูณ ด้วยเลขชี้กำลังเริ่มต้นของ 2 จะเพิ่มขึ้นในวิธีต่อไปนี้: 0.5, 1, 2, 4, 8, 16, 32, 64
ตัวอย่างการใช้งาน: การใช้อัลกอริทึม backoff ที่ค่อยๆสำรองการโทร API หากพวกเขาส่งคืนรหัสสถานะ "ไม่ว่าง" ดังนั้นเซิร์ฟเวอร์จะไม่ถูกครอบงำโดยคำขอซ้ำ ทุกครั้งที่เซิร์ฟเวอร์ส่งคืนสถานะ "ไม่ว่าง" ความล่าช้าระหว่างการโทร API จะเพิ่มขึ้น
BackoffBase - คลาสพื้นฐานที่คลาส backofflib ทั้งหมดได้รับการสืบทอดมาจากBackoffCaller - ด้วยวัตถุ BackoffBase ที่ระบุ: ใช้การเรียกคืนการเรียกร้องการหน่วงเวลาและฟังก์ชั่นการเรียกกลับแบบออนไฟร์ สามารถใช้เพื่อใช้ backoff อย่างรวดเร็วด้วยรหัสน้อยที่สุดBackoffExponential - backoff แบบเอ็กซ์โปเนนเชียลBackoffExponentialMax - backoff แบบเอ็กซ์โปเนนเชียลด้วยค่า MaxiumumBackoffIncremental - backoff ที่เพิ่มขึ้นBackoffIncrementalMax - backoff ที่เพิ่มขึ้นด้วยค่าสูงสุดBackoffMultiplicative - backoff หลายแบบBackoffRandom - backoff แบบสุ่ม$Backoff->backoff(); - ดำเนินการการใช้งาน backoff$Backoff->getInterval(); - การใช้อัลกอริทึมสำหรับชั้นเรียนที่กำหนด แก้ไข $Backoff->interval$Backoff->getTime(); - ส่งคืนค่าของ backoff (ล่าช้า) $time หลังจากโทร backoff()$Backoff->getIntervalValue(); - ส่งคืนค่าของ $interval สำหรับชั้นเรียน
$BackoffCaller($bo,$cb,$delayCb); - สร้างวัตถุ BackoffCaller ด้วยวัตถุ BackoffBase ที่ระบุ bool function($data); การโทรกลับและ int function($length) หน่วงเวลาการโทรกลับ (ส่งคืน 0 ในความสำเร็จ) ) ดูส่วนหัวไฟล์สำหรับการใช้งานที่อธิบายเพิ่มเติม$BackoffCaller->run(); - เรียกใช้การโทรกลับในลูปจนกว่าจะส่งคืน trueIBackoffMaximum - ดำเนินการโดยคลาสที่มีค่าสูงสุดphpunit ในไดเรกทอรี BackoffLib require ( ' BackoffLib/Backoff.php ' );
$ be = new BackoffLib BackoffIncrementalMax ( 1 , 2 ); //increment by 1, max 2
function beo ( $ b ) {
$ b -> backoff ();
echo "$ b->time = " . $ b -> getTime () . PHP_EOL ;
}
for ( $ i = 1 ; $ i <= 5 ; $ i ++)
beo ( $ be );
echo " be->getCount = " . $ be -> getCount () . PHP_EOL ; require ( ' BackoffLib/Backoff.php ' );
$ be = new BackoffLib BackoffExponential ( 2.0 );
function beo ( $ b , $ backoff = true ) {
if ( $ backoff )
$ b -> backoff ();
echo "$ b->time = " . $ b -> getTime () . PHP_EOL ;
}
beo ( $ be , false );
beo ( $ be );
beo ( $ be );
beo ( $ be );
beo ( $ be );BackoffCaller require_once ( ' BackoffLib/Backoff.php ' );
define ( ' CB_MAX_COUNTER ' , 10 ); //hard limit for number of times the callback fires
define ( ' CB_MAX_DELAY ' , 64 ); //good limit for multiplicative backoff
//callback that fires on each backoff call
$ backoff_callback = function ( $ delayValue ) {
static $ counter = 0 ;
$ counter ++;
if ( is_numeric ( $ delayValue ))
$ delayValue = sprintf ( " %.2f " , $ delayValue ); //truncate the value
//output the call counter and the delay value
echo " [debug] callback # $ {counter}t $ {delayValue} n" ;
if ( isset ( $ delayValue ) && is_numeric ( $ delayValue ))
if ( $ delayValue >= CB_MAX_DELAY )
return true ; //halt backoffCaller execution, the delay has reached its maximum allowed value
if ( $ counter >= CB_MAX_COUNTER )
return true ; //halt execution, the callback counter has reached its maximum allowed value.
return false ; //continue backoffCaller execution: the next delay->backoff calls will occur
};
$ backoff_delay_callback = function ( $ len ) {
sleep ( $ len ); //implement a delay using sleep(): anything could be done here, i.e. limit the max delay.
return 0 ; //return 0 on success: the next backoff call will occur
};
//create a backoff that multiplies the interval by 2 each time it backs off
$ backoffClass = new BackoffLib BackoffMultiplicative ( 2.0 );
//create a caller class, that implements a basic delay-on-fire. the delay is actually handled in the callback.
$ backoff = new BackoffLib BackoffCaller ( $ backoffClass , $ backoff_callback , $ backoff_delay_callback );
$ backoff -> run (); //begin execution: can only be halted by returning TRUE from the callback. BackoffLib เป็นซอฟต์แวร์โอเพ่นซอร์สที่มีอยู่ภายใต้ใบอนุญาต MIT ดูไฟล์ใบอนุญาตสำหรับข้อมูลเพิ่มเติม