// wait for sec patient, exit 0 or 1
wait for sec patient, exit 0 or 1
// wait for sec patient exit 0 or 1
func main() {
	flag.Parse()
	received := make(chan int, 1)
	defer close(received)
	h := func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusAccepted)
		fmt.Fprintln(w, "payload received.")
		reqDump, _ := httputil.DumpRequest(r, true)
		log.Printf("incoming request: %s", string(reqDump))
		exitCode := 0
		_, err := resources.Parse(r)
		if err != nil {
			log.Println(err.Error())
			exitCode = 1
		}
		ch := make(chan struct{})
		go func() {
			ch <- struct{}{}
			time.Sleep(time.Second)
			received <- exitCode
		}()
		<-ch
	}
	r := http.NewServeMux()
	r.HandleFunc("/", h)
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
	server := &http.Server{
		Addr:    fmt.Sprintf(":%d", helpers.ValidationReceivePort),
		Handler: r,
	}
	go func() {
		log.Println("start listening to validation report...")
		if err := server.ListenAndServe(); err != http.ErrServerClosed {
			log.Fatalf("failed to wake server up: %v", err)
		}
	}()
	counter := 0
	ticker := time.NewTicker(time.Second)
	for {
		select {
		case <-ticker.C:
			counter += 1
			if counter > patient {
				cancel()
				log.Fatalln("exhausted to wait for validation report.")
			}
		case exitCode := <-received:
			server.Shutdown(ctx)
			cancel()
			os.Exit(exitCode)
		}
	}
}
func main ( ) { flag . Parse ( ) received := make ( chan int , 1 ) defer close ( received ) h := func ( w http . ResponseWriter , r * http . Request ) { w . WriteHeader ( http . StatusAccepted ) fmt . Fprintln ( w , " " ) reqDump , _ := httputil . DumpRequest ( r , true ) log . Printf ( " " , string ( reqDump ) ) exitCode := 0 _ , err := resources . Parse ( r ) if err != nil { log . Println ( err . Error ( ) ) exitCode = 1 } ch := make ( chan struct { } ) go func ( ) { ch <- struct { } { } time . Sleep ( time . Second ) received <- exitCode } ( ) <- ch } r := http . NewServeMux ( ) r . HandleFunc ( " " , h ) ctx , cancel := context . WithTimeout ( context . Background ( ) , time . Second * 5 ) server := & http . Server { Addr : fmt . Sprintf ( " " , helpers . ValidationReceivePort ) , Handler : r , } go func ( ) { log . Println ( " " ) if err := server . ListenAndServe ( ) ; err != http . ErrServerClosed { log . Fatalf ( " " , err ) } } ( ) counter := 0 ticker := time . NewTicker ( time . Second ) for { select { case <- ticker . C : counter += 1 if counter > patient { cancel ( ) log . Fatalln ( " " ) } case exitCode := <- received : server . Shutdown ( ctx ) cancel ( ) os . Exit ( exitCode ) } } }
wait for sec patient, exit 0 or 1
wait for sec patient exit 0 or 1
// ensure the goroutine of the handler has been finished.