Skip to content

Commit 06588e6

Browse files
committed
gofmt + golint
1 parent 716c4cd commit 06588e6

File tree

3 files changed

+109
-111
lines changed

3 files changed

+109
-111
lines changed

dcached.go

Lines changed: 72 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,59 @@ package main
22

33
import (
44
"fmt"
5+
"log"
6+
"net"
7+
"net/http"
58
"os"
6-
"syscall"
79
"os/signal"
8-
"github.com/julienschmidt/httprouter"
9-
"net"
10-
"net/http"
11-
"log"
10+
"syscall"
1211
"time"
12+
13+
"github.com/julienschmidt/httprouter"
1314
//"encoding/hex"
1415
)
1516

1617
var (
18+
// VERSION My current version
1719
VERSION = "0.0.0"
1820
)
1921

2022
var (
21-
SIBLINGS_ADDR = "224.0.0.1:9999"
22-
BEACON_FREQ time.Duration = 2 //seconds
23-
SIBLING_TTL int64 = 5 //seconds
24-
BEACON_INTERFACE = ""
23+
siblingsAddr = "224.0.0.1:9999"
24+
beaconFreq time.Duration = 2 //seconds
25+
siblingTTL int64 = 5 //seconds
26+
beaconInterface = ""
2527

2628
maxDatagramSize = 128
2729

28-
CACHE_IP = ""
29-
CACHE_PORT = "8080"
30-
CACHE_GC_FREQ = 3600
31-
32-
CACHE_MODE = "standalone"
33-
34-
CACHE_GET_URL = "cache/get"
35-
CACHE_SET_URL = "cache/set"
36-
CACHE_REMOVE_URL = "cache/remove/:cache_block"
37-
CACHE_REMOVE_KEY_URL = "cache/remove/key"
38-
CACHE_REMOVE_APP_URL = "cache/remove/application"
39-
CACHE_REMOVE_ALL_URL = "cache/remove/all"
40-
CACHE_STATS_URL = "cache/stats/:stats_type"
41-
CACHE_STATS_LOCAL_URL = "cache/stats/local"
42-
CACHE_STATS_ALL_URL = "cache/stats/all"
43-
CACHE_IMPORT_URL = "cache/import"
44-
45-
ME = ""
46-
SIBLINGS_MANAGER *SiblingsManager
30+
cacheIP = ""
31+
cachePort = "8080"
32+
cacheGCFreq = 3600
33+
34+
cacheMode = "standalone"
35+
36+
cacheGetURL = "cache/get"
37+
cacheSetURL = "cache/set"
38+
cacheRemoveURL = "cache/remove/:cache_block"
39+
cacheRemoveKeyURL = "cache/remove/key"
40+
cacheRemoveAppURL = "cache/remove/application"
41+
cacheRemoveAllURL = "cache/remove/all"
42+
cacheStatsURL = "cache/stats/:stats_type"
43+
cacheStatslocalURL = "cache/stats/local"
44+
cacheStatsAllURL = "cache/stats/all"
45+
cacheImportURL = "cache/import"
46+
47+
// ME amongst my siblings
48+
ME = ""
49+
siblingsMgr *siblingsManager
4750
)
4851

4952
var (
50-
CACHE *Cache
53+
mainCache *cacheOperator
5154
)
5255

