This repository has been archived by the owner on Jan 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
147 lines (123 loc) · 2.99 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"database/sql"
"encoding/json"
"flag"
"fmt"
_ "github.com/lib/pq"
"io"
"log"
"math/rand"
"net/http"
"time"
)
type Index struct {
Path string
Version string
}
type Response struct {
Version string `json:"Version"`
}
func main() {
var (
host = flag.String("host", "localhost", "DB host")
port = flag.Int("port", 5432, "DB port")
user = flag.String("user", "", "DB user")
password = flag.String("password", "", "DB password")
dbname = flag.String("dbname", "", "DB name")
sleep = flag.Int("sleep", 1, "Sleep time in minutes")
workers = flag.Int("workers", 1, "Number of workers")
sleeprmin = flag.Int("sleeprmin", 1, "Sleep time in seconds, after request")
sleeprmax = flag.Int("sleeprmax", 60, "Sleep time in seconds, after request")
)
flag.Parse()
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=disable",
*host, *port, *user, *password, *dbname)
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
log.Fatal(err)
}
defer db.Close()
err = db.Ping()
if err != nil {
log.Fatal(err)
}
var workChan = make(chan Index, *workers)
for i := 0; i < *workers; i++ {
go processor(workChan, *sleeprmin, *sleeprmax)
}
for {
var rows *sql.Rows
rows, err = db.Query(`SELECT path, version FROM public.indexes ORDER BY RANDOM ()`)
if err != nil {
log.Fatal(err)
}
for rows.Next() {
var idx Index
err = rows.Scan(&idx.Path, &idx.Version)
if err != nil {
log.Fatal(err)
}
workChan <- idx
}
err = rows.Close()
if err != nil {
log.Println(err)
}
log.Printf("Sleeping for %d minutes\n", *sleep)
time.Sleep(time.Minute * time.Duration(*sleep))
}
}
func processor(workChan chan Index, minSleep int, maxSleep int) {
for {
idx := <-workChan
process(idx)
// Sleep random seconds between minSleep and maxSleep
sleep := minSleep + rand.Intn(maxSleep-minSleep)
log.Printf("Sleeping for %d seconds\n", sleep)
time.Sleep(time.Second * time.Duration(sleep))
}
}
func process(idx Index) {
log.Printf("Processing %s\n", idx.Path)
var resp *http.Response
var err error
resp, err = http.Get(fmt.Sprintf("http://localhost:3000/%s/@latest", idx.Path))
if err != nil {
log.Println(err)
return
}
var body []byte
body, err = io.ReadAll(resp.Body)
if err != nil {
log.Println(err)
return
}
var r Response
err = json.Unmarshal(body, &r)
if err != nil {
log.Printf("Failed to unmarshal %s (%v)\n", body, err)
return
}
resp.Body.Close()
if r.Version == idx.Version {
return
}
_, err = http.Get(fmt.Sprintf("http://localhost:3000/%s/@v/%s.info", idx.Path, idx.Version))
if err != nil {
log.Println(err)
return
}
_, err = http.Get(fmt.Sprintf("http://localhost:3000/%s/@v/%s.mod", idx.Path, idx.Version))
if err != nil {
log.Println(err)
return
}
_, err = http.Get(fmt.Sprintf("http://localhost:3000/%s/@v/%s.zip", idx.Path, idx.Version))
if err != nil {
log.Println(err)
return
}
log.Printf("Processed %s\n", idx.Path)
}