Mar 11, 2019

Cancellation in Golang

Cancellation is very common, but a bit hard to implement.

Golang provides cancellation to ease the effort. Let’s take a example, I want to traverse a directory, and stop while I have listed enough files.

max := 5
numbers := make(chan int)
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)

Here I created max files to list, a channel to print numbers, and a context for cancellation.

count := 0
for num := range numbers {
    fmt.Printf("count: %d, num: %d\n", count, num)
    count++
    if count >= max {
        fmt.Println("cancel")
        cancel()
    }
}

Print numbers is simple, when maxFiles reaches, invoke cancel function to stop producer feeding in numbers channel.

outer:
  for i := start; i < end; i++ {
    select {
    case <- ctx.Done():
      fmt.Println("get done")
      break outer
    case numbers <- i:
      time.Sleep(500 * time.Millisecond)
      // do nothing
    }
  }

The producer procedure is a little complex, before feeding every number, it will check cancellcation. Here “break outer” is necessary, or break will only break select statement.

When we have multiple producers, the statement “if count >= max” will reach multi times. As multiple producers is waiting on “case numbers <- i”, after max reaches, another producer may still writing to numbers channel. To make code correct, another condition check is needs before printing number. As the code becomes ugly, I don’t recommend cancellation on multiple producers.

Besides Context, we can also use an empty struct channel, when closing, every receiver will receive the broadcast. Actually, ctx.Done() is an empty struct channel(<- chan struct{}), it’s identical to use ctx.Done() or empty struct channel manually.

Mar 1, 2019

Notes on AWS and Cassandra

Recently I’m working on set up a cassandra cluster on AWS with terraform, and get some notes to record.

Cassandra nodes joining seeds

Cassandra nodes may get conflict when joining the same seed, so the best way is to add dependency to make them joining in order.

Build base image to reduce duplication

At first I put all provision steps after vm set up, like add deb source, update and install packages. When several vms download simulatenously, downloading may get SSL connection error and whole terraforming will fail.

Then I create a base image with packer and this time no SSL connection error again.

Assign role to EC2 instance

We need to do S3 operation. in EC2 instance, the first thing I do is to create a new user for S3 operations. Then I’m told to create a role and assign it to created EC2 instances, S3 client will use that role automatically. It works like magic, and I find this article to explain the mechanism, https://octopus.com/blog/aws-roles .

Nov 2, 2018

Jul 21, 2014

沈万三的故事

没想到过了600多年,中国的富豪仍然是权贵案板上的鱼肉。叹息褚时健,李经纬,仰融等人。

Jul 21, 2014

所谓Best Practice

在Ruby社区,因为Ruby的创始人的想法是做一件事可以有不同的做法,导致很多人患上选择困难症。

这个时候,Best Practice就是他们的灵丹妙药。我没有认为这些Best Practice是没用的,很多时候就像经典一样,这些“标准”做法是会过时的,也会并不普遍适用。

我年轻的时候也喜欢把从书本里面得到的经验到处宣扬,以为就是自己的经验,当时对于反对自己的人不能够理解。现在我终于能够理解了,那些反对我的人只是反对我得到想法的过程,因为这些想法并没有得到我的实际行动证明。书本中的经验是作者的亲身经历,但是这些经验可能不适用在不同的文化,不同的个体上。

可惜我直到工作9年后才明白这个道理。我刚工作的时候喜欢宣扬敏捷开发,自己也想实践敏捷开发,直到我看到了Programming, Motherfucker! (域名过期了)才知道敏捷开发无非就是多些代码,多些一些对功能没用但是对流程有用的代码,所以敏捷开发的核心还是提升写代码的能力。

向我不理解的人致歉。

Jul 21, 2014

页面的末位淘汰制

很多公司的销售岗位都有末位淘汰制,就是在一个周期内按照销售额进行评比,淘汰掉排名靠后的销售人员,已更好的激励没有被淘汰的销售人员。

网页也可以实行这种机制。网页的一次点击就是一次销售。定期对网站的页面进行评比,从而找出很少被用户访问的页面,加以淘汰。当然像“找回密码”,“联系我们”这种必要的页面还是要保留,但是可能需要改变展示形态让用户更快的找到。API也可以实行。

这样做以后,对于首页应该会有很大的促进。首页上的链接寸土寸金,数量不宜过多,如果在显著的位置的链接很少被点击就像在闹市区设置了广告牌却无人问津一样,是很大的浪费。

可以考虑在下一家公司试验我这个想法。

Jul 20, 2014

小论“骑虎难下”

今天在知乎上答题,突然想到“奇虎”这个名字实在是有不好的寓意。

“骑虎难下”的来历: http://www.baike.com/wiki/%E9%AA%91%E8%99%8E%E9%9A%BE%E4%B8%8B

“骑虎难下”今天的意思是进退两难,最早的原意则有一些“破釜沉舟”,只能进,不能退的意思。

虽然我不用360安全产品,但是至少接触的刚用电脑的人总会用上这样的集合杀毒、系统补丁和应用下载的软件。在习惯了盗版的中国用户群体面前,这样的软件只能靠广告和周边增值产品赚钱。结果没想到这样的局面下也诞生了一只巨头,让人不能不感慨中国人到底是聪明还是愚蠢。

Jul 13, 2014

A new start

I have migrated my blog to tumblr, my previous blog was hosted on wordpress . As wordpress requires payment for custom domain, I have to take a new start.

I also considered to host my blog on github with static site generator like jekyll and octopress, but I am too lazy.

About
A humble programmer. Email: masterwujiang_at_gmail_dot_com Subscribe via RSS.