-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.go
More file actions
175 lines (146 loc) · 4.24 KB
/
Copy pathhttp.go
File metadata and controls
175 lines (146 loc) · 4.24 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Programmed by Mazehua (Reference groupcache)
// South China Normal University
// August 13, 2020
package gocache
import (
"fmt"
"gocache/consisenthash"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"sync"
)
// DefaultBasePath HTTP default path
// DefaultReplicas The default number of virtual nodes for a hash ring
const (
DefaultBasePath = "/_gocache/"
DefaultReplicas = 50
)
// HTTPPool :HTTP contains all Peer <Peer units>
// PeerPicker interface is implemented
type HTTPPool struct {
// The basic URL of Peer
self string
basePath string
mu sync.Mutex
// Place the Peer node in a hash ring
peers *consisenthash.Map
// Map the remote node to the corresponding httpGetter
httpGetters map[string]*httpGetter
}
// NewHTTPPool +
func NewHTTPPool(self string) *HTTPPool {
return &HTTPPool{
self: self,
basePath: DefaultBasePath,
}
}
// Set :Register Peers into cluster (Hash ring)
func (p *HTTPPool) Set(peers ...string) {
p.mu.Lock()
defer p.mu.Unlock()
//Initializes the node hash ring
p.peers = consisenthash.New(DefaultReplicas, nil)
p.peers.Add(peers...)
// Initializes the list of remote mappings
p.httpGetters = make(map[string]*httpGetter)
// eg. 192.168.0.1 -> 192.168.0.1/_gocache/
for _, peer := range peers {
p.httpGetters[peer] = &httpGetter{
baseURL: peer + p.basePath,
}
}
}
// PickPeer :Select the Peer node based on the key value
func (p *HTTPPool) PickPeer(key string) (peer PeerGetter, ok bool) {
p.mu.Lock()
defer p.mu.Unlock()
//consisenthash.Get(...)
if peer := p.peers.Get(key); peer != "" && peer != p.self {
p.Log("Pick peer %s", peer)
return p.httpGetters[peer], true
}
return nil, false
}
// Log !+
func (p *HTTPPool) Log(format string, v ...interface{}) {
log.Printf("[Server %s] %s", p.self, fmt.Sprintf(format, v...))
}
// The cache is listening as a server
func (p *HTTPPool) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.RequestURI() == "/favicon.ico" {
return
}
if !strings.HasPrefix(r.URL.Path, p.basePath) {
panic("HTTPPool serving unexpected path: " + r.URL.Path)
}
p.Log("%s %s", r.Method, r.URL.Path)
parts := strings.Split(r.URL.Path[len(p.basePath):], "/")
if len(parts) < 2 {
http.Error(w, "bad request", http.StatusBadRequest)
return
}
//Parts [0] is a group name
groupName := parts[0]
group := GetGroup(groupName)
if group == nil {
http.Error(w, "no such group: "+groupName, http.StatusNotFound)
return
}
// .../_gocache/[groupsName]/_del/[keys] DELETE
//.../_gocache/[groupsName]/[keys] GET
if parts[1] != "_del" {
view, err := group.Get(parts[1])
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/octet-stream")
w.Write(view.ByteSlice())
} else if parts[1] == "_del" {
group.Delete(parts[2])
}
}
// ----------------------------------------
// httpGetter :Implement client function
// PeerGetter interface is implemented
type httpGetter struct {
baseURL string
}
// Get :Gets the cache remotely as a client
func (h *httpGetter) Get(group string, key string) ([]byte, error) {
// .../_gocache/[groupsName]/[keys] GET
u := fmt.Sprintf("%v%v/%v", h.baseURL, url.QueryEscape(group), url.QueryEscape(key))
res, err := http.Get(u)
if err != nil {
return nil, err
}
defer res.Body.Close()
//The response code is not 200
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Server return code: %d", res.StatusCode)
}
//Read all bytes
bytes, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("Reading response body: %v", err)
}
return bytes, nil
}
// Delete :Deletes the cache remotely as a client
func (h *httpGetter) Delete(group string, key string) error {
// .../_gocache/[groupsName]/_del/[keys] DELETE
u := fmt.Sprintf("%v%v/_del/%v", h.baseURL, url.QueryEscape(group), url.QueryEscape(key))
res, err := http.Get(u)
if err != nil {
return err
}
defer res.Body.Close()
//The response code is not 200
if res.StatusCode != http.StatusOK {
return fmt.Errorf("Reading response body: %v", err)
}
return nil
}