-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
89 lines (74 loc) · 1.62 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
package main
import (
"fmt"
"log"
"math/rand"
"net/http"
"os"
"strconv"
"sync"
"time"
)
var (
machineID string
filename string = "counter.txt"
mu sync.Mutex
wg sync.WaitGroup
batchSize = 500
)
func saveCounter(counter int){
data := strconv.Itoa(counter)
os.WriteFile(filename, []byte(data), 0644)
}
func loadCounter() int{
data, err := os.ReadFile(filename)
if err != nil{
panic(err)
}
id, err := strconv.Atoi(string(data))
if err != nil{
panic(err)
}
return id
}
// returns batch id's in sets of 500
func getBatchID(prefix string) []string{
ids := make([]string, batchSize)
counterBase := loadCounter()
wg.Add(batchSize)
for i:=0; i<batchSize; i++{
go func(i int){
defer wg.Done()
id := fmt.Sprintf("%s-%s-%d-%d", prefix, machineID, counterBase+i, time.Now().UnixNano())
ids[i] = id
}(i)
}
wg.Wait()
saveCounter(counterBase + batchSize)
return ids
}
// generate random machine ID for each request - simulate multiple
func generateMachineID() string {
return fmt.Sprintf("M%d", rand.Intn(100)+1)
}
func orderHandler(w http.ResponseWriter, r *http.Request){
fmt.Fprintln(w, "NEW ORDER IDS")
ids := getBatchID("orders")
for _, id := range(ids){
fmt.Fprintln(w, id)
}
}
func paymentHandler(w http.ResponseWriter, r *http.Request){
fmt.Fprintln(w, "NEW PAYMENT IDS")
ids := getBatchID("payments")
for _, id := range(ids){
fmt.Fprintln(w, id)
}
}
func main(){
machineID = generateMachineID()
http.HandleFunc("/orders/get_ids", orderHandler)
http.HandleFunc("/payments/get_ids", paymentHandler)
fmt.Println("Starting server on port 8000")
log.Fatal(http.ListenAndServe(":8000", nil))
}