-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
117 lines (97 loc) · 2.05 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
package main
import (
"errors"
"flag"
"log"
"os"
"os/signal"
"runtime"
"sync"
"syscall"
"github.com/panjf2000/gnet/pkg/pool/goroutine"
"github.com/yusank/godis/debug"
"github.com/yusank/godis/redis"
"github.com/yusank/godis/server"
v2 "github.com/yusank/godis/server/v2"
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
defer func() {
for _, f := range deferred {
f()
}
}()
log.SetFlags(log.LstdFlags | log.Lshortfile)
var addr string
flag.StringVar(&addr, "addr", "tcp://:7379", "server address")
flag.Parse()
log.Printf("addr:%s, debug:%t, version:%s\n", addr, debug.DEBUG, server.Version)
if debug.DEBUG {
insertPreData()
}
// srv
srv, err := getSrv(server.Version)
if err != nil {
panic(err)
}
var (
wg = new(sync.WaitGroup)
sigChan = make(chan os.Signal, 1)
errChan = make(chan error, 1)
)
// signal
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)
wg.Add(1)
go func() {
defer wg.Done()
if err = srv.Start(addr); err != nil {
log.Println("service got err: ", err)
errChan <- err
}
}()
wg.Add(1)
go func() {
defer wg.Done()
select {
case <-sigChan:
srv.Stop()
case <-errChan:
srv.Stop()
}
}()
// wait for service shutdown graceful
wg.Wait()
}
var deferred = make([]func(), 0)
func getSrv(version string) (server.IServer, error) {
var srv server.IServer
switch version {
case "v1":
//srv = v1.NewServer()
return nil, errors.New("v1 already deprecated")
case "v2":
p := goroutine.Default()
deferred = append(deferred, p.Release)
srv = v2.NewServer(p)
default:
return nil, errors.New("unknown server version")
}
return srv, nil
}
var prepareData = [][]string{
{"rpush", "list1", "1", "2", "3", "4", "5", "6"},
{"set", "key1", "hello"},
{"set", "key2", "10"},
{"zadd", "zset", "1", "a", "2", "b", "3", "c", "4", "d", "5", "e"},
}
func insertPreData() {
redis.PrintSupportedCmd()
for _, datum := range prepareData {
c := &redis.Command{
Command: datum[0],
Values: datum[1:],
}
c.ExecuteAsync()
}
log.Println("data prepared")
}