[TIL][Go] 把http.response.body 在關閉前先清乾淨可以達到重複使用增加四倍速度

前言

主要是因為這一份twiiter讓我注意到,Filippo Valsorda(@FiloSottile) 提到說他只花了四行程式碼就讓go-github (Google 出的直接操控github 的package) 連線速度可以增加四倍

原因?

根據這次的PR,可以知道主要的原因是在於json decoder對於io.reader讀取資料的時候. 不會一次把所有的直抓完,而會剩下一個 \n

由於這個因素 response.Body (io.Readcloser)呼叫close的時候會把整個TLS的connect關閉.造成每次的connection 都會重新啟動,浪費了許多無謂的時間.

解法

針對Reader的行為而言,如果是讀到了最後一個位元,接下來的讀取都會讀到EOF. 這裡有一個範例可以參考

(When Read encounters an error or end-of-file condition after successfully reading n > 0 bytes, it returns the number of bytes read. It may return the (non-nil) error from the same call or return the error (and n == 0) from a subsequent call. An instance of this general case is that a Reader returning a non-zero number of bytes at the end of the input stream may return either err == EOF or err == nil. The next Read should return 0, EOF.)

所以如果不是使用json decoder,而是使用其他方式來讀資料.就會連\n都讀完,使得response.Body清空後close不會直接把connect 整個關閉.

所以mattn也提供了一個小套件,其實就是把這件事情實現:

resp, err := http.Get(blah)
if err != nil {
    return err
}
json.NewDecoder(resp.Body).Decode(&data)

// 如果reader 已經被清掉,直接結束
if resp.Body == nil {
	return nil
}

// 如果body reader有存在,先把它清空 (drain)
// 透過io.Copy的方式把 resp.Body清空
if _, err := io.Copy(ioutil.Discard, resp.Body); err != nil {
	return err
}

// 這樣一來這樣的Close就不會將TLS connection關閉.
return resp.Body.Close()

此外,透過mattn的這篇文章,也有提到.json.Unmarlshal並不會有這個問題.

2016/03/31 更新

Bradfitz 也就是Go net/http的作者就跳出來說要把這個問題修回去Golang裡面,避免以後其他部分的影響.不過這部分的修改,已經趕不上Go 1.6之中了,大家要稍微注意一下.

[TIL] Note: about change your shell from bash to fish

Preface

When I first take a look about fisherman, I got lots of interesting to change my shell from bash to fish

play

How to Change to Fish (shell)

Install Fish shell

brew install fish

Change it to default shell

//Add fish shell into known shell list
sudo -s 
echo /usr/local/bin/fish >> /etc/shells

//back to original user
//change default shell to fish
exit
chsh -s /usr/local/bin/fish

Suggestion a good plugin for Fisherman?

## My suggest plugin for fisherman
fisher omf/theme-bobthefish

# ~   master *…  Documents/ 

Ops, Why my all configurations missing?

You might occur all the ~/.bash_profile and ~/.bashrc setting missing in fish.

Here is guideline to change it

I got help from @jbucaran, here is his guildline.

1. Convert aliases into functions to opt in fish autoloading, tl;dr alias is slow, functions are fast.

For example:

function vi
     vim $argv
end

2. For export, use set -x

For example:

//setup GOPATH
set -x GOPATH $HOME/src/go

//Extend your PATH
set -x PATH $PATH $GOBIN

Note:

//bash setting
export GOROOT=`go env GOROOT`

//change to fish
set -x GOROOT (go env GOROOT)

More detail, feel free to check this gist.

Let use “fisherman”

“fisherman” is fish plugin manager, which is powerful and useful. Check here for more detail.

curl -sL get.fisherman.sh | fish

Any problem? How to change it back?

Don’t worry, just use chsh to change it back.

chsh -s /bin/bash

[TIL] Push Go 1.6 code to heroku and add Deploy to Heroku button

Publish to Heroku

  • Init git and commit your code.
    • git init
    • git add .
    • git commit -m "init project"
  • Login heroku
    • heroku login
  • Using Golang backpack
    • heroku create -b https://github.com/kr/heroku-buildpack-go.git
  • Vendoring: For Go 1.6 (refer here)
    • go get -u github.com/tools/godep
    • Save your dependency godep save github.com/kkdai/githubrss
    • Submit your depdendency
      • git add .
      • git commit -m "godep"
  • Push your complete code to Heroku
    • git push heroku master

Add “Deploy to Heroku”

  • copy app.json (refer more spec here)
  • Remember must include buildpack. And all buildpack list here.
  • Make sure your buildpack sync with what you use
    • In my case, I use https://github.com/kr/heroku-buildpack-go.git:
