1
|
|
|
package main |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"errors" |
6
|
|
|
"flag" |
7
|
|
|
"fmt" |
8
|
|
|
"github.com/arazmj/gerdu/cache" |
9
|
|
|
"github.com/arazmj/gerdu/httpserver" |
10
|
|
|
"github.com/arazmj/gerdu/lfucache" |
11
|
|
|
"github.com/arazmj/gerdu/lrucache" |
12
|
|
|
"github.com/arazmj/gerdu/proto" |
13
|
|
|
"github.com/arazmj/gerdu/weakcache" |
14
|
|
|
"github.com/inhies/go-bytesize" |
15
|
|
|
"google.golang.org/grpc" |
16
|
|
|
"google.golang.org/grpc/credentials" |
17
|
|
|
"log" |
18
|
|
|
"net" |
19
|
|
|
"os" |
20
|
|
|
"strconv" |
21
|
|
|
"strings" |
22
|
|
|
"sync" |
23
|
|
|
) |
24
|
|
|
|
25
|
|
|
var gerdu cache.UnImplementedCache |
26
|
|
|
var wg = sync.WaitGroup{} |
27
|
|
|
|
28
|
|
|
var ( |
29
|
|
|
verbose = flag.Bool("verbose", false, "verbose logging") |
30
|
|
|
capacityStr = flag.String("capacity", "64MB", |
31
|
|
|
"The size of cache, once cache reached this capacity old values will evicted.\n"+ |
32
|
|
|
"Specify a numerical value followed by one of the following units (not case sensitive)"+ |
33
|
|
|
"\nK or KB: Kilobytes"+ |
34
|
|
|
"\nM or MB: Megabytes"+ |
35
|
|
|
"\nG or GB: Gigabytes"+ |
36
|
|
|
"\nT or TB: Terabytes") |
37
|
|
|
httpPort = flag.Int("httpport", 8080, "the http server port number") |
38
|
|
|
grpcPort = flag.Int("grpcport", 8081, "the grpc server port number") |
39
|
|
|
kind = flag.String("type", "lru", "type of cache, lru or lfu, weak") |
40
|
|
|
protocols = flag.String("protocols", "http", "protocol grpc or http, multiple values can be selected seperated by comma") |
41
|
|
|
tlsKey = flag.String("key", "", "SSL certificate private key") |
42
|
|
|
tlsCert = flag.String("cert", "", "SSL certificate public key") |
43
|
|
|
secure = len(*tlsCert) > 0 && len(*tlsKey) > 0 |
44
|
|
|
) |
45
|
|
|
|
46
|
|
|
func main() { |
47
|
|
|
flag.Parse() |
48
|
|
|
capacity, _ := bytesize.Parse(*capacityStr) |
49
|
|
|
|
50
|
|
|
if strings.ToLower(*kind) == "lru" { |
51
|
|
|
gerdu = lrucache.NewCache(capacity) |
52
|
|
|
} else if strings.ToLower(*kind) == "lfu" { |
53
|
|
|
gerdu = lfucache.NewCache(capacity) |
54
|
|
|
} else if strings.ToLower(*kind) == "weak" { |
55
|
|
|
gerdu = weakcache.NewWeakCache() |
56
|
|
|
} else { |
57
|
|
|
fmt.Println("Invalid value for type") |
58
|
|
|
os.Exit(1) |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
*protocols = strings.ToLower(*protocols) |
62
|
|
|
if strings.Contains(*protocols, "http") { |
63
|
|
|
wg.Add(1) |
64
|
|
|
go func() { |
65
|
|
|
defer wg.Done() |
66
|
|
|
if secure { |
67
|
|
|
httpserver.HttpServeTLS(*httpPort, *tlsCert, *tlsKey, gerdu, *verbose) |
68
|
|
|
} else { |
69
|
|
|
httpserver.HttpServe(*httpPort, gerdu, *verbose) |
70
|
|
|
} |
71
|
|
|
}() |
72
|
|
|
} |
73
|
|
|
if strings.Contains(*protocols, "grpc") { |
74
|
|
|
wg.Add(1) |
75
|
|
|
go func() { |
76
|
|
|
grpcServer() |
77
|
|
|
}() |
78
|
|
|
} else { |
79
|
|
|
fmt.Println("Invalid value for protocol") |
80
|
|
|
os.Exit(1) |
81
|
|
|
} |
82
|
|
|
wg.Wait() |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
func grpcServer() { |
86
|
|
|
defer wg.Done() |
87
|
|
|
host := ":" + strconv.Itoa(*grpcPort) |
88
|
|
|
lis, err := net.Listen("tcp", host) |
89
|
|
|
log.Printf("Gerdu started listening gRPC on %d port\n", *grpcPort) |
90
|
|
|
if err != nil { |
91
|
|
|
log.Fatalf("failed to listen: %v", err) |
92
|
|
|
} |
93
|
|
|
var s *grpc.Server |
94
|
|
|
if len(*tlsCert) > 0 && len(*tlsKey) > 0 { |
95
|
|
|
credentials, err := credentials.NewServerTLSFromFile(*tlsCert, *tlsKey) |
96
|
|
|
if err != nil { |
97
|
|
|
log.Fatalf("Failed to setup TLS for gRPC service: %v", err) |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
s = grpc.NewServer(grpc.Creds(credentials)) |
101
|
|
|
} else { |
102
|
|
|
s = grpc.NewServer() |
103
|
|
|
} |
104
|
|
|
proto.RegisterGerduServer(s, &server{}) |
105
|
|
|
if err := s.Serve(lis); err != nil { |
106
|
|
|
log.Fatalf("failed to serve: %v", err) |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
type server struct { |
111
|
|
|
proto.UnimplementedGerduServer |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
func (s *server) Put(ctx context.Context, request *proto.PutRequest) (*proto.PutResponse, error) { |
115
|
|
|
value := string(request.Value) |
116
|
|
|
key := request.Key |
117
|
|
|
created := gerdu.Put(key, value) |
118
|
|
|
if *verbose { |
119
|
|
|
if !created { |
120
|
|
|
log.Printf("gRPC UPDATE Key: %s Value: %s\n", key, value) |
121
|
|
|
} else { |
122
|
|
|
log.Printf("gRPC INSERT Key: %s Value: %s\n", key, value) |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
return &proto.PutResponse{ |
126
|
|
|
Created: created, |
127
|
|
|
}, nil |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
func (s *server) Get(ctx context.Context, request *proto.GetRequest) (*proto.GetResponse, error) { |
131
|
|
|
value, ok := gerdu.Get(request.Key) |
132
|
|
|
if ok { |
133
|
|
|
if *verbose { |
134
|
|
|
log.Printf("gRPC RETREIVED Key: %s Value: %s\n", request.Key, value) |
135
|
|
|
} |
136
|
|
|
return &proto.GetResponse{ |
137
|
|
|
Value: []byte(value), |
138
|
|
|
}, nil |
139
|
|
|
} |
140
|
|
|
if *verbose { |
141
|
|
|
log.Printf("gRPC MISSED Key: %s \n", value) |
142
|
|
|
} |
143
|
|
|
return nil, errors.New("key not found") |
144
|
|
|
} |
145
|
|
|
|