main.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package main
  2. import (
  3. "bufio"
  4. "context"
  5. "fmt"
  6. "net/http"
  7. "os"
  8. "time"
  9. )
  10. func main() {
  11. baseURL := "https://staging.ntfy.sh"
  12. if len(os.Args) > 1 {
  13. baseURL = os.Args[1]
  14. }
  15. for i := 0; i < 2000; i++ {
  16. go subscribe(i, baseURL)
  17. }
  18. time.Sleep(5 * time.Second)
  19. for i := 0; i < 2000; i++ {
  20. go func(worker int) {
  21. for {
  22. poll(worker, baseURL)
  23. }
  24. }(i)
  25. }
  26. time.Sleep(time.Hour)
  27. }
  28. func subscribe(worker int, baseURL string) {
  29. fmt.Printf("[subscribe] worker=%d STARTING\n", worker)
  30. start := time.Now()
  31. topic, ip := fmt.Sprintf("subtopic%d", worker), fmt.Sprintf("1.2.%d.%d", (worker/255)%255, worker%255)
  32. req, _ := http.NewRequest("GET", fmt.Sprintf("%s/%s/json", baseURL, topic), nil)
  33. req.Header.Set("X-Forwarded-For", ip)
  34. resp, err := http.DefaultClient.Do(req)
  35. if err != nil {
  36. fmt.Printf("[subscribe] worker=%d time=%d error=%s\n", worker, time.Since(start).Milliseconds(), err.Error())
  37. return
  38. }
  39. defer resp.Body.Close()
  40. scanner := bufio.NewScanner(resp.Body)
  41. for scanner.Scan() {
  42. // Do nothing
  43. }
  44. fmt.Printf("[subscribe] worker=%d status=%d time=%d EXITED\n", worker, resp.StatusCode, time.Since(start).Milliseconds())
  45. }
  46. func poll(worker int, baseURL string) {
  47. fmt.Printf("[poll] worker=%d STARTING\n", worker)
  48. topic, ip := fmt.Sprintf("polltopic%d", worker), fmt.Sprintf("1.2.%d.%d", (worker/255)%255, worker%255)
  49. start := time.Now()
  50. ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
  51. defer cancel()
  52. //req, _ := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://staging.ntfy.sh/%s/json?poll=1&since=all", topic), nil)
  53. req, _ := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/%s/json?poll=1&since=all", baseURL, topic), nil)
  54. req.Header.Set("X-Forwarded-For", ip)
  55. resp, err := http.DefaultClient.Do(req)
  56. if err != nil {
  57. fmt.Printf("[poll] worker=%d time=%d status=- error=%s\n", worker, time.Since(start).Milliseconds(), err.Error())
  58. cancel()
  59. return
  60. }
  61. defer resp.Body.Close()
  62. fmt.Printf("[poll] worker=%d time=%d status=%s\n", worker, time.Since(start).Milliseconds(), resp.Status)
  63. }