53-
54-
5556
func udpBeacon() {
56-
addr, err := net.ResolveUDPAddr("udp", SIBLINGS_ADDR)
57+
addr, err := net.ResolveUDPAddr("udp", siblingsAddr)
5758
if err != nil {
5859
log.Fatal(err)
5960
}
@@ -62,7 +63,7 @@ func udpBeacon() {
6263
c, err := net.DialUDP("udp", nil, addr)
6364
for {
6465
c.Write([]byte(ME))
65-
time.Sleep(BEACON_FREQ * time.Second)
66+
time.Sleep(beaconFreq * time.Second)
6667
}
6768
}
6869

@@ -89,13 +90,13 @@ func serveMulticastUDP(a string, iface *net.Interface, callback func(*net.UDPAdd
8990
}
9091
}
9192

92-
func exportcache(sig_ch chan os.Signal) {
93+
func exportcache(sigCh chan os.Signal) {
9394

94-
s := <-sig_ch
95+
s := <-sigCh
9596

96-
log.Println("Received signal",s)
97+
log.Println("Received signal", s)
9798

98-
SIBLINGS_MANAGER.DistributeContent()
99+
siblingsMgr.distributeContent()
99100

100101
os.Exit(0)
101102
}
@@ -104,68 +105,66 @@ func main() {
104105

105106
ME, _ = os.Hostname()
106107

107-
var beacon_interface *net.Interface
108+
var beaconIface *net.Interface
108109

109110
readConfig()
110111

111-
if BEACON_INTERFACE == "" {
112-
beacon_interface = nil
113-
BEACON_INTERFACE = "default"
112+
if beaconInterface == "" {
113+
beaconIface = nil
114+
beaconInterface = "default"
114115
} else {
115116
var err error
116-
beacon_interface, err = net.InterfaceByName(BEACON_INTERFACE)
117+
beaconIface, err = net.InterfaceByName(beaconInterface)
117118
if err != nil {
118119
log.Fatal(err)
119120
}
120121
}
121122

122-
log.Println("Starting Dcached", VERSION,"on", ME, "[ port", CACHE_PORT,"]", CACHE_MODE, "mode")
123-
if CACHE_MODE == "cluster" {
124-
log.Println("Multicast group", SIBLINGS_ADDR)
125-
log.Println("Beacon interval", int(BEACON_FREQ), "seconds")
126-
log.Println("Siblings TTL", SIBLING_TTL, "seconds")
123+
log.Println("Starting Dcached", VERSION, "on", ME, "[ port", cachePort, "]", cacheMode, "mode")
124+
if cacheMode == "cluster" {
125+
log.Println("Multicast group", siblingsAddr)
126+
log.Println("Beacon interval", int(beaconFreq), "seconds")
127+
log.Println("Siblings TTL", siblingTTL, "seconds")
127128
log.Println("Max.datagram size", maxDatagramSize)
128-
log.Println("Beacon network interface", BEACON_INTERFACE)
129+
log.Println("Beacon network interface", beaconInterface)
129130
}
130-
log.Println("Garbage collector interval", CACHE_GC_FREQ, "seconds")
131+
log.Println("Garbage collector interval", cacheGCFreq, "seconds")
131132

132-
CACHE = NewCache()
133-
SIBLINGS_MANAGER = NewSiblingsManager()
133+
mainCache = newCache()
134+
siblingsMgr = newSiblingsManager()
134135

135-
sig_ch := make(chan os.Signal, 1)
136-
signal.Notify(sig_ch, os.Interrupt)
137-
signal.Notify(sig_ch, syscall.SIGTERM)
136+
sigCh := make(chan os.Signal, 1)
137+
signal.Notify(sigCh, os.Interrupt)
138+
signal.Notify(sigCh, syscall.SIGTERM)
138139

139-
go exportcache(sig_ch)
140+
go exportcache(sigCh)
140141

141-
if CACHE_MODE == "cluster" {
142+
if cacheMode == "cluster" {
142143

143144
go udpBeacon()
144-
go serveMulticastUDP(SIBLINGS_ADDR, beacon_interface, SIBLINGS_MANAGER.MsgHandler)
145+
go serveMulticastUDP(siblingsAddr, beaconIface, siblingsMgr.msgHandler)
145146
//go serveMulticastUDP(SIBLINGS_ADDR, nil, SIBLINGS_MANAGER.MsgHandler)
146147
}
147148

148-
router := httprouter.New()
149+
router := httprouter.New()
149150

150-
router.NotFound = NotFoundHandler{}
151-
router.MethodNotAllowed = MethodNotAllowedHandler{}
151+
router.NotFound = notFoundHandler{}
152+
router.MethodNotAllowed = methodNotAllowedHandler{}
152153

153-
router.POST("/"+CACHE_GET_URL, CacheGet)
154-
router.OPTIONS("/"+CACHE_GET_URL, CacheGetDoc)
154+
router.POST("/"+cacheGetURL, cacheGet)
155+
router.OPTIONS("/"+cacheGetURL, cacheGetDoc)
155156

156-
router.POST("/"+CACHE_SET_URL, CacheSet)
157-
router.OPTIONS("/"+CACHE_SET_URL, CacheSetDoc)
157+
router.POST("/"+cacheSetURL, cacheSet)
158+
router.OPTIONS("/"+cacheSetURL, cacheSetDoc)
158159

159-
router.OPTIONS("/"+CACHE_REMOVE_URL, CacheRemoveDoc)
160-
router.POST("/"+CACHE_REMOVE_URL, CacheRemove)
160+
router.OPTIONS("/"+cacheRemoveURL, cacheRemoveDoc)
161+
router.POST("/"+cacheRemoveURL, cacheRemove)
161162

162-
router.OPTIONS("/"+CACHE_STATS_URL, CacheStatsHandlerDoc)
163-
router.GET("/"+CACHE_STATS_URL, CacheStatsHandler)
163+
router.OPTIONS("/"+cacheStatsURL, cacheStatsHandlerDoc)
164+
router.GET("/"+cacheStatsURL, cacheStatsHandler)
164165

165-
router.OPTIONS("/"+CACHE_IMPORT_URL, CacheImportDoc)
166-
router.POST("/"+CACHE_IMPORT_URL, CacheImport)
166+
router.OPTIONS("/"+cacheImportURL, cacheImportDoc)
167+
router.POST("/"+cacheImportURL, cacheImport)
167168

168-
log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%s", CACHE_IP, CACHE_PORT), router))
169+
log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%s", cacheIP, cachePort), router))
169170
}
170-
171-

request_errors.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
package main
22

33
import (
4-
"net/http"
4+
"net/http"
55
)
66

7-
type NotFoundHandler struct {
7+
type notFoundHandler struct {
88
}
9-
func (h NotFoundHandler) ServeHTTP(w http.ResponseWriter,r *http.Request) {
109

11-
e := NewException("ResourceNotFoundException", "Resource not found")
12-
e.Write(w)
10+
func (h notFoundHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
11+
12+
e := newException("ResourceNotFoundException", "Resource not found")
13+
e.write(w)
1314

1415
}
1516

16-
type MethodNotAllowedHandler struct {}
17+
type methodNotAllowedHandler struct{}
1718

18-
func (h MethodNotAllowedHandler) ServeHTTP(w http.ResponseWriter,r *http.Request) {
19+
func (h methodNotAllowedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
1920

20-
e := NewException("MethodNotAllowedException", "Method not allowed")
21-
e.Write(w)
21+
e := newException("MethodNotAllowedException", "Method not allowed")
22+
e.write(w)
2223

2324
}

request_stats.go

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,77 @@
11
package main
22

33
import (
4-
"fmt"
4+
"encoding/json"
5+
"fmt"
56
"log"
7+
"net/http"
68
"time"
7-
"net/http"
8-
"encoding/json"
9-
"github.com/julienschmidt/httprouter"
9+
10+
"github.com/julienschmidt/httprouter"
1011
)
1112

12-
type StatsAll struct {
13+
type statsAll struct {
1314
TotalBytes int64
14-
Nodes []*CacheStats
15+
Nodes []*cacheStats
1516
}
16-
func (sta *StatsAll) String() string {
17+
18+
func (sta *statsAll) String() string {
1719
b, _ := json.Marshal(sta)
1820
return string(b)
1921
}
20-
func (sta *StatsAll) Write(w http.ResponseWriter) {
22+
func (sta *statsAll) write(w http.ResponseWriter) {
2123
w.Header().Set("Content-Type", "application/json")
2224
w.WriteHeader(200)
2325
fmt.Fprintf(w, "%s", sta)
2426
}
2527

26-
27-
func (c *CacheStats) String() string {
28+
func (c *cacheStats) String() string {
2829
b, _ := json.Marshal(c)
2930
return string(b)
3031
}
31-
func (c *CacheStats) Write(w http.ResponseWriter) {
32+
func (c *cacheStats) write(w http.ResponseWriter) {
3233
w.Header().Set("Content-Type", "application/json")
3334
w.WriteHeader(200)
3435
fmt.Fprintf(w, "%s", c)
3536
}
3637

37-
38-
func CacheStatsHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
38+
func cacheStatsHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
3939

4040
statsop := &statsOp{
4141
done: make(chan bool),
4242
}
4343

44-
stats_type := ps.ByName("stats_type")
44+
statsType := ps.ByName("stats_type")
4545

46-
log.Println("request::stats type", stats_type)
46+
log.Println("request::stats type", statsType)
4747

48-
timeit_start := time.Now().UnixNano()
48+
timeitStart := time.Now().UnixNano()
4949

50-
CACHE.CacheStats <-statsop
50+
mainCache.CacheStats <- statsop
5151
<-statsop.done
5252

53-
if stats_type == "local" {
54-
statsop.stats.Elapsed_ns = time.Now().UnixNano() - timeit_start
55-
statsop.stats.Write(w)
53+
if statsType == "local" {
54+
statsop.stats.ElapsedNs = time.Now().UnixNano() - timeitStart
55+
statsop.stats.write(w)
5656

57-
} else if stats_type == "all" {
58-
sta := &StatsAll{}
57+
} else if statsType == "all" {
58+
sta := &statsAll{}
5959
sta.Nodes = append(sta.Nodes, statsop.stats)
6060
sta.TotalBytes = statsop.stats.MemBytes
6161

62-
ch := SIBLINGS_MANAGER.PropagateStats()
62+
ch := siblingsMgr.propagateStats()
6363
for stat := range ch {
6464
if stat != nil {
6565
sta.Nodes = append(sta.Nodes, stat)
6666
sta.TotalBytes += stat.MemBytes
6767
}
6868
}
69-
statsop.stats.Elapsed_ns = time.Now().UnixNano() - timeit_start
70-
sta.Write(w)
71-
69+
statsop.stats.ElapsedNs = time.Now().UnixNano() - timeitStart
70+
sta.write(w)
7271

7372
} else {
74-
e := NewException("ResourceNotFoundException", "Resource not found")
75-
e.Write(w)
73+
e := newException("ResourceNotFoundException", "Resource not found")
74+
e.write(w)
7675
}
7776

7877
}
79-

0 commit comments

Comments
 (0)