在文本编辑器里显示空白字符

2016 Oct 28

下午同事遇到一个bug,数据库始终连接不上。 网络检查正常(用数据库的client可以正常连接)、配置文件也是“正常的”(和其他可以正确连接的同事的配置“一摸一样”)。 最后发现是配置文件中的数据库密码的结尾多了几个空格😓。

所以最好在编辑器/IDE里显示空白字符。绝大部分编辑器都是支持空白字符显示的,包括Vim。

另一个空格相关的bug

Encode String before Put It into URL

2016 Oct 18

在实现、测试OneFeed的tag功能时,发现tag c++不能正常工作,

<a href="<%= '/posts?tag=' + t.name %>" class="..."><%= t.name %></a>

请求/posts?tag=c++到达server端后,params[:tag]的值为c ,导致数据库查询没有返回正确的结果。

原因是构造href时没有对tag的name做encode,而+恰好是URL规范里的特殊字符,它代表一个空格

Within the query string, the plus sign is reserved as shorthand notation for a space. Therefore, real plus signs must be encoded.

所以,在把一个字符串拼接到URL里前,要先把它encode一下。 毕竟,不是每次测试时都有足够运气使用像c++这样测试数据,提前发现bug。

在Rails里可以通过下面的方法来encode一个字符串,

URI.encode 'c++ java'
# => "c++%20java" 空格被encode,URL规范的特殊字符“+”没有被encode
ERB::Util.url_encode 'c++ java'
#=> "c%2B%2B%20java" 所有字符都被encode,包括像"+", "/", "?"这样的保留字符
<!-- fix: u是url_encode的alias -->
<a href="<%= '/posts?tag=' + u(t.name) %>" class="..."><%= t.name %></a>

关于URL decode,大部分web框架都会自动decode。

注意⚠️,“URL”也包括同一页面内的anchor

<a href="#need-to-encode-too">about</a>
<div id="need-to-encode-too">About us...</div>

另外,在准备测试数据时,别忘了中文;中文也是需要encode的。

Trim String If It is a Key

2016 Oct 13

如果字符串的用途是某种类型的key,那么字符串最好要trim一下,去掉首尾的空格。

曾经遇到一个bug,RabbitMQ中一个queue始终收不到routing key为某些值的message, 但是却能收到routing key为另一个值的message。 Queue的binding是通过配置文件指定的,

<routingKey value="routing-key.a.*, routing-key.b.*, routing-key.c.*">

Routing key为routing-key.b.*routing-key.c.*的消息始终收不到。 尝试直接通过“RabbitMQ Management”(http://localhost:15672/)发送消息,总是得到 “Message published, but not routed.”错误。

最后发现root cause是binding创建的时候是通过value.split(',')来提取routing key的。 因为没有trim所以 routing-key.b.*的开头有个空格。 但是sender在publish message的时候用的routing key总是不带空格的😱。

数据库的index也可以认为是一种key。 假设Customer表有一个email column,且email column是有index的。 那么在存数据或者查询时应该先trim一下email。

不要把重要文件放在/tmp目录下!

2016 Oct 13

永远不要把重要文件放到/tmp目录下,即使是临时存放也不要,因为只要系统一重启/tmp目录就会被清空。

曾经在Ubuntu虚拟机上测试OneFeed部署的时候,想记录下部署时涉及到的每个命令方便后来在生产环境部署。 因为涉及到多个Linux user(一个没有sudo权限的Linux user用于跑Rails App,另一个有sudo权限的user用于安装各种软件), 所以就把文件放在了/tmp目录下。 在差不多部署好的时候,为了能在主机上访问虚拟机上部署的应用,需要配置VirtualBox的Host-only Networks, 配置好后需要重启虚拟机。。。😱

The cleaning of /tmp is done by the upstart script /etc/init/mounted-tmp.conf. The script is run by upstart everytime /tmp is mounted. Practically that means at every boot.

The script does roughly the following: if a file in /tmp is older than $TMPTIME days it will be deleted.

The default value of $TMPTIME is 0, which means every file and directory in /tmp gets deleted. $TMPTIME is an environment variable defined in /etc/default/rcS.