-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
185 lines (166 loc) · 5.24 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"encoding/binary"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"net/http/pprof"
"os"
"sync/atomic"
"time"
)
type response struct {
Index uint32 `json:"index"`
Value string `json:"value"`
}
type server struct {
currentIndex uint32
fib *FibTracker
debug bool
backup *os.File
}
// Make server for fibonacci api
func makeServer(fib *FibTracker, backup *os.File, startIdx uint32, debug bool) *server {
s := &server{startIdx, fib, debug, backup}
return s
}
// handler for requests for current fibonacci value
func (s *server) handleGetCurrent() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
idx := s.currentIndex
resp := response{idx, s.fib.Get(idx).String()}
responseEncoder := json.NewEncoder(w)
if err := responseEncoder.Encode(resp); err != nil {
http.Error(w, "Server error", http.StatusInternalServerError)
return
}
}
}
// handler for requests for next fibonacci value - increments sequence index and returns value
func (s *server) handleSetNext() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
idx := atomic.AddUint32(&s.currentIndex, 1)
resp := response{idx, s.fib.Get(idx).String()}
responseEncoder := json.NewEncoder(w)
if err := responseEncoder.Encode(resp); err != nil {
http.Error(w, "Server error", http.StatusInternalServerError)
return
}
}
}
// handler for requests for previous fibonacci value - decrements sequence index and returns value
func (s *server) handleSetPrevious() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
idx := uint32(0)
if s.currentIndex > 0 {
idx = atomic.AddUint32(&s.currentIndex, ^uint32(0))
}
resp := response{idx, s.fib.Get(idx).String()}
responseEncoder := json.NewEncoder(w)
if err := responseEncoder.Encode(resp); err != nil {
http.Error(w, "Server error", http.StatusInternalServerError)
return
}
}
}
// handler for getting cache hit/miss numbers
func (s *server) handleGetCacheStats() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
responseEncoder := json.NewEncoder(w)
if err := responseEncoder.Encode(s.fib.CacheStats); err != nil {
http.Error(w, "Server error", http.StatusInternalServerError)
return
}
}
}
// Create router for the fibonacci server
func (s *server) makeRouter() http.Handler {
router := http.NewServeMux()
router.HandleFunc("/current", s.handleGetCurrent())
router.HandleFunc("/next", s.handleSetNext())
router.HandleFunc("/previous", s.handleSetPrevious())
if s.debug {
router.HandleFunc("/debug/cache", s.handleGetCacheStats())
router.HandleFunc("/debug/pprof/", pprof.Index)
router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
router.HandleFunc("/debug/pprof/profile", pprof.Profile)
router.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
router.HandleFunc("/debug/pprof/trace", pprof.Trace)
router.Handle("/debug/pprof/heap", pprof.Handler("heap"))
router.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine"))
router.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate"))
router.Handle("/debug/pprof/block", pprof.Handler("block"))
}
return router
}
// logs current index on a timer - panics if it fails multiple times
func (s *server) logCurrentIndex(seconds time.Duration) {
ticker := time.NewTicker(seconds)
bs := make([]byte, 4)
remainingFails := 3
for {
<-ticker.C
binary.LittleEndian.PutUint32(bs, s.currentIndex)
_, err := s.backup.WriteAt(bs, 0)
if err != nil {
remainingFails -= 1
if remainingFails == 0 {
log.Fatalf("Failed to write backup: %v (exiting)\n", err)
}
log.Printf("Failed to write backup: %v (%v fails remaining)\n", err, remainingFails)
}
}
}
func main() {
var port *uint = flag.Uint("port", 80, "port on which to expose the API")
var backupPath *string = flag.String("file", "fibapi_backup", "file to journal sequence index to")
var backupSeconds *uint = flag.Uint("seconds", 3, "seconds between each backup")
flag.Parse()
// create fibonacci tracker
hc, err := MakeSliceCache(100000)
if err != nil {
log.Fatal(err)
}
fib := MakeFibTracker(10, hc)
// setup the backup file
startIdx := uint32(0)
var backupFile *os.File
_, err = os.Stat(*backupPath)
if os.IsNotExist(err) {
backupFile, err = os.Create(*backupPath)
defer backupFile.Close()
if err != nil {
log.Fatal(err)
}
} else {
backupFile, err = os.OpenFile(*backupPath, os.O_RDWR, os.ModePerm)
defer backupFile.Close()
if err != nil {
log.Fatal(err)
}
startIdxBytes := make([]byte, 4)
n, err := backupFile.Read(startIdxBytes)
if err != nil || n != 4 {
log.Printf("Failed reading backup: %v\n", err)
} else {
startIdx = binary.LittleEndian.Uint32(startIdxBytes)
}
}
log.Printf("Starting sequence index at %v\n", startIdx)
// create the server and routes
fibServer := makeServer(fib, backupFile, startIdx, true)
router := fibServer.makeRouter()
address := fmt.Sprintf(":%d", *port)
httpServer := &http.Server{
Addr: address,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
Handler: router,
}
// start logger and server
go fibServer.logCurrentIndex(time.Duration(*backupSeconds) * time.Second)
log.Printf("Serving at %v\n", address)
log.Fatal(httpServer.ListenAndServe())
}