-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
225 lines (188 loc) · 4.1 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"fmt"
//"reflect"
"database/sql"
_ "github.com/go-sql-driver/mysql"
"net/url"
"os"
"os/signal"
"runtime"
"sync/atomic"
//"syscall"
"time"
)
const (
PerPageNums = 30
)
var (
mc *MySqlConfig
p uint32
over = make(chan bool)
sem = make(chan uint32, 10)
signalChan = make(chan os.Signal, 1)
)
type Mobile struct {
id int
mobile string
}
type MySqlConfig struct {
Host string
MaxIdle int
MaxOpen int
User string
Pwd string
DB string
Port int
pool *sql.DB
}
func (mc *MySqlConfig) Init() (err error) {
// 构建 DSN 时尤其注意 loc 和 parseTime 正确设置东八区,允许解析时间字段
url := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&loc=%s&parseTime=true",
mc.User,
mc.Pwd,
mc.Host,
mc.Port,
mc.DB,
url.QueryEscape("Asia/Shanghai"),
)
// 全局实例只需调用一次
mc.pool, err = sql.Open("mysql", url)
if err != nil {
return err
}
// 使用前 Ping,确保 DB 连接正常
err = mc.pool.Ping()
if err != nil {
return err
}
// 设置最大连接数,一定要设置 MaxOpen
mc.pool.SetMaxIdleConns(mc.MaxIdle)
mc.pool.SetMaxOpenConns(mc.MaxOpen)
return nil
}
func init() {
mc = &MySqlConfig{
Host: "localhost",
MaxIdle: 1000,
MaxOpen: 2000,
User: "testuser",
Pwd: "testpwd",
DB: "mobiles",
Port: 3306,
}
err := mc.Init()
if err != nil {
panic(err)
}
}
func getAll(sql string, param ...interface{}) ([]*Mobile, error) {
var mobiles []*Mobile
//fmt.Println(param)
//fmt.Println(reflect.TypeOf(param))
err := mc.pool.Ping()
if err != nil {
return mobiles, err
}
stmt, err := mc.pool.Prepare(sql)
if err != nil {
return mobiles, err
}
defer stmt.Close()
rows, err := stmt.Query(param...)
if err != nil {
return mobiles, err
}
defer rows.Close()
//fmt.Println(reflect.TypeOf(rows))
for rows.Next() {
mobile := &Mobile{}
err = rows.Scan(&mobile.id, &mobile.mobile)
if err != nil {
continue
}
mobiles = append(mobiles, mobile)
}
return mobiles, nil
}
func getAll1(sql string) (map[interface{}]interface{}, error) {
mobiles := make(map[interface{}]interface{})
var mobile Mobile
err := mc.pool.Ping()
if err != nil {
return mobiles, err
}
rows, err := mc.pool.Query(sql)
if err != nil {
return mobiles, err
}
defer rows.Close()
for rows.Next() {
err = rows.Scan(&mobile.id, &mobile.mobile)
if err != nil {
continue
}
mobiles[mobile.id] = mobile
}
//sort.Sort(mobiles)
return mobiles, nil
}
func main() {
maxProcs := runtime.NumCPU() // 获取cpu个数
runtime.GOMAXPROCS(maxProcs) //限制同时运行的goroutines数量
//fmt.Println("数据库初始化完成!")
t1 := time.Now()
for i := 0; i < 3; i++ {
go func() {
LabExit:
for {
select {
case <-over:
break LabExit
default:
no := atomic.AddUint32(&p, 1)
sem <- no
mobiles, err := getAll("SELECT id,mobile FROM t_mobile WHERE mobile LIKE ? LIMIT ?,?", "1%", (no-1)*PerPageNums, PerPageNums)
if err != nil {
<-sem
break LabExit
}
//fmt.Printf("p=%d, %v, %d\n", p, mobiles, len(mobiles))
//fmt.Println(reflect.TypeOf(mobiles).Elem())
//fmt.Println(reflect.ValueOf(mobiles))
//fmt.Printf("p=%d, %v, %d\n", p, mobiles, len(mobiles))
for _, mobile := range mobiles {
fmt.Println((*mobile).id, (*mobile).mobile)
}
<-sem
//time.Sleep(time.Millisecond)
if len(mobiles) == 0 {
over <- true
}
}
}
}()
}
go func() {
//signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
signal.Notify(signalChan, os.Interrupt, os.Kill)
}()
select {
case <-over:
fmt.Println("正常结束...")
case <-signalChan:
over <- true
fmt.Println("强制退出...")
}
//time.Sleep(time.Millisecond)
time.Sleep(time.Second) //这里不添加Sleep,会有部分数据显示不出来,如果你有更好的办法,欢迎提出
t2 := time.Since(t1)
fmt.Println("运行时长: ", t2)
/*
infos, err1 := getAll1("SELECT id,mobile FROM t_mobile ORDER BY id ASC LIMIT 50")
if err1 != nil{
panic(err1)
}
fmt.Println(infos)
*/
}