Advertisement
Negasus

Untitled

Jan 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.50 KB | None | 0 0
  1. main.go
  2. -------
  3. package main
  4.  
  5. import (
  6.     _ "net/http/pprof"
  7.     "fmt"
  8.     "github.com/valyala/fasthttp"
  9.     "time"
  10.     "github.com/Sirupsen/logrus"
  11.     "sync"
  12.     "strings"
  13. )
  14.  
  15. var urls []string
  16.  
  17. var urlsCount = 20
  18. var timeout = time.Millisecond * 700
  19.  
  20. func initUrls() {
  21.  
  22.     for i := 0; i < urlsCount; i++ {
  23.         urls = append(urls, fmt.Sprintf("http://xml.adoptim.com/search?feed=99810&auth=bu64S2&subid=%d&ua=mozilla&url=http://ya.ru&user_ip=3.0.0.0&query=www", i))
  24.     }
  25. }
  26.  
  27. func main() {
  28.  
  29.     initUrls()
  30.  
  31.     clearScreen()
  32.     print(0, 0, strings.Repeat(".", urlsCount - 1), true)
  33.     fmt.Print("\n")
  34.  
  35.     logrus.Info("Listening on 127.0.0.1:8000")
  36.  
  37.     fasthttp.ListenAndServe("127.0.0.1:8000", handler)
  38.  
  39. }
  40.  
  41. func handler(ctx *fasthttp.RequestCtx) {
  42.  
  43.     var wg sync.WaitGroup
  44.  
  45.     wg.Add(urlsCount)
  46.  
  47.     for idx, url := range urls {
  48.         go func(idx int, url string) {
  49.             print(0, idx, "*", false)
  50.  
  51.             defer print(0, idx, ".", false)
  52.             defer wg.Done()
  53.  
  54.             code, _, err := fasthttp.GetTimeout(nil, url, timeout)
  55.             if err != nil {
  56.                 logrus.Error("Response error:", err)
  57.                 return
  58.             }
  59.             if code != 200 {
  60.                 logrus.Error("Response code:", code)
  61.             }
  62.  
  63.         }(idx, url)
  64.     }
  65.  
  66.     wg.Wait()
  67.  
  68.     fmt.Fprint(ctx, "complete")
  69. }
  70.  
  71.  
  72. display.go
  73. ----------
  74. package main
  75.  
  76. import "fmt"
  77.  
  78. func clearScreen() {
  79.     fmt.Print("\033[2J")
  80. }
  81.  
  82. func print(x, y int, message string, clearLine bool) {
  83.  
  84.     s := fmt.Sprintf("\033[%d;%dH", x, y)
  85.  
  86.     fmt.Print()
  87.     if clearLine {
  88.         s = s + fmt.Sprint("\033[K")
  89.     }
  90.     fmt.Print(s + message)
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement