-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
51 lines (43 loc) · 998 Bytes
/
node.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
// Package core @Author Bing
// @Date 2023/11/1 15:30:00
// @Desc
package core
import (
"github.com/learnselfs/geeCache/utils"
"net/http"
)
const (
defaultReplicas = 2 << 6
defaultSize = 2 << 10
)
type Node struct {
*Remote
*Group
*Http
*Map
baseUrl string
}
func NewNode(host, port string, api bool, remoteNodeUrl []string, callback callback, basePath string) *Node {
urls := []string{host, ":", port}
url := utils.StringJoin(urls...)
remote := newRemote(url, remoteNodeUrl)
hashMap := NewMap(defaultReplicas, nil)
hashMap.Add(remoteNodeUrl...)
group := NewGroup(defaultSize, "default", callback)
sf := NewSingleFlight()
h := NewHttp(group, remote, hashMap, sf, url, basePath, api)
return &Node{
Map: hashMap,
Group: group,
Http: h,
Remote: remote,
baseUrl: url,
}
}
func (n *Node) Run() {
err := http.ListenAndServe(n.baseUrl, n)
if err != nil {
return
}
utils.OkWithMsg(http.StatusOK, utils.StringJoin("【Server】listen", n.baseUrl))
}