Time-limited invocation method implemented with interface-blog

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

In the real-time requirements of high-performance projects, the time for the implementation of the method has a high demand, this example program, the implementation of a safe method of passing arbitrary parameters time-limited Call tool class, can execute the maximum tolerable runtime of the method, to ensure that the service does not time out.

@description time-out to run the method, limiting the runtime of the method//need to implement the abstract interface Appruning Run method//Call when Timeoutrun (timeout, fun, args ...) Timeout time-out (MS), fun implementation of run () callback method, args ... Call parameter list for method//@author chenbintao//@data2017 -03-2019:00 First draft//2017-03-2019:30 debug via Package mainimport ("Log" "Runtime/debug "Time") const (TIME_OUT_RUN_OK int = 1//run complete Time_out_run_out int = 0//Timeout exit) var (_time_out_run_debug = False) func Setdeb UG (debug_mode bool) {//Set debug mode: True debug mode _time_out_run_debug = Debug_modereturn}func Timeoutrunapp (Timeout UInt64, Callback Func (v ... interface{}), v ... interface{}) {var single chan int = make (chan int, 1)//Open thread run (parameter must be in V ..., this is the full parameter pass, no The []interface{} issue appears] go Timeoutrunapp (single, Safefunction (callback), V ...) Block wait for run result or timeout Select {case <-single://run complete exit if _time_out_run_debug {log. Println ("Run Ok!")} Close (single) Returncase <-time. After (Time.millisecond/*ms*/* time. Duration (Timeout))://Timeout exit if _time_out_run_debug {log. Println ("Run Timeout!")} Close (single) Return}}func Timeoutrunapp (single Chan int, callback func (v ... interface{}) {//interface{}) {//Call execution callback method, after execution completes, write Chancallback (v ...) Single <-Time_out_run_okreturn}func safefunction (fn func (v ... interface{}) func (v. ... interface{}) {// The closure method safely runs return func (v ... interface{}) {defer func () {if e, ok: = Recover (). ( Error); OK {log. Println ("Warn:panic in%v-%v", FN, e) log. Println (String (Debug. Stack ()))}} () fn (v ...)}} ================= test sample Func Run (v ... interface{}) {//For variable type conversion var convert interface{}//first argument convert = V[0]var args0 String = Convert. (string)//The second argument convert = V[1]var args1 string = Convert. (string)//Loop execution, Flag=false: Demo timeout condition, Flag=true: Demo non-timeout condition var count uint32var flag bool = falsefor {time. Sleep (Time.millisecond *) log. Println (Args0, ARGS1, count) count++if flag {Return}}return}func Main () {//Set call Timeout 5s, incoming callback method class app, and related parameters Timeoutrunapp ( Interface, run, "implement a method with timeout implementation:", "count=")}

Operation can be found in http://studygolang.com/topics/2569

431 Reads

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.