1
|
|
|
package main |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"flag" |
5
|
|
|
"github.com/arazmj/gerdu/cache" |
6
|
|
|
"github.com/arazmj/gerdu/grpcserver" |
7
|
|
|
"github.com/arazmj/gerdu/httpserver" |
8
|
|
|
"github.com/arazmj/gerdu/lfucache" |
9
|
|
|
"github.com/arazmj/gerdu/lrucache" |
10
|
|
|
"github.com/arazmj/gerdu/memcached" |
11
|
|
|
"github.com/arazmj/gerdu/weakcache" |
12
|
|
|
"github.com/inhies/go-bytesize" |
13
|
|
|
log "github.com/sirupsen/logrus" |
14
|
|
|
"os" |
15
|
|
|
"strconv" |
16
|
|
|
"strings" |
17
|
|
|
"sync" |
18
|
|
|
) |
19
|
|
|
|
20
|
|
|
var gerdu cache.UnImplementedCache |
21
|
|
|
var wg = sync.WaitGroup{} |
22
|
|
|
|
23
|
|
|
var ( |
24
|
|
|
loglevel = flag.String("log", "info", |
25
|
|
|
"log level can be any of values of 'panic', 'fatal', 'error', 'warn', 'info', 'debug', 'trace'") |
26
|
|
|
capacityStr = flag.String("capacity", "64MB", |
27
|
|
|
"The size of cache, once cache reached this capacity old values will evicted.\n"+ |
28
|
|
|
"Specify a numerical value followed by one of the following units (not case sensitive)"+ |
29
|
|
|
"\nK or KB: Kilobytes"+ |
30
|
|
|
"\nM or MB: Megabytes"+ |
31
|
|
|
"\nG or GB: Gigabytes"+ |
32
|
|
|
"\nT or TB: Terabytes") |
33
|
|
|
httpPort = flag.Int("httpport", 8080, "the http server port number") |
34
|
|
|
grpcPort = flag.Int("grpcport", 8081, "the grpc server port number") |
35
|
|
|
mcdPort = flag.Int("mcdport", 11211, "the memcached server port number") |
36
|
|
|
kind = flag.String("type", "lru", "type of cache, lru or lfu, weak") |
37
|
|
|
protocols = flag.String("protocols", "http", |
38
|
|
|
"protocol 'grpc', 'http' or 'mcd' (memcached), multiple values can be selected separated by comma") |
39
|
|
|
tlsKey = flag.String("key", "", "SSL certificate private key") |
40
|
|
|
tlsCert = flag.String("cert", "", "SSL certificate public key") |
41
|
|
|
host = flag.String("host", "127.0.0.1", "The host that server listens") |
42
|
|
|
secure = len(*tlsCert) > 0 && len(*tlsKey) > 0 |
43
|
|
|
) |
44
|
|
|
|
45
|
|
|
func main() { |
46
|
|
|
flag.Parse() |
47
|
|
|
setLogLevel() |
48
|
|
|
setCache() |
49
|
|
|
serve() |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
func serve() { |
53
|
|
|
*protocols = strings.ToLower(*protocols) |
54
|
|
|
var validProtocol bool |
55
|
|
|
if strings.Contains(*protocols, "http") { |
56
|
|
|
validProtocol = true |
57
|
|
|
wg.Add(1) |
58
|
|
|
go func() { |
59
|
|
|
defer wg.Done() |
60
|
|
|
httpHost := *host + ":" + strconv.Itoa(*httpPort) |
61
|
|
|
if secure { |
62
|
|
|
httpserver.HTTPServeTLS(httpHost, *tlsCert, *tlsKey, gerdu) |
63
|
|
|
} else { |
64
|
|
|
httpserver.HTTPServe(httpHost, gerdu) |
65
|
|
|
} |
66
|
|
|
}() |
67
|
|
|
} |
68
|
|
|
if strings.Contains(*protocols, "grpc") { |
69
|
|
|
validProtocol = true |
70
|
|
|
wg.Add(1) |
71
|
|
|
go func() { |
72
|
|
|
defer wg.Done() |
73
|
|
|
grpcHost := *host + ":" + strconv.Itoa(*grpcPort) |
74
|
|
|
if secure { |
75
|
|
|
grpcserver.GrpcServeTLS(grpcHost, *tlsCert, *tlsKey, gerdu) |
76
|
|
|
} else { |
77
|
|
|
grpcserver.GrpcServe(grpcHost, gerdu) |
78
|
|
|
} |
79
|
|
|
}() |
80
|
|
|
} |
81
|
|
|
if strings.Contains(*protocols, "mcd") { |
82
|
|
|
validProtocol = true |
83
|
|
|
wg.Add(1) |
84
|
|
|
go func() { |
85
|
|
|
defer wg.Done() |
86
|
|
|
mcdHost := *host + ":" + strconv.Itoa(*mcdPort) |
87
|
|
|
if secure { |
88
|
|
|
log.Fatalln("Memcached protocol does not support TLS") |
89
|
|
|
os.Exit(1) |
90
|
|
|
} |
91
|
|
|
memcached.Serve(mcdHost, gerdu) |
92
|
|
|
}() |
93
|
|
|
} |
94
|
|
|
if !validProtocol { |
95
|
|
|
log.Fatalf("Invalid value for protocol") |
96
|
|
|
os.Exit(1) |
97
|
|
|
} |
98
|
|
|
wg.Wait() |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
func setCache() { |
102
|
|
|
capacity, err := bytesize.Parse(*capacityStr) |
103
|
|
|
if err != nil { |
104
|
|
|
log.Fatal("Invalid value for capacity", err.Error()) |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if strings.ToLower(*kind) == "lru" { |
108
|
|
|
gerdu = lrucache.NewCache(capacity) |
109
|
|
|
} else if strings.ToLower(*kind) == "lfu" { |
110
|
|
|
gerdu = lfucache.NewCache(capacity) |
111
|
|
|
} else if strings.ToLower(*kind) == "weak" { |
112
|
|
|
gerdu = weakcache.NewWeakCache() |
113
|
|
|
} else { |
114
|
|
|
log.Fatalf("Invalid value for type") |
115
|
|
|
os.Exit(1) |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
func setLogLevel() { |
120
|
|
|
switch *loglevel { |
121
|
|
|
case "panic": |
122
|
|
|
log.SetLevel(log.PanicLevel) |
123
|
|
|
case "fatal": |
124
|
|
|
log.SetLevel(log.FatalLevel) |
125
|
|
|
case "error": |
126
|
|
|
log.SetLevel(log.ErrorLevel) |
127
|
|
|
case "warn": |
128
|
|
|
log.SetLevel(log.WarnLevel) |
129
|
|
|
case "info": |
130
|
|
|
log.SetLevel(log.InfoLevel) |
131
|
|
|
case "debug": |
132
|
|
|
log.SetLevel(log.DebugLevel) |
133
|
|
|
case "trace": |
134
|
|
|
log.SetLevel(log.DebugLevel) |
135
|
|
|
default: |
136
|
|
|
log.Fatalf("Invalid log level value %s\n", *loglevel) |
137
|
|
|
os.Exit(1) |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|