"buildpacks": [
    {
      "url": "https://github.com/kr/heroku-buildpack-go.git"
    },
    {
      "url": "heroku/go"
    }	

All app.json sample as follow (refer to rss-webserver):

程式設計週記[2016/03/25]: Docker歡慶三歲.. Docker生態圈已經日漸茁壯

這是什麼?

程式週記主要內容如下:

Gihub project 介紹:

  • 主要會貼一些github,但是會盡量寫上一些有用的評語(或是我容易想到的關鍵詞)幫助以後查詢

網路文章心得:

  • 會寫些心得,強迫自己閱讀.

“程式週記”並且定期週期性更新.

大部分內容在我的twitter都會有,這邊只是將一些簡單的心得與感想註解一下.

本週摘要

本週時間花在Survey 各種IoT Big Data的架構與各種元件的用法,因為Docker三週年聚會,也花了不少時間玩Docker.

比較少時間來弄Golang.下周應該繼續看分散式系統才對.

Go

Fast, powerful, yet easy to use template engine for Go. Optimized for speed, zero memory allocations in hot paths. Up to 20x faster than html/template

這個Go template 效能有點恐怖.. 20x faster than html/template!!

“Go Learn: Some rookie Go mistakes we made (And how we recovered from them)”

不少值得看的粗心錯誤,挑出幾個很容易犯的:

  1. Naked return (先定義好的return value) 會讓整個function 更難閱讀(因為return不需要再帶回傳值). 並且很容易少回傳幾個參數而造成回傳沒有意義的初始值.

  2. Map 的concurrency 不穩定性 (可以參考Go FAQ: Atomic_Maps),要寫concurrency program 要儘量避免使用map

同場加印: Gist上面有人提供Go地雷Landmines

  • 主要是for i scope
  • interface pointer
  • 前面提到Naked return與容易踩到scope問題.

vim-go 1.5 Release

vim-go release新版 New in 1.5 (1) fatih/motion 提供函式檢索的功能 (2) GoAlternate 可以快速切換test跟原本檔案

A microservice toolkit

號稱有著相當多microservice功能的 toolkit,並且具有pluggable(可拔插)功能的設計.感覺很酷…

jsonparser: Alternative JSON parser for Go that does not require schema

相當快速的json parser(根據作者測試),甚至可以比”encoding/json” 快上九倍.

其實有附上跟一些其他json parser的效能比較表,可以參考看看. 同場加映: https://github.com/mailru/easyjson 也說他比 “ encoding/json”快上不少… 不過他跟jsonparser沒有比過效能.. 倒底”encoding/json”是有多慢呢 XDDD

Python

Android/JAVA/NODE.JS

Docker

docker-NAT-router: Docker container that functions as a simple NAT router. Linux iptables MASQUERADE provides network address translation (NAT) and dnsmasq provides DHCP, DNS, and TFTP services.

透過docker來做NAT router.之前再找就是想透過docker來做hole punching的.總算找到比較類似的東西… 主要是透過pipework來達成,這篇有更多相關應用.

此外這一篇有提到Docker 1.9裡面的新東西Container Network Model (CNM) (更多官方文件,).不過似乎無法達成我需要的MASQUERADE的功能. 繼續多多研究.

katacoda: Learn new technologies right in your browser

透過瀏覽器來學習一些科技,裡面有docker的相關教學.可以線上透過瀏覽器來下指令操作.

原生的Docker VM for Mac/Windows

Docker for Mac 與 Docker for Windows 版本,以後在 Mac 與 Windows 上面跑 Docker 不用再安裝VirtualBox,提供的原生的網路支援與更棒的磁碟掛載功能

iOS/Swift

網站文章

網站收集

有聲書/影片心得

本週專案

這邊會寫一些我的Project 52的成果.

本週專案: https://github.com/kkdai/rss-webserver

主要是把前一次的專案githubrss架成RSS Server,順便複習一下Host在Heroku上面. 結果花最多的時間都在弄Heroku.

結果弄好之後,接上IFTTT沒兩天就爆量 (笑) 不過至少看來是個正確的RSS Provider.

[TIL] docker-compose yaml文件檔案格式問題

快速紀錄一下:

  1. 在Windows 的Quick Start Terminal (Toolbox)下,如果git下載 docker-compose.yml下來會無法順利讀到. 原因是unix的文件檔案在Windows底下會無法正確閱讀. (CRLF問題)
�[31mERROR�[0m: In file '.\docker-compose.yml' service 'version' doesn't have any configuration options. All top level keys in your docker-compose.yml must map to a dictionary of configuration option
  1. 此外,如果很聰明地使用了vagrant來進入ubuntu VM中跑docker-compose.如果不小心把檔案寫到了跟Widows共享的資料夾內,一樣會有相同的問題.

結論: MacOSX/Linux 的Docker最安全… Windows好危險 XDDD

[TIL] Vagrant installation and docker installation

起因

想要分享檔案在VM跟電腦,其實VBOX本身有點麻煩.比起VBOX,vagrant反而提供比較簡單的方式.

步驟

//Check vagrant box list
vagrant list

//init
vagrant init ubuntu/trusty64

//update for vagrant shared folder
// editing Vagrantfile
//  --> config.vm.synced_folder "./data", "/vagrant_data"

//startup
vagrant up

//login
vagrant ssh


//Save bot
vagrant package --output BOT_NAME


//Install about basic component
sudo apt-get install build-essential openssl
sudo apt-get install -y pkg-config
sudo apt-get install -y libcairo2-dev

//Install about docker
sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates
sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
//edit /etc/apt/sources.list.d/docker.list
// deb https://apt.dockerproject.org/repo ubuntu-wily main

sudo apt-get update
sudo apt-get purge lxc-docker
sudo apt-cache policy docker-engine
sudo apt-get install docker-engine
sudo service docker start
 
//Docker-compose
curl -L https://github.com/docker/compose/releases/download/1.6.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

//Docker-machine
curl -L https://github.com/docker/machine/releases/download/v0.6.0/docker-machine-`uname -s`-`uname -m` > /usr/local/bin/docker-machine && \
chmod +x /usr/local/bin/docker-machine

//Install npm
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs

sudo apt-get update
sudo apt-get install nodejs-legacy
sudo apt-get install npm

//Install npm related 
npm install -g node-gyp

Refer