def module_pack(txn: Txn, txndata: TxnData): """Output data pack function. It's called after 'module_run' function. the workflow (It would no effect if there is no response needed) @param txn Transaction context which is used for getting shared transaction data or invoking `service.call()` @param data Input data @return How many bytes be consumed """ logger.debug("py module pack") # Increase counters mod_metrics = metrics.module() mod_metrics.response.inc(1) # Assemble response if txn.status() != Txn.TXN_OK: txndata.append('error') else: sharedData = txn.data() logger.debug("pack data: %s" % sharedData.data) logger.info('ModulePack', 'module_pack: data sz: {}'.format( len(sharedData.data))) txndata.append(sharedData.data)
def module_init(config): print ("py module init") Logger.trace('py module init: trace test') Logger.debug('py module init: debug test') Logger.info('1', 'py module init: info test') Logger.warn('2', 'py module init: warn test', 'no suggestion') Logger.error('3', 'py module init: error test', 'no solution') Logger.fatal('4', 'py module init: fatal test', 'no solution') return True
def module_init(config): logger.debug("py module init") logger.info('ModuleInit', 'config: {}'.format(pprint.pformat(config))) global CFG_MIN_TTL global CFG_MAX_TTL global CFG_LOW_LATENCY_BAR global CFG_LATENCY_FACTOR CFG_MIN_TTL = config['min_ttl'] CFG_MAX_TTL = config['max_ttl'] CFG_LOW_LATENCY_BAR = config['low_latency_bar'] CFG_LATENCY_FACTOR = config['latency_factor'] return True
def score(records): scoringTmp = [] # Calculate the latency score according to the http status info for record in records: ip = record.ip ttl = record.ttl httpInfos = record.http_info totalLatency = 0 validRecords = 0 for httpInfo in httpInfos: # status list: # 0: OK # 1: ERROR # 2. TIMEOUT status = httpInfo.status httpCode = httpInfo.httpCode latency = httpInfo.latency if status != 1: totalLatency += latency else: # For error connection record, increase 2000ms totalLatency += 2000 validRecords += 1 # Calculate average latency and build a scoring result avgLatency = 0 if validRecords > 0: avgLatency = int(totalLatency / validRecords) scoringTmp.append( _create_result_item(ip, ttl, avgLatency, validRecords)) # Ranking if logger.isDebugEnabled(): logger.debug("scoringTmp: {}".format(scoringTmp)) scoringResults = sorted(scoringTmp, key=lambda record: record['avgLatency']) if logger.isDebugEnabled(): logger.debug("scoringResults: {}".format(scoringResults)) return scoringResults
def module_run(txn: Txn): """ Module Runnable Entry, be called when this module be picked up in current workflow @param txn Transaction context @return - True if no errors - False if error occurred """ logger.debug("py module run") # Increase counters mod_metrics = metrics.module() mod_metrics.request.inc(1) return True
def _dns_response(txn, iostatus, api_name, request_msg, response_msg): if iostatus != Txn.IO_OK: logger.error( "DNS_E2", "Dns response IO error: {}".format(iostatus), '') return False sharedData = txn.data() for record in response_msg.record: if logger.isDebugEnabled(): logger.debug( "For question: {}, qtype: {}, got ip: {}, ttl: {}".format( sharedData.question, sharedData.qtype, record.ip, record.ttl)) sharedData.record.add(ip=record.ip, ttl=record.ttl) return True
def module_run(txn: Txn): logger.debug("dns_ask module run") sharedData = txn.data() question = sharedData.question question_type = sharedData.qtype dns_query = service_dns_query_req_pto.query_req() dns_query.question = question dns_query.qtype = question_type == 1 and 1 or 2 logger.debug("question: {}, qtype: {}".format(question, question_type)) ret = txn.iocall('dns', 'query', dns_query, api_cb=_dns_response) if ret == Txn.IO_OK: return True else: logger.error( "DNS_E1", "Dns call failed, ret: {}".format(ret), 'Check the parameters') return False
def module_unpack(txn: Txn, data: bytes): """Input data unpack function. It's called if this module is the 'first' module in the workflow and there is input data incoming @param txn Transaction context which is used for getting shared transaction data or invoking `service.call()` @param data Input read-only `bytes` data @return - > 0: Tell engine N bytes be consumed - = 0: Need more data - < 0: Error occurred """ logger.debug('ModuleUnpack: receive data: {}'.format(data.decode('UTF-8'))) # Store data into txn sharedData sharedata = txn.data() sharedata.data = data return len(data)
def module_release(): """Module Release Function, it's called once when module be unloaded""" logger.debug("py module release")
def module_release(): logger.debug("Response module release") return
def module_init(config): logger.debug("Response module init") logger.info('0', 'config: {}'.format(pprint.pformat(config))) return True
def module_release(): logger.debug("dns_ask module release") return
def module_init(config): logger.debug("dns_ask module init") logger.info('ModuleInit', 'config: {}'.format(pprint.pformat(config))) return True
def module_release(): logger.debug("py module release") return