2012年11月30日 星期五

Stingray Rule中的一進一出

Stingray的rule中有分所謂的Request Rule與Response Rule,而真正的作用是在針對User Request跟Server Response兩件事情增加操作的規則。下面展示兩個Rule中怎麼彼此交換資料來達到某些特定的操作目的:

這邊假設管理人員需要針對伺服器的連線時間過長(在此為5秒)的這個Event增加操作,操作中會去觸發一個HTTP的連線,連線中就可以由開發人員撰寫相關程式來達到通知、記錄、甚至Cloud Scale Out的目的...

此邏輯的設計包含:

  1. Request Rule(RULE_SET_RESP_TIME_FLAG)做進入時間記錄
  2. Response Rule(RULE_SET_RESP_TIME)做回應時間記錄,並操作HTTP連線動作


Rule的原始碼如下:

RULE_SET_RESP_TIME_FLAG.rule
$tm = sys.time(); connection.data.set("start", $tm);


RULE_SET_RESP_TIME.rule

# Response time in (integer) seconds above
$THRESHOLD =5;
# which requests are logged.
$start = connection.data.get("start");
$now = sys.time();
$diff =($now - $start);

if( $diff >= $THRESHOLD ){
   $uri = http.getRawURL();
   $node = connection.getNode();
   log.info ("SLOW REQUEST (". $diff ."s) ". $node .":". $uri );
   # Doing scale out...
   http.request.get("http://your.server.ip.address:300/scaleout" );
}