This repository has been archived by the owner on Mar 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 125
/
server.go
140 lines (125 loc) · 3.01 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
// go-redis-server is a helper library for building server software capable of speaking the redis protocol.
// This could be an alternate implementation of redis, a custom proxy to redis,
// or even a completely different backend capable of "masquerading" its API as a redis database.
package redis
import (
"fmt"
"io"
"io/ioutil"
"net"
"reflect"
)
type Server struct {
Proto string
Addr string // TCP address to listen on, ":6389" if empty
MonitorChans []chan string
methods map[string]HandlerFn
}
func (srv *Server) ListenAndServe() error {
addr := srv.Addr
if srv.Proto == "" {
srv.Proto = "tcp"
}
if srv.Proto == "unix" && addr == "" {
addr = "/tmp/redis.sock"
} else if addr == "" {
addr = ":6389"
}
l, e := net.Listen(srv.Proto, addr)
if e != nil {
return e
}
return srv.Serve(l)
}
// Serve accepts incoming connections on the Listener l, creating a
// new service goroutine for each. The service goroutines read requests and
// then call srv.Handler to reply to them.
func (srv *Server) Serve(l net.Listener) error {
defer l.Close()
srv.MonitorChans = []chan string{}
for {
rw, err := l.Accept()
if err != nil {
return err
}
go srv.ServeClient(rw)
}
}
// Serve starts a new redis session, using `conn` as a transport.
// It reads commands using the redis protocol, passes them to `handler`,
// and returns the result.
func (srv *Server) ServeClient(conn net.Conn) (err error) {
defer func() {
if err != nil {
fmt.Fprintf(conn, "-%s\n", err)
}
conn.Close()
}()
clientChan := make(chan struct{})
// Read on `conn` in order to detect client disconnect
go func() {
// Close chan in order to trigger eventual selects
defer close(clientChan)
defer Debugf("Client disconnected")
// FIXME: move conn within the request.
if false {
io.Copy(ioutil.Discard, conn)
}
}()
var clientAddr string
switch co := conn.(type) {
case *net.UnixConn:
f, err := conn.(*net.UnixConn).File()
if err != nil {
return err
}
clientAddr = f.Name()
default:
clientAddr = co.RemoteAddr().String()
}
for {
request, err := parseRequest(conn)
if err != nil {
return err
}
request.Host = clientAddr
request.ClientChan = clientChan
reply, err := srv.Apply(request)
if err != nil {
return err
}
if _, err = reply.WriteTo(conn); err != nil {
return err
}
}
return nil
}
func NewServer(c *Config) (*Server, error) {
srv := &Server{
Proto: c.proto,
MonitorChans: []chan string{},
methods: make(map[string]HandlerFn),
}
if srv.Proto == "unix" {
srv.Addr = c.host
} else {
srv.Addr = fmt.Sprintf("%s:%d", c.host, c.port)
}
if c.handler == nil {
c.handler = NewDefaultHandler()
}
rh := reflect.TypeOf(c.handler)
for i := 0; i < rh.NumMethod(); i++ {
method := rh.Method(i)
if method.Name[0] > 'a' && method.Name[0] < 'z' {
continue
}
println(method.Name)
handlerFn, err := srv.createHandlerFn(c.handler, &method.Func)
if err != nil {
return nil, err
}
srv.Register(method.Name, handlerFn)
}
return srv, nil
}