-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
86 lines (75 loc) · 1.74 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
package main
import (
"context"
"distributed_cache/cache"
"distributed_cache/client"
"flag"
"fmt"
"log"
"time"
)
func main() {
// log.SetFlags(log.LstdFlags | log.Lshortfile)
// conn, err := net.Dial("tcp", ":3000")
// if err != nil {
// log.Fatal(err)
// }
// _, err = conn.Write([]byte("SET hello world 50000000000"))
// if err != nil {
// log.Fatal(err)
// }
// select {}
// return
// go func() {
// time.Sleep(time.Second * 2)
// conn, err := net.Dial("tcp", *listenAddr)
// if err != nil {
// log.Fatal(err)
// }
// conn.Write([]byte("SET hello world 50000000000"))
// time.Sleep(time.Second * 1)
// conn.Write([]byte("GET hello"))
// buf := make([]byte, 1024)
// n, _ := conn.Read(buf)
// fmt.Println(string(buf[:n]))
// }()
listenAddr := flag.String("listenaddr", ":3000", "listen address of the server")
leaderAddr := flag.String("leaderaddr", "", "listen address of theleader ")
flag.Parse()
opts := ServerOpts{
ListenAddr: *listenAddr,
IsLeader: len(*leaderAddr) == 0,
LeaderAddr: *leaderAddr,
}
go func() {
time.Sleep(time.Second * 10)
if opts.IsLeader {
sendDummyData()
}
}()
server := NewServer(opts, cache.New())
log.Fatal(server.Start())
}
func sendDummyData() {
for i := 0; i < 100; i++ {
go func(i int) {
key := []byte(fmt.Sprintf("key_%d", i))
value := []byte(fmt.Sprintf("val_%d", i))
client, err := client.New(":3000", client.Options{})
if err != nil {
log.Fatal(err)
}
err = client.Set(context.Background(), key, value, 0)
if err != nil {
log.Fatal(err)
}
time.Sleep(time.Second * 1)
_, err = client.Get(context.Background(), key)
if err != nil {
log.Fatal(err)
}
// fmt.Println(string(val))
client.Close()
}(i)
}
}