-
Notifications
You must be signed in to change notification settings - Fork 371
/
Copy pathp_mysql.go
190 lines (172 loc) · 4.99 KB
/
p_mysql.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package backends
import (
"database/sql"
"strings"
"time"
"github.com/flashmob/go-guerrilla/envelope"
"github.com/go-sql-driver/mysql"
"runtime/debug"
)
type MysqlProcessorConfig struct {
NumberOfWorkers int `json:"save_workers_size"`
MysqlTable string `json:"mail_table"`
MysqlDB string `json:"mysql_db"`
MysqlHost string `json:"mysql_host"`
MysqlPass string `json:"mysql_pass"`
MysqlUser string `json:"mysql_user"`
RedisExpireSeconds int `json:"redis_expire_seconds"`
RedisInterface string `json:"redis_interface"`
PrimaryHost string `json:"primary_mail_host"`
}
type MysqlProcessor struct {
cache stmtCache
config *MysqlProcessorConfig
}
func (m *MysqlProcessor) connect(config *MysqlProcessorConfig) (*sql.DB, error) {
var db *sql.DB
var err error
conf := mysql.Config{
User: config.MysqlUser,
Passwd: config.MysqlPass,
DBName: config.MysqlDB,
Net: "tcp",
Addr: config.MysqlHost,
ReadTimeout: GuerrillaDBAndRedisBatchTimeout + (time.Second * 10),
WriteTimeout: GuerrillaDBAndRedisBatchTimeout + (time.Second * 10),
Params: map[string]string{"collation": "utf8_general_ci"},
}
if db, err = sql.Open("mysql", conf.FormatDSN()); err != nil {
mainlog.Error("cannot open mysql", err)
return nil, err
}
mainlog.Info("connected to mysql on tcp ", config.MysqlHost)
return db, err
}
// prepares the sql query with the number of rows that can be batched with it
func (g *MysqlProcessor) prepareInsertQuery(rows int, db *sql.DB) *sql.Stmt {
if rows == 0 {
panic("rows argument cannot be 0")
}
if g.cache[rows-1] != nil {
return g.cache[rows-1]
}
sqlstr := "INSERT INTO " + g.config.MysqlTable + " "
sqlstr += "(`date`, `to`, `from`, `subject`, `body`, `charset`, `mail`, `spam_score`, `hash`, `content_type`, `recipient`, `has_attach`, `ip_addr`, `return_path`, `is_tls`)"
sqlstr += " values "
values := "(NOW(), ?, ?, ?, ? , 'UTF-8' , ?, 0, ?, '', ?, 0, ?, ?, ?)"
// add more rows
comma := ""
for i := 0; i < rows; i++ {
sqlstr += comma + values
if comma == "" {
comma = ","
}
}
stmt, sqlErr := db.Prepare(sqlstr)
if sqlErr != nil {
mainlog.WithError(sqlErr).Fatalf("failed while db.Prepare(INSERT...)")
}
// cache it
g.cache[rows-1] = stmt
return stmt
}
func (g *MysqlProcessor) doQuery(c int, db *sql.DB, insertStmt *sql.Stmt, vals *[]interface{}) {
var execErr error
defer func() {
if r := recover(); r != nil {
//logln(1, fmt.Sprintf("Recovered in %v", r))
mainlog.Error("Recovered form panic:", r, string(debug.Stack()))
sum := 0
for _, v := range *vals {
if str, ok := v.(string); ok {
sum = sum + len(str)
}
}
mainlog.Errorf("panic while inserting query [%s] size:%d, err %v", r, sum, execErr)
panic("query failed")
}
}()
// prepare the query used to insert when rows reaches batchMax
insertStmt = g.prepareInsertQuery(c, db)
_, execErr = insertStmt.Exec(*vals...)
if execErr != nil {
mainlog.WithError(execErr).Error("There was a problem the insert")
}
}
func MySql() Decorator {
var config *MysqlProcessorConfig
var vals []interface{}
var db *sql.DB
mp := &MysqlProcessor{}
Service.AddInitializer(Initialize(func(backendConfig BackendConfig) error {
configType := baseConfig(&MysqlProcessorConfig{})
bcfg, err := ab.extractConfig(backendConfig, configType)
if err != nil {
return err
}
config = bcfg.(*MysqlProcessorConfig)
mp.config = config
db, err = mp.connect(config)
if err != nil {
mainlog.Fatalf("cannot open mysql: %s", err)
return err
}
return nil
}))
// shutdown
Service.AddShutdowner(Shutdown(func() error {
if db != nil {
db.Close()
}
return nil
}))
return func(c Processor) Processor {
return ProcessorFunc(func(e *envelope.Envelope) (BackendResult, error) {
var to, body string
to = trimToLimit(strings.TrimSpace(e.RcptTo[0].User)+"@"+config.PrimaryHost, 255)
hash := ""
if len(e.Hashes) > 0 {
hash = e.Hashes[0]
}
var co *compressor
// a compressor was set
if e.Meta != nil {
if c, ok := e.Meta["zlib-compressor"]; ok {
body = "gzip"
co = c.(*compressor)
}
// was saved in redis
if _, ok := e.Meta["redis"]; ok {
body = "redis"
}
}
// build the values for the query
vals = []interface{}{} // clear the vals
vals = append(vals,
to,
trimToLimit(e.MailFrom.String(), 255),
trimToLimit(e.Subject, 255),
body)
if body == "redis" {
// data already saved in redis
vals = append(vals, "")
} else if co != nil {
// use a compressor (automatically adds e.DeliveryHeader)
vals = append(vals, co.String())
//co.clear()
} else {
vals = append(vals, e.DeliveryHeader+e.Data.String())
}
vals = append(vals,
hash,
to,
e.RemoteAddress,
trimToLimit(e.MailFrom.String(), 255),
e.TLS)
stmt := mp.prepareInsertQuery(1, db)
mp.doQuery(1, db, stmt, &vals)
// continue to the next Processor in the decorator chain
return c.Process(e)
})
}
}