-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
152 lines (128 loc) · 3.43 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
148
149
150
151
152
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"sync"
"time"
"os"
"github.com/segmentio/ksuid"
)
type config struct {
Servers []string
PpS int
Port string
MetricsFile string
}
type free struct {
M sync.Mutex
Urls []string
}
type clients struct {
M sync.Mutex
Uuids map[string]*client
}
type client struct {
ID string
LastAccess time.Time
Link string
}
var conf config
var linklist []string
var c clients
var serverIdx int
var metricsFile *os.File
var waitingClient *client
// check if client at waiting slot is still active. Otherwise delete it.
func cleanUp() {
if waitingClient == nil {
return
}
now := time.Now()
timeout, _ := time.ParseDuration("10s")
if now.Sub(waitingClient.LastAccess) > timeout {
// delete from client list
delete(c.Uuids, waitingClient.ID)
// free waiting slot
waitingClient = nil
}
}
func tryToPair(currentClient *client) {
// delete waiting client if not active any longer
cleanUp()
if waitingClient != nil {
// do not pair client with itself
if waitingClient.ID == currentClient.ID {
return
}
serverIdx += 1
idx := serverIdx % len(conf.Servers)
server := conf.Servers[idx]
fmt.Fprintf(metricsFile, "%d\n", time.Now().UnixNano())
link := fmt.Sprintf("%s/%s", server, currentClient.ID)
waitingClient.Link = link
currentClient.Link = link
waitingClient = nil
} else {
waitingClient = currentClient
}
}
func handleRegister(w http.ResponseWriter, r *http.Request) {
c.M.Lock()
uuid := ksuid.New().String()
log.Printf("uuid registered: %s", uuid)
currentClient := client{ID: uuid, LastAccess: time.Now(), Link: ""}
c.Uuids[uuid] = ¤tClient
tryToPair(¤tClient)
fmt.Fprintf(w, uuid)
c.M.Unlock()
}
func handlePoll(w http.ResponseWriter, r *http.Request) {
c.M.Lock()
uuid := r.FormValue("uuid")
currentClient := c.Uuids[uuid]
if currentClient == nil {
log.Printf("uuid %s not registered", uuid)
fmt.Fprintf(w, "nouuid")
c.M.Unlock()
return
}
// look if paired
if currentClient.Link != "" {
fmt.Fprintf(w, currentClient.Link)
log.Printf("%s assigned to %s", currentClient.Link, uuid)
} else {
// find pair
tryToPair(currentClient)
// if pairing succesfull, directly print link
if currentClient.Link != "" {
fmt.Fprintf(w, currentClient.Link)
log.Printf("%s assigned to %s", currentClient.Link, uuid)
} else {
fmt.Fprintf(w, "wait")
}
}
c.M.Unlock()
}
func main() {
//load url list
data, err := ioutil.ReadFile("config.json")
if err != nil {
panic(err)
}
json.Unmarshal(data, &conf)
waitingClient = nil
c.Uuids = make(map[string]*client)
metricsFile, err = os.OpenFile(conf.MetricsFile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
log.Fatalf("error opening metrics file: %v", err)
}
defer metricsFile.Close()
http.Handle("/", http.FileServer(http.Dir("static")))
http.HandleFunc("/api/register", handleRegister)
http.HandleFunc("/api/poll", handlePoll)
log.Printf("listen on %s", conf.Port)
http.ListenAndServe(conf.Port, nil)
}