背景

前段时间由于不想将一些团队必须有的业务逻辑(如封禁IP,各种常规检查,转发等)写于各个业务线上,于是我们就动起了使用luanginx做手脚的心思,一开始本来就组长一个人在弄,后面我出于好奇,就『自愿』援助了其中一条业务线的一个适合写在nginx的功能,这个功能具体不加以描述,总体来说涉及到了过滤请求,查询数据库,分发请求这几个步骤。

Read more »

背景知识

Pub/Sub

Redis中使用SUBSCRIBE, UNSUBSCRIBEPUBLISH 方法将可以实现生产者/消费者模式,所谓的生产者其实就是使用PUBLISH将message(这里需要说明,message只能是String,不过我们可以序列化一个对象为Json再进行推送)推送到指定的队列中,再由SUBSCRIBE监听到消息并取出。UNSUBSCRIBE是取消监听。

Read more »

基本概念

Topic:Kafka将消息种子(Feed)分门别类, 每一类的消息称之为话题(Topic).
Producer:发布消息的对象称之为话题生产者(Kafka topic producer)
Consumer:订阅消息并处理发布的消息的种子的对象称之为话题消费者(consumers)
Broker:已发布的消息保存在一组服务器中,称之为Kafka集群。集群中的每一个服务器都是一个代理(Broker). 消费者可以订阅一个或多个话题,并从Broker拉数据,从而消费这些已发布的消息。
Partition:每个Topic下会有一个或多个Partition,创建Topic时可指定Parition数量。每个Partition对应于一个文件夹,该文件夹下存储该{Partition的数据和索引文件。

Read more »

首先我们需要明确我们什么时候需要用到数据库:

  1. 当缓存并不能解决你的问题,比如写操作,事务操作
  2. 缓存的创建或过期需要通过数据库。

其次,我们可能需要一个专业的工具来指导我们优化:
mysqlreport
这是作为一个Mysql第三方的状态报告工具,其实就是将一下两行命令所获得的数据以一种更加人性化的方法呈现到我们眼前:

1
2
mysql> show status;
mysql> show engine innodb status;

你完全可以在自己电脑上先使用这个工具,这时候我们将逐条讲解工具为我们呈现的数据

Read more »

之所以会有这篇博客其主要原因是因为最近需要写一些脚本用于定时运行,而我个人对于Shell的crontab又是处于看了就忘,忘了就不想看的阶段,Python在我目前看来是最适合替换掉Shell脚本的手段,其中自然也有大量定时运行的技术手段,这篇对于三种可使用不同方法执行定时任务的库做个介绍,仅仅只是简单介绍,如果需要详细了解恐怕还是看其官方文档来得更加适合

Read more »

问题

  1. 同步与异步,阻塞与非阻塞有什么区别,举例说明
  2. 什么是多路复用

问题引入

这是我们通常所写的程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def normal():
for i in range(10):
print(i)

if __name__ == '__main__':
print('begin')
normal()
print('end')

#result
begin
0
1
2
3
4
5
6
7
8
9
end

通常我们的输入需要等到上条输入结束之后才能进行,这也许是我们通常最想要得到的结果,但是有某些时候如果我们所需要的结果没有前后文影响的情况下,我们可能更希望它不是这么死板地来执行的,而是异步执行,最典型的一个例子就是访问网站,网站在进行渲染的时候用到了大量的IO操作,而其他用户不可能等到一个用户渲染完之后在进行渲染,否则会造成比较差的用户体验。

事实上,所有的IO操作(如数据库查询,读写文件等)都回造成阻塞,它们都会让我们无法利用到IO执行这一期间的计算机资源。

为了解决这个问题,计算机引入了一些IO模式的区别。

Read more »

跨站请求伪造(英语:Cross-site request forgery),也被称为 one-click attack 或者 session riding,通常缩写为 CSRF 或者 XSRF, 是一种挟制用户在当前已登录的Web应用程序上执行非本意的操作的攻击方法。

Read more »

首先我们看看 ab -h 命令提供的信息:

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
Usage: ab [options] [http[s]://]hostname[:port]/path
Options are:
-n requests Number of requests to perform # 请求数
-c concurrency Number of multiple requests to make at a time # 并发请求数
-t timelimit Seconds to max. to spend on benchmarking # 测试所进行的最大秒数。
This implies -n 50000 # 其内部隐含值是-n 50000
-s timeout Seconds to max. wait for each response # 响应等待超时时间
Default is 30 seconds # 默认是 30 秒
-b windowsize Size of TCP send/receive buffer, in bytes # TCP窗口大小
-B address Address to bind to when making outgoing connections # 代理地址?
-p postfile File containing data to POST. Remember also to set -T # post请求包含的文件,也可以使用 -T
-u putfile File containing data to PUT. Remember also to set -T # put求包含的文件,也可以使用 -T
-T content-type Content-type header to use for POST/PUT data, eg. # post / put 请求时候 content-type 请求头
'application/x-www-form-urlencoded'
Default is 'text/plain'
-v verbosity How much troubleshooting info to print # 是否需要打印故障日志
-w Print out results in HTML tables # 以HTML表的格式输出结果
-i Use HEAD instead of GET # 使用 HEAD 方法代替 GET 方法
-x attributes String to insert as table attributes # 设置<table>属性的字符串。
-y attributes String to insert as tr attributes # 设置<tr>属性的字符串。
-z attributes String to insert as td or th attributes # 设置<td>属性的字符串。
-C attribute Add cookie, eg. 'Apache=1234'. (repeatable) # 对请求附加一个Cookie:行。其典型形式是name=value的一个参数对,此参数可以重复。
-H attribute Add Arbitrary header line, eg. 'Accept-Encoding: gzip' #对请求附加额外的头信息。此参数的典型形式是一个有效的头信息行,其中包含了以冒号分隔的字段和值的对(如,"Accept-Encoding:zip/zop;8bit")。
Inserted after all normal header lines. (repeatable)
-A attribute Add Basic WWW Authentication, the attributes # 对服务器提供BASIC认证信任。
are a colon separated username and password.
-P attribute Add Basic Proxy Authentication, the attributes # 对一个中转代理提供BASIC认证信任。
are a colon separated username and password.
-X proxy:port Proxyserver and port number to use # 对请求使用代理服务器。
-V Print version number and exit # 显示版本号
-k Use HTTP KeepAlive feature # 启用HTTP KeepAlive功能,即在一个HTTP会话中执行多个请求。默认时,不启用KeepAlive功能。
-d Do not show percentiles served table. # 不显示"percentage served within XX [ms] table"的消息(为以前的版本提供支持)。
-S Do not show confidence estimators and warnings. # 不显示估计配置与警告
-q Do not show progress when doing more than 150 requests # 如果处理的请求数大于150,ab每处理大约10%或者100个请求时,会在stderr输出一个进度计数。此-q标记可以抑制这些信息。
-l Accept variable document length (use this for dynamic pages) # 接受可变长度的文档
-g filename Output collected data to gnuplot format file. # 通过 gnuplot 文件格式收集输出数据
-e filename Output CSV file with percentages served # 产生一个以逗号分隔的(CSV)文件,其中包含了处理每个相应百分比的请求所需要(从1%到100%)的相应百分比的(以微妙为单位)时间。由于这种格式已经“二进制化”,所以比'gnuplot'格式更有用。
-r Don't exit on socket receive errors. # 当 socket 返回错误时不退出
-m method Method name # 请求方法名称
-h Display usage information (this message) # 显示帮助信息
-Z ciphersuite Specify SSL/TLS cipher suite (See openssl ciphers) # 指定 SSL/TLS 加密方式
-f protocol Specify SSL/TLS protocol # 指定 SSL/TLS 协议
(SSL3, TLS1, TLS1.1, TLS1.2 or ALL)

接下来我们使用 http://www.douban.com/ 作为测试网站

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
➜  ~ ab -n 100 -c 100 http://www.douban.com/
This is ApacheBench, Version 2.3 <$Revision: 1604373 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.douban.com (be patient).....done


Server Software: dae # 测试服务器软件名称
Server Hostname: www.douban.com # 测试网址Host
Server Port: 80 # 测试端口

Document Path: / # 测试Path
Document Length: 160 bytes # 返回的报文大小

Concurrency Level: 100 # 标识并发的用户数
Time taken for tests: 0.284 seconds # 执行完所有的请求所花费的时间
Complete requests: 100 # 完成请求数
Failed requests: 0 # 失败请求数
Non-2xx responses: 100 # 返回响应非2xx数
Total transferred: 30100 bytes # 整个过程中的网络传输量
HTML transferred: 16000 bytes # 整个过程中的HTML大小
Requests per second: 352.02 [#/sec] (mean) # 吞吐率,表示每秒处理的请求数,mean标识平均值
Time per request: 284.075 [ms] (mean) # 每个用户等待时间,mean标识平均值
Time per request: 2.841 [ms] (mean, across all concurrent requests) #服务器平均请求处理的时间.
Transfer rate: 103.47 [Kbytes/sec] received # 这些请求在单位内,从服务器获取的数据长度.

Connection Times (ms) # 网络上消耗的时间的分解
min mean[+/-sd] median max
Connect: 26 37 7.9 35 63
Processing: 44 130 76.7 88 239
Waiting: 44 129 76.5 88 239
Total: 70 167 77.5 133 277

Percentage of the requests served within a certain time (ms) # 整个场景中所有请求的响应情况。
50% 133
66% 240
75% 271
80% 272
90% 275
95% 277
98% 277
99% 277
100% 277 (longest request)
0%