-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
145 lines (124 loc) · 3.78 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
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
package main
import (
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
"runtime"
"syscall"
"github.com/arthurkiller/my_mp/conf"
"github.com/arthurkiller/my_mp/db"
"github.com/arthurkiller/my_mp/grpc/news"
"github.com/arthurkiller/my_mp/grpc/profile"
"github.com/arthurkiller/my_mp/news"
"github.com/arthurkiller/my_mp/profile"
"google.golang.org/grpc"
)
func main() {
pprof := flag.Bool("pprof", false, "set if turn on the pprof")
flag.Parse()
//gen the config
conf := conf.Config{
NewsServerAddr: "127.0.0.1:23588",
ProfileServerAddr: "127.0.0.1:23589",
UIDInfo: []string{"127.0.0.1:16380"},
UIDInfoS: [][]string{{"127.0.0.1:16380"}},
NewsIDInfo: []string{"127.0.0.1:16381"},
NewsIDInfoS: [][]string{{"127.0.0.1:16381"}},
MeipaiIDInfo: []string{"127.0.0.1:16382"},
MeipaiIDInfoS: [][]string{{"127.0.0.1:16382"}},
UIDBox: []string{"127.0.0.1:16383"},
UIDBoxS: [][]string{{"127.0.0.1:16383"}},
UIDSelfbox: []string{"127.0.0.1:16384"},
UIDSelfboxS: [][]string{{"127.0.0.1:16384"}},
UIDFans: []string{"127.0.0.1:16385"},
UIDFansS: [][]string{{"127.0.0.1:16385"}},
UIDFollow: []string{"127.0.0.1:16386"},
UIDFollowS: [][]string{{"127.0.0.1:16386"}},
Maxactive: 5000,
Maxidle: 3000,
Idletimeout: 60,
}
redisConf := redism.RedismConf{
Maxactive: conf.Maxactive,
Maxidle: conf.Maxidle,
Idletimeout: conf.Idletimeout,
Masters: map[string]([]string){},
Slaves: map[string]([][]string){},
}
redisConf.Masters = make(map[string]([]string), 10)
redisConf.Slaves = make(map[string]([][]string), 10)
redisConf.Masters["uid-box"] = conf.UIDBox
redisConf.Masters["uid-info"] = conf.UIDInfo
redisConf.Masters["uid-selfbox"] = conf.UIDSelfbox
redisConf.Masters["newsid-info"] = conf.NewsIDInfo
redisConf.Masters["uid-fans"] = conf.UIDFans
redisConf.Masters["uid-follow"] = conf.UIDFollow
redisConf.Masters["meipaiid-info"] = conf.MeipaiIDInfo
redisConf.Slaves["uid-box"] = conf.UIDBoxS
redisConf.Slaves["uid-info"] = conf.UIDInfoS
redisConf.Slaves["uid-selfbox"] = conf.UIDSelfboxS
redisConf.Slaves["uid-fans"] = conf.UIDFansS
redisConf.Slaves["uid-follow"] = conf.UIDFollowS
redisConf.Slaves["newsid-info"] = conf.NewsIDInfoS
redisConf.Slaves["meipaiid-info"] = conf.MeipaiIDInfoS
//makeup the redis connetion pool manager
redispool := redism.NewRedism(redisConf)
//set up the pprof server
if *pprof {
runtime.SetBlockProfileRate(1)
lis, err := net.Listen("tcp", ":6062")
if err != nil {
fmt.Println(err)
}
fmt.Println("server profiling address: ", lis.Addr())
go func() {
if err := http.Serve(lis, nil); err != nil {
fmt.Println(err)
return
}
}()
}
//startup the server
newsconn, err := net.Listen("tcp", conf.NewsServerAddr)
defer newsconn.Close()
if err != nil {
log.Println("error in listen ", conf.NewsServerAddr)
}
newsServer := grpc.NewServer()
newsS := newsm.NewNewsServer(redispool)
news.RegisterNewsServer(newsServer, newsS)
go func() {
newsServer.Serve(newsconn)
}()
log.Println("News server started!")
profileconn, err := net.Listen("tcp", conf.ProfileServerAddr)
defer profileconn.Close()
if err != nil {
log.Println("error in listen ", conf.ProfileServerAddr)
}
profileServer := grpc.NewServer()
profileS := profilem.NewProfileServer(redispool)
profile.RegisterProfileServer(profileServer, profileS)
go func() {
profileServer.Serve(profileconn)
}()
log.Println("Profile server started!")
//catch the sys signal
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
for {
<-sigc
redispool.Close()
log.Println("news server is stoping")
newsconn.Close()
log.Println("profile server is stoping")
profileconn.Close()
log.Println("redispoll is stoping")
log.Println("server stoped, bye bye!")
return
}
}