Describe EliezerCroitoru/GoLangDelayer here.

   1 /*
   2 license note: http://ngtech.co.il/license/
   3 Copyright (c) 2016, Eliezer Croitoru
   4 All rights reserved.
   5 
   6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
   7 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
   8 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
   9 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  10 
  11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  12 */
  13 package main
  14 
  15 import (
  16         "bufio"
  17         "flag"
  18         "fmt"
  19         //      "log"
  20         "os"
  21         "strings"
  22         "sync"
  23         "time"
  24 )
  25 
  26 //Config vars
  27 var debug *bool
  28 var ans *string
  29 var delay *int
  30 var err error
  31 
  32 func process_request(line string, wg *sync.WaitGroup) {
  33         defer wg.Done()
  34 
  35         lparts := strings.Split(strings.TrimRight(line, "\n"), " ")
  36         if len(lparts[0]) > 0 {
  37                 if *debug {
  38                         fmt.Fprintln(os.Stderr, "ERRlog: Request nubmer => "+lparts[0]+"")
  39                 }
  40         } else {
  41                 return
  42         }
  43 
  44         if *debug {
  45                 fmt.Fprintln(os.Stderr, "ERRlog: request from squid => ")
  46                 fmt.Fprintln(os.Stderr, lparts)
  47         }
  48         time.Sleep(time.Millisecond * time.Duration(*delay))
  49         fmt.Println(lparts[0] + " " + *ans)
  50 }
  51 
  52 func init() {
  53         fmt.Fprintln(os.Stderr, "ERRlog: hello go, running [filter_helper] (probably under squid) :D")
  54 
  55         debug = flag.Bool("debug", false, "Debug mode, use \"1\" to enable.")
  56         ans = flag.String("default_answer", "OK", "Default answer For cases of Errors")
  57         delay = flag.Int("delay", 500, "Default delay in Milliseconds")
  58 
  59         flag.Parse()
  60 
  61         flagsMap := make(map[string]interface{})
  62         flagsMap["debug"] = *debug
  63         flagsMap["default_answer"] = *ans
  64 
  65         fmt.Fprintln(os.Stderr, "ERRlog: Config Variables:")
  66 
  67         for k, v := range flagsMap {
  68                 fmt.Fprintf(os.Stderr, "ERRlog:\t%v =>  %v\n", k, v)
  69         }
  70 
  71 }
  72 
  73 func main() {
  74         /*
  75                 // open a file
  76                 f, err := os.OpenFile("test.log", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
  77                 if err != nil {
  78                         fmt.Printf("error opening file: %v", err)
  79                 }
  80 
  81                 // don't forget to close it
  82                 defer f.Close()
  83 
  84                 // assign it to the standard logger
  85                 log.SetOutput(f)
  86         */
  87         var wg sync.WaitGroup
  88         reader := bufio.NewReader(os.Stdin)
  89         for {
  90                 line, err := reader.ReadString('\n')
  91 
  92                 if err != nil {
  93                         // You may check here if err == io.EOF
  94                         break
  95                 }
  96                 wg.Add(1)
  97                 go process_request(line, &wg)
  98 
  99         }
 100         wg.Wait()
 101 }

EliezerCroitoru/GoLangDelayer (last edited 2016-10-24 18:12:45 by Eliezer Croitoru)