This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
/
frontend.go
86 lines (66 loc) · 1.91 KB
/
frontend.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
package main
// This represents a cache front end server, that front slowdb/slowserver requests
import (
"flag"
"fmt"
"github.com/capotej/groupcache-db-experiment/api"
"github.com/capotej/groupcache-db-experiment/client"
"github.com/golang/groupcache"
"net"
"net/http"
"net/rpc"
"os"
"strconv"
)
type Frontend struct {
cacheGroup *groupcache.Group
}
func (s *Frontend) Get(args *api.Load, reply *api.ValueResult) error {
var data []byte
fmt.Printf("cli asked for %s from groupcache\n", args.Key)
err := s.cacheGroup.Get(nil, args.Key,
groupcache.AllocatingByteSliceSink(&data))
reply.Value = string(data)
return err
}
func NewServer(cacheGroup *groupcache.Group) *Frontend {
server := new(Frontend)
server.cacheGroup = cacheGroup
return server
}
func (s *Frontend) Start(port string) {
rpc.Register(s)
rpc.HandleHTTP()
l, e := net.Listen("tcp", port)
if e != nil {
fmt.Println("fatal")
}
http.Serve(l, nil)
}
func main() {
var port = flag.String("port", "8001", "groupcache port")
flag.Parse()
peers := groupcache.NewHTTPPool("http://localhost:" + *port)
client := new(client.Client)
var stringcache = groupcache.NewGroup("SlowDBCache", 64<<20, groupcache.GetterFunc(
func(ctx groupcache.Context, key string, dest groupcache.Sink) error {
result := client.Get(key)
fmt.Printf("asking for %s from dbserver\n", key)
dest.SetBytes([]byte(result))
return nil
}))
peers.Set("http://localhost:8001", "http://localhost:8002", "http://localhost:8003")
frontendServer := NewServer(stringcache)
i, err := strconv.Atoi(*port)
if err != nil {
// handle error
fmt.Println(err)
os.Exit(2)
}
var frontEndport = ":" + strconv.Itoa(i+1000)
go frontendServer.Start(frontEndport)
fmt.Println(stringcache)
fmt.Println("cachegroup slave starting on " + *port)
fmt.Println("frontend starting on " + frontEndport)
http.ListenAndServe("127.0.0.1:"+*port, http.HandlerFunc(peers.ServeHTTP))
}