-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
150 lines (129 loc) · 3.21 KB
/
server.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"context"
"distributed_cache/cache"
"distributed_cache/client"
proto "distributed_cache/protocol"
"encoding/binary"
"fmt"
"io"
"log"
"net"
"time"
)
type ServerOpts struct {
ListenAddr string
LeaderAddr string
IsLeader bool
}
type Server struct {
ServerOpts
members map[*client.Client]struct{}
cache cache.Cacher
}
func NewServer(opts ServerOpts, c cache.Cacher) *Server {
return &Server{ServerOpts: opts, cache: c, members: make(map[*client.Client]struct{})}
}
func (s *Server) Start() error {
ln, err := net.Listen("tcp", s.ListenAddr)
if err != nil {
return fmt.Errorf("listen error: %s", err)
}
if !s.IsLeader && len(s.LeaderAddr) != 0 {
go func() {
if err := s.dialLeader(); err != nil {
log.Println(err)
}
}()
}
log.Printf("server starting on port [%s]\n", s.ListenAddr)
for {
conn, err := ln.Accept()
log.Printf("new connection [%s]\n", conn.RemoteAddr())
if err != nil {
log.Printf("accept error %s\n", err)
continue
}
go s.HandleConn(conn)
}
}
func (s *Server) dialLeader() error {
conn, err := net.Dial("tcp", s.LeaderAddr)
if err != nil {
return fmt.Errorf("failed to dial leader [%s]", s.LeaderAddr)
}
fmt.Println("Connected to learder ", s.LeaderAddr)
binary.Write(conn, binary.LittleEndian, proto.CmdJoin)
s.HandleConn(conn)
return nil
}
func (s *Server) HandleConn(conn net.Conn) {
defer conn.Close()
for {
cmd, err := proto.ParseCommand(conn)
if err != nil {
if err != io.EOF {
log.Println("parse command error : ", err)
}
break
}
go s.handleCommand(conn, cmd)
}
// fmt.Println("connection closed: ", conn.RemoteAddr())
}
func (s *Server) handleCommand(conn net.Conn, cmd any) {
switch v := cmd.(type) {
case *proto.CommandSet:
s.handleSetCommand(conn, v)
case *proto.CommandGet:
s.handleGetCommand(conn, v)
case *proto.CommandJoin:
s.handleJoinCommand(conn, v)
}
}
func (s *Server) handleJoinCommand(conn net.Conn, cmd *proto.CommandJoin) error {
fmt.Println("member just joined the cluster: ", conn.RemoteAddr())
s.members[client.NewFromConn(conn)] = struct{}{}
return nil
}
func (s *Server) handleSetCommand(conn net.Conn, cmd *proto.CommandSet) error {
log.Printf("SET %s %s\n", cmd.Key, cmd.Value)
go func() {
for memeber := range s.members {
err := memeber.Set(context.TODO(), cmd.Key, cmd.Value, cmd.TTL)
if err != nil {
log.Println("forward to member error:", err)
}
}
}()
resp := proto.ResponseSet{}
if err := s.cache.Set(cmd.Key, cmd.Value, time.Duration(cmd.TTL)*time.Second); err != nil {
resp.Status = proto.StatusERR
_, err := conn.Write(resp.Bytes())
return err
}
resp.Status = proto.StatusOK
_, err := conn.Write(resp.Bytes())
return err
}
func (s *Server) handleGetCommand(conn net.Conn, cmd *proto.CommandGet) error {
log.Printf("GET %s\n", cmd.Key)
resp := proto.ResponseGet{}
val, err := s.cache.Get(cmd.Key)
if err != nil {
resp.Status = proto.StatusKeyNotFound
_, err := conn.Write(resp.Bytes())
return err
}
resp.Status = proto.StatusOK
resp.Value = val
_, err = conn.Write(resp.Bytes())
return err
}
func respondClient(conn net.Conn, msg proto.ResponseGet) error {
_, err := conn.Write(msg.Bytes())
if err != nil {
return err
}
return nil
}