2016/03/29


go-github という、Google が開発している GitHub API ライブラリがあるのですが、今回 filosottile さんがたった4行のコードで実行速度を4倍にするという pull-request を書きました。

いったいどういう事かというと、golang の json.Decoder を使って http.Response.Body から JSON を読み取ると最後の改行(EOF)が読み込まれずに残ってしまい、レスポンスを完全に読み切らないまま http.Response.Body.Close() が呼ばれてしまう。これはこれで別に問題のあるコードではないのですが、継続してリクエストを送る場合にレスポンスを完全に読み切っていなかった事でクライアントは物理切断してしまい TLS 接続が再利用されなくなります。この TLS 接続を再利用する為には残りのたった1バイトを読み捨ててあげる必要がある。そこで

defer func() {
    // Drain and close the body to let the Transport reuse the connection
    io.Copy(ioutil.Discard, resp.Body)
    resp.Body.Close()
}()

というコードになったという事です。個人的にはこれは golang 本体がやるべき仕事な気もするのでオススメはしませんし、今後 golang に何かしらの対応が入るのかもしれませんが、今すぐ高速な HTTP 通信が必要という方は試してみるのも良いかもしれません。

ちなみにこの件、Content-Length 分読み取って json.Unmarshal していれば発生しないはずですが json.Decoder のメモリを極力使わないというメリットが使えなくなるというのもあり一長一短ですね。

Posted at by



2016/03/28


golangでIOへのテストを行う | おおたの物置

まとめ fmt.Print等にちゃんと出力されるかテストしたい 結論としては直接は無理 io.Writerを利用するように変えることで簡単にテスト可能 渡されたio.Writerに書き込むようにする ...

http://ota42y.com/blog/2015/04/01/go-io-test/

golang には Example Test という機能があり、テスト関数名に Example のプレフィックスを付ける事で実行結果として出力される標準出力のテストを行う事が出来ます。

期待する結果はこの関数の中にコメントとして書くことが出来ます。

go-pipeline/example_test.go at master - mattn/go-pipeline-· GitHub
https://github.com/mattn/go-pipeline/blob/master/example_test.go

このまま go test として使えるので、CI にもそもままのコードが使えます。

ところで golang のテストで稀にハマるのが「map のイテレートが順不同である」という件。ある map を加工した結果がある期待値と同じかどうかを確認する為に reflect を使うとか面倒ですよね。そこで go1.7 には Unordered Output in Example という機能が入る予定です。

testing: support unordered output in Examples. · Issue #10149 · golang/go · GitHub

Other use cases are like the one I am specifically working on now. A client I work on makes calls to...

https://github.com/golang/go/issues/10149

都合上、issue がまだ閉じられていませんが、コードはマージ済みなので tip で使う事が出来ます。

func ExampleShuffle() {
    x := []int{12345}
    shuffle.ShuffleInt(x)
    for index, value := range x {
        fmt.Printf("index[%d] = %d\n", index, value)
    }

    // Unordered output:
    // 4
    // 2
    // 5
    // 1
    // 3
}

Output の代わりに Unordered output を指定する事で、結果の順が不同であっても OK となります。気軽に map の検査が行える様になりました。

Posted at by




Windows で C++ を使い、データベースの接続先を選ばないアプリケーションを書くのであれば ODBC 接続が一つの選択肢になります。しかし ODBC 接続のアプリケーションを書くのは非常に骨の折れる仕事です。

データの取得 - eternalwindows.jp
http://eternalwindows.jp/windevelop/odbc/odbc05.html

SQLAllocHandle によるハンドルの作成を事ある毎に行わなければなりません。そんな ODBC を使ったアプリケーションの開発を楽にしてくれそうなのが nanodbc です。

nanodbc

A small C++ wrapper for the native C ODBC API.

https://lexicalunit.github.io/nanodbc/

Windows で ODBC のシステムDSNに「mattn」という名前の接続情報を作り、今回は SQLite3 の ODBC ドライバを割り当てました。

odbc

コードも SQLAllocHandle が頻出しないのでとても綺麗に掛けます。

#include <nanodbc.h>
#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;

int
main(int argc, char* argv[]) {
  try {
    nanodbc::connection conn("DSN=mattn");
    cout << "Connected with driver " << conn.driver_name() << endl;
    execute(conn, "drop table if exists example;");
    execute(conn, "create table example(id integer primary key, content text);");

    nanodbc::statement stmt(conn);
    prepare(stmt, "insert into example(content) values(?);");
    stmt.bind(0"ふー");
    nanodbc::result result = execute(stmt);
    cout << "\nAffected " << result.affected_rows() << " rows";

    nanodbc::result rows = execute(conn, "select * from example;");
    cout << "\nDisplaying " << rows.affected_rows() << " rows ";
    cout << "(" << rows.rowset_size() << " fetched at a time):" << endl;
    const short cols = rows.columns();
    for(short i = 0; i < cols; i++)
      cout << rows.column_name(i) << "\t";
    cout << endl;

    while(rows.next()) {
      for(short i = 0; i < cols; i++)
        cout << "(" << rows.get<string>(i, "null") << ")\t";
      cout << endl;
    }
  } catch(const exception& e) {
    cerr << e.what() << endl;
    return 1;
  }
}

/* vim:set et sw=2 cino=j2: */

ただしあまり C++ ぽさが無いので物足りない人もいるかもしれません。ライセンスは MIT です。

Posted at by