-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
152 lines (120 loc) · 3.01 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
146
147
148
149
150
151
152
package main
import (
"database/sql"
"flag"
"net"
)
func init() {
log = NewLogger(1000)
// Set console log as default
log.SetLogger("console", `{"level":7}`)
}
func main() {
// Close logger
defer log.Close()
var (
cfg *Config
conn *net.TCPConn
db *sql.DB
err error
server *Server
stmt *sql.Stmt
)
// Read flags
flag.Parse()
if PrintVersion {
showVersion()
}
// Read configuration
if cfg, err = NewConfig(CONFIGFILE); err != nil {
log.Critical(err.Error())
} else {
if err = cfg.Parse(); err != nil {
log.Critical(err.Error())
}
}
// Reconfigure log adapters
for _, a := range []string{"file", "console"} {
if err = log.SetLogAdapter(cfg, a); err != nil {
log.Error(err)
err = nil
}
}
// Prepare statement
if db, err = openDB(cfg.Database.DSN()); err != nil {
log.Critical(err.Error())
} else {
if stmt, err = OpenStmt(db); err != nil {
log.Critical(err.Error())
}
}
// Listen on TCP port 2000 on all interfaces.
if server, err = NewServer(cfg.Server); err != nil {
log.Fatal(err)
}
defer func() {
stmt.Close()
db.Close()
server.Close()
log.Close()
}()
// Inform that service is started
log.Info("Service %s started at %s", NAME, server.Addr())
for {
// Wait for a connection.
if conn, err = server.AcceptTCP(); err != nil {
log.Error(err)
}
// Handle the connection in a new goroutine.
// The loop then returns to accepting, so that
// multiple connections may be served concurrently.
go func(c *net.TCPConn) {
var (
conn = NewConnection(c)
fn_wrap = callback_wrap(conn, stmt, cfg)
)
defer func(conn *Connection) {
log.Debug("[%s] Close from %s", conn.Id(), conn.RemoteAddr())
conn.Close()
}(conn)
log.Info("[%s] Connection from %s at %s", conn.Id(), conn.RemoteAddr(), conn.LocalAddr().String())
if err := readConn(conn, fn_wrap); err != nil {
log.Error("[%s] %s", conn.Id(), err.Error())
}
}(conn)
}
}
func callback_wrap(conn *Connection, stmt *sql.Stmt, cfg *Config) (fn func(buf []byte) (int64, error)) {
var (
id = conn.Id()
interval = cfg.GetScoreInterval()
limit = cfg.GetScoreLimit()
)
fn = func(buf []byte) (count int64, err error) {
var (
cmd *Command
)
log.Debug("[%s] Read from connection %d bytes", id, len(buf))
if cmd, err = NewCommand(buf); err != nil {
log.Error("[%s] %s", id, err.Error())
return 0, err
}
log.Debug("[%s] Requested `%s'", id, cmd.GetStr())
if cmd.wr {
log.Warn("[%s] Command `put' is not supported yet", id)
return 0, nil
}
log.Debug("[%s] Call prepared statement with args: client=%s, interval=%d, score_limit=%f", id, cmd.GetStr(), interval, limit)
count, err = isSpammer(stmt, cmd.GetStr(), interval, interval, limit)
if count == 0 {
log.Debug("[%s] Requested `%s' was not found", id, cmd.GetStr())
} else {
log.Info("[%s] Requested `%s' has bothersome score %d, reject", id, cmd.GetStr(), count)
}
if err != nil {
log.Error("[%s], %s", id, err.Error())
}
return
}
return
}