{"action":"created","comment":{"url":"https://api.github.com/repos/h2o/h2o/pulls/comments/83785166","pull_request_review_id":4602943,"id":83785166,"diff_hunk":"@@ -0,0 +1,219 @@\n+/*\n+ * Copyright (c) 2016 DeNA Co., Ltd., Ichito Nagata\n+ *\n+ * Permission is hereby granted, free of charge, to any person obtaining a copy\n+ * of this software and associated documentation files (the \"Software\"), to\n+ * deal in the Software without restriction, including without limitation the\n+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or\n+ * sell copies of the Software, and to permit persons to whom the Software is\n+ * furnished to do so, subject to the following conditions:\n+ *\n+ * The above copyright notice and this permission notice shall be included in\n+ * all copies or substantial portions of the Software.\n+ *\n+ * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\n+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS\n+ * IN THE SOFTWARE.\n+ */\n+#include \"async.h\"\n+#include \"h2o/redis.h\"\n+#include \"h2o/socket.h\"\n+\n+static int attach_loop(redisAsyncContext* ac, h2o_loop_t* loop);\n+\n+struct st_h2o_redis_conn_t {\n+    redisAsyncContext *redis;\n+    h2o_redis_callbacks_t cb;\n+    void *cb_data;\n+};\n+\n+struct st_h2o_redis_callback_args_t {\n+    h2o_redis_command_cb cb;\n+    void *data;\n+};\n+\n+void on_command(redisAsyncContext *redis, void *reply, void *privdata)\n+{\n+    struct st_h2o_redis_callback_args_t *args = (struct st_h2o_redis_callback_args_t *)privdata;\n+    if (args != NULL) {\n+        args->cb((redisReply *)reply, args->data);\n+        free(args);\n+    }\n+}\n+\n+void h2o_redis_command(h2o_redis_conn_t *conn, h2o_redis_command_cb cb, void *cb_data, const char *format, ...)\n+{\n+    if (conn->redis == NULL) {\n+        if (conn->cb.on_connection_failure != NULL) {\n+            conn->cb.on_connection_failure(\"redis connection has been closed\", conn->cb_data);\n+        }\n+        return;\n+    }\n+\n+    struct st_h2o_redis_callback_args_t *args = NULL;\n+    if (cb != NULL) {\n+        args = h2o_mem_alloc(sizeof(struct st_h2o_redis_callback_args_t));\n+        args->cb = cb;\n+        args->data = cb_data;\n+    }\n+\n+    va_list ap;\n+    va_start(ap, format);\n+    if (redisvAsyncCommand(conn->redis, on_command, args, format, ap) != REDIS_OK) {\n+        /* the case that redisAsyncContext is disconnecting or freeing */\n+        /* call the callback immediately with NULL reply */\n+        on_command(conn->redis, NULL, args);\n+    }\n+    va_end(ap);\n+}\n+\n+static void on_redis_connect(const redisAsyncContext *redis, int status)\n+{\n+    h2o_redis_conn_t *conn = (h2o_redis_conn_t *)redis->data;\n+    if (status == REDIS_OK) {\n+        if (conn->cb.on_connect != NULL) {\n+            conn->cb.on_connect(conn->cb_data);\n+        }\n+    } else {\n+        conn->redis = NULL;\n+        if (conn->cb.on_connection_failure != NULL) {\n+            conn->cb.on_connection_failure(redis->errstr, conn->cb_data);\n+        }\n+    }\n+}\n+\n+static void on_redis_disconnect(const redisAsyncContext *redis, int status)\n+{\n+    h2o_redis_conn_t *conn = (h2o_redis_conn_t *)redis->data;\n+    if (status == REDIS_OK) {\n+        if (conn->cb.on_disconnect != NULL) {\n+            conn->cb.on_disconnect(conn->cb_data);\n+        }\n+    } else {\n+        conn->redis = NULL;\n+        if (conn->cb.on_connection_failure != NULL) {\n+            conn->cb.on_connection_failure(redis->errstr, conn->cb_data);\n+        }\n+    }\n+}\n+\n+h2o_redis_conn_t *h2o_redis_connect(h2o_loop_t *loop, const char *host, uint16_t port, h2o_redis_callbacks_t cb, void *cb_data)\n+{\n+    h2o_redis_conn_t *conn = h2o_mem_alloc(sizeof(*conn));\n+    *conn = (h2o_redis_conn_t){NULL};\n+\n+    redisAsyncContext *redis = redisAsyncConnect(host, port);\n+\n+    if (redis == NULL || attach_loop(redis, loop) != 0) {\n+        /* failed to allocate memory */\n+        goto Error;\n+    }\n+\n+    conn->cb = cb;\n+    conn->cb_data = cb_data;\n+\n+    if (redis->err == REDIS_OK) {\n+        conn->redis = redis;\n+        conn->redis->data = conn;\n+        redisAsyncSetConnectCallback(redis, on_redis_connect);\n+        redisAsyncSetDisconnectCallback(redis, on_redis_disconnect);\n+    } else {\n+        /* some connection failures can be detected at this time */\n+        if (conn->cb.on_connection_failure != NULL) {\n+            conn->cb.on_connection_failure(redis->errstr, conn->cb_data);\n+        }\n+        redisAsyncFree(redis);\n+    }\n+\n+    return conn;\n+\n+Error:\n+    if (redis != NULL)\n+        redisAsyncFree(redis);\n+    free(conn);\n+    return NULL;\n+}\n+\n+void h2o_redis_free(h2o_redis_conn_t *conn)\n+{\n+    if (conn->redis != NULL) {\n+        redisAsyncDisconnect(conn->redis);\n+    }\n+    free(conn);\n+}\n+\n+/* redis socket adapter */\n+\n+struct st_redis_socket_data {\n+    redisAsyncContext *context;\n+    h2o_socket_t *socket;\n+};\n+\n+static void on_read(h2o_socket_t* sock, const char *err)\n+{\n+    struct st_redis_socket_data *p = (struct st_redis_socket_data *)sock->data;\n+    redisAsyncHandleRead(p->context);\n+}\n+\n+static void on_write(h2o_socket_t *sock, const char *err)\n+{\n+    struct st_redis_socket_data *p = (struct st_redis_socket_data *)sock->data;\n+    redisAsyncHandleWrite(p->context);\n+}\n+\n+static void socket_add_read(void *privdata) {\n+    struct st_redis_socket_data *p = (struct st_redis_socket_data *)privdata;\n+    h2o_socket_read_start(p->socket, on_read);\n+}\n+\n+\n+static void socket_del_read(void *privdata) {\n+    struct st_redis_socket_data *p = (struct st_redis_socket_data *)privdata;\n+    h2o_socket_read_stop(p->socket);\n+}\n+\n+\n+static void socket_add_write(void *privdata) {\n+    struct st_redis_socket_data *p = (struct st_redis_socket_data *)privdata;\n+    if (! h2o_socket_is_writing(p->socket)) {\n+        h2o_socket_notify_write(p->socket, on_write);\n+    }\n+}\n+\n+static void socket_cleanup(void *privdata) {\n+    struct st_redis_socket_data *p = (struct st_redis_socket_data *)privdata;\n+    h2o_socket_close(p->socket);\n+    p->context->c.fd = -1; /* prevent hiredis from closing fd twice */\n+    free(p);\n+}\n+\n+static int attach_loop(redisAsyncContext* ac, h2o_loop_t* loop) {\n+    redisContext *c = &(ac->c);\n+\n+    struct st_redis_socket_data *p = (struct st_redis_socket_data *)malloc(sizeof(*p));\n+    if (p == NULL) {\n+        return -1;\n+    }\n+    memset(p, 0, sizeof(*p));","path":"lib/common/redis.c","position":201,"original_position":201,"commit_id":"bead9015cda328b45a61dc0e833884c98d76983a","original_commit_id":"bead9015cda328b45a61dc0e833884c98d76983a","user":{"login":"kazuho","id":41567,"avatar_url":"https://avatars.githubusercontent.com/u/41567?v=3","gravatar_id":"","url":"https://api.github.com/users/kazuho","html_url":"https://github.com/kazuho","followers_url":"https://api.github.com/users/kazuho/followers","following_url":"https://api.github.com/users/kazuho/following{/other_user}","gists_url":"https://api.github.com/users/kazuho/gists{/gist_id}","starred_url":"https://api.github.com/users/kazuho/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kazuho/subscriptions","organizations_url":"https://api.github.com/users/kazuho/orgs","repos_url":"https://api.github.com/users/kazuho/repos","events_url":"https://api.github.com/users/kazuho/events{/privacy}","received_events_url":"https://api.github.com/users/kazuho/received_events","type":"User","site_admin":false},"body":"As a general rule, I recommend using compound assignment for initializing a structure. In this case, `*p = (struct ...){NULL}`?","created_at":"2016-10-18T05:50:13Z","updated_at":"2016-10-18T05:56:37Z","html_url":"https://github.com/h2o/h2o/pull/1087#discussion_r83785166","pull_request_url":"https://api.github.com/repos/h2o/h2o/pulls/1087","_links":{"self":{"href":"https://api.github.com/repos/h2o/h2o/pulls/comments/83785166"},"html":{"href":"https://github.com/h2o/h2o/pull/1087#discussion_r83785166"},"pull_request":{"href":"https://api.github.com/repos/h2o/h2o/pulls/1087"}}},"pull_request":{"url":"https://api.github.com/repos/h2o/h2o/pulls/1087","id":86690886,"html_url":"https://github.com/h2o/h2o/pull/1087","diff_url":"https://github.com/h2o/h2o/pull/1087.diff","patch_url":"https://github.com/h2o/h2o/pull/1087.patch","issue_url":"https://api.github.com/repos/h2o/h2o/issues/1087","number":1087,"state":"open","locked":false,"title":"I110/redis session resumption","user":{"login":"i110","id":969287,"avatar_url":"https://avatars.githubusercontent.com/u/969287?v=3","gravatar_id":"","url":"https://api.github.com/users/i110","html_url":"https://github.com/i110","followers_url":"https://api.github.com/users/i110/followers","following_url":"https://api.github.com/users/i110/following{/other_user}","gists_url":"https://api.github.com/users/i110/gists{/gist_id}","starred_url":"https://api.github.com/users/i110/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/i110/subscriptions","organizations_url":"https://api.github.com/users/i110/orgs","repos_url":"https://api.github.com/users/i110/repos","events_url":"https://api.github.com/users/i110/events{/privacy}","received_events_url":"https://api.github.com/users/i110/received_events","type":"User","site_admin":false},"body":"make it able to use Redis as session cache backend.","created_at":"2016-09-26T07:06:40Z","updated_at":"2016-10-18T05:56:37Z","closed_at":null,"merged_at":null,"merge_commit_sha":"46aa60cf64c8445615d7a66c2ac5e9ff5d2b3297","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/h2o/h2o/pulls/1087/commits","review_comments_url":"https://api.github.com/repos/h2o/h2o/pulls/1087/comments","review_comment_url":"https://api.github.com/repos/h2o/h2o/pulls/comments{/number}","comments_url":"https://api.github.com/repos/h2o/h2o/issues/1087/comments","statuses_url":"https://api.github.com/repos/h2o/h2o/statuses/bead9015cda328b45a61dc0e833884c98d76983a","head":{"label":"h2o:i110/redis-session-resumption","ref":"i110/redis-session-resumption","sha":"bead9015cda328b45a61dc0e833884c98d76983a","user":{"login":"h2o","id":9275116,"avatar_url":"https://avatars.githubusercontent.com/u/9275116?v=3","gravatar_id":"","url":"https://api.github.com/users/h2o","html_url":"https://github.com/h2o","followers_url":"https://api.github.com/users/h2o/followers","following_url":"https://api.github.com/users/h2o/following{/other_user}","gists_url":"https://api.github.com/users/h2o/gists{/gist_id}","starred_url":"https://api.github.com/users/h2o/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/h2o/subscriptions","organizations_url":"https://api.github.com/users/h2o/orgs","repos_url":"https://api.github.com/users/h2o/repos","events_url":"https://api.github.com/users/h2o/events{/privacy}","received_events_url":"https://api.github.com/users/h2o/received_events","type":"Organization","site_admin":false},"repo":{"id":23029617,"name":"h2o","full_name":"h2o/h2o","owner":{"login":"h2o","id":9275116,"avatar_url":"https://avatars.githubusercontent.com/u/9275116?v=3","gravatar_id":"","url":"https://api.github.com/users/h2o","html_url":"https://github.com/h2o","followers_url":"https://api.github.com/users/h2o/followers","following_url":"https://api.github.com/users/h2o/following{/other_user}","gists_url":"https://api.github.com/users/h2o/gists{/gist_id}","starred_url":"https://api.github.com/users/h2o/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/h2o/subscriptions","organizations_url":"https://api.github.com/users/h2o/orgs","repos_url":"https://api.github.com/users/h2o/repos","events_url":"https://api.github.com/users/h2o/events{/privacy}","received_events_url":"https://api.github.com/users/h2o/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/h2o/h2o","description":"H2O - the optimized HTTP/1, HTTP/2 server","fork":false,"url":"https://api.github.com/repos/h2o/h2o","forks_url":"https://api.github.com/repos/h2o/h2o/forks","keys_url":"https://api.github.com/repos/h2o/h2o/keys{/key_id}","collaborators_url":"https://api.github.com/repos/h2o/h2o/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/h2o/h2o/teams","hooks_url":"https://api.github.com/repos/h2o/h2o/hooks","issue_events_url":"https://api.github.com/repos/h2o/h2o/issues/events{/number}","events_url":"https://api.github.com/repos/h2o/h2o/events","assignees_url":"https://api.github.com/repos/h2o/h2o/assignees{/user}","branches_url":"https://api.github.com/repos/h2o/h2o/branches{/branch}","tags_url":"https://api.github.com/repos/h2o/h2o/tags","blobs_url":"https://api.github.com/repos/h2o/h2o/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/h2o/h2o/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/h2o/h2o/git/refs{/sha}","trees_url":"https://api.github.com/repos/h2o/h2o/git/trees{/sha}","statuses_url":"https://api.github.com/repos/h2o/h2o/statuses/{sha}","languages_url":"https://api.github.com/repos/h2o/h2o/languages","stargazers_url":"https://api.github.com/repos/h2o/h2o/stargazers","contributors_url":"https://api.github.com/repos/h2o/h2o/contributors","subscribers_url":"https://api.github.com/repos/h2o/h2o/subscribers","subscription_url":"https://api.github.com/repos/h2o/h2o/subscription","commits_url":"https://api.github.com/repos/h2o/h2o/commits{/sha}","git_commits_url":"https://api.github.com/repos/h2o/h2o/git/commits{/sha}","comments_url":"https://api.github.com/repos/h2o/h2o/comments{/number}","issue_comment_url":"https://api.github.com/repos/h2o/h2o/issues/comments{/number}","contents_url":"https://api.github.com/repos/h2o/h2o/contents/{+path}","compare_url":"https://api.github.com/repos/h2o/h2o/compare/{base}...{head}","merges_url":"https://api.github.com/repos/h2o/h2o/merges","archive_url":"https://api.github.com/repos/h2o/h2o/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/h2o/h2o/downloads","issues_url":"https://api.github.com/repos/h2o/h2o/issues{/number}","pulls_url":"https://api.github.com/repos/h2o/h2o/pulls{/number}","milestones_url":"https://api.github.com/repos/h2o/h2o/milestones{/number}","notifications_url":"https://api.github.com/repos/h2o/h2o/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/h2o/h2o/labels{/name}","releases_url":"https://api.github.com/repos/h2o/h2o/releases{/id}","deployments_url":"https://api.github.com/repos/h2o/h2o/deployments","created_at":"2014-08-16T23:59:03Z","updated_at":"2016-10-18T04:58:30Z","pushed_at":"2016-10-18T04:43:23Z","git_url":"git://github.com/h2o/h2o.git","ssh_url":"git@github.com:h2o/h2o.git","clone_url":"https://github.com/h2o/h2o.git","svn_url":"https://github.com/h2o/h2o","homepage":"https://h2o.examp1e.net","size":58377,"stargazers_count":5988,"watchers_count":5988,"language":"C","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":459,"mirror_url":null,"open_issues_count":260,"forks":459,"open_issues":260,"watchers":5988,"default_branch":"master"}},"base":{"label":"h2o:master","ref":"master","sha":"5d422ce12ef41fb05c00f39f8e34c93311586a42","user":{"login":"h2o","id":9275116,"avatar_url":"https://avatars.githubusercontent.com/u/9275116?v=3","gravatar_id":"","url":"https://api.github.com/users/h2o","html_url":"https://github.com/h2o","followers_url":"https://api.github.com/users/h2o/followers","following_url":"https://api.github.com/users/h2o/following{/other_user}","gists_url":"https://api.github.com/users/h2o/gists{/gist_id}","starred_url":"https://api.github.com/users/h2o/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/h2o/subscriptions","organizations_url":"https://api.github.com/users/h2o/orgs","repos_url":"https://api.github.com/users/h2o/repos","events_url":"https://api.github.com/users/h2o/events{/privacy}","received_events_url":"https://api.github.com/users/h2o/received_events","type":"Organization","site_admin":false},"repo":{"id":23029617,"name":"h2o","full_name":"h2o/h2o","owner":{"login":"h2o","id":9275116,"avatar_url":"https://avatars.githubusercontent.com/u/9275116?v=3","gravatar_id":"","url":"https://api.github.com/users/h2o","html_url":"https://github.com/h2o","followers_url":"https://api.github.com/users/h2o/followers","following_url":"https://api.github.com/users/h2o/following{/other_user}","gists_url":"https://api.github.com/users/h2o/gists{/gist_id}","starred_url":"https://api.github.com/users/h2o/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/h2o/subscriptions","organizations_url":"https://api.github.com/users/h2o/orgs","repos_url":"https://api.github.com/users/h2o/repos","events_url":"https://api.github.com/users/h2o/events{/privacy}","received_events_url":"https://api.github.com/users/h2o/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/h2o/h2o","description":"H2O - the optimized HTTP/1, HTTP/2 server","fork":false,"url":"https://api.github.com/repos/h2o/h2o","forks_url":"https://api.github.com/repos/h2o/h2o/forks","keys_url":"https://api.github.com/repos/h2o/h2o/keys{/key_id}","collaborators_url":"https://api.github.com/repos/h2o/h2o/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/h2o/h2o/teams","hooks_url":"https://api.github.com/repos/h2o/h2o/hooks","issue_events_url":"https://api.github.com/repos/h2o/h2o/issues/events{/number}","events_url":"https://api.github.com/repos/h2o/h2o/events","assignees_url":"https://api.github.com/repos/h2o/h2o/assignees{/user}","branches_url":"https://api.github.com/repos/h2o/h2o/branches{/branch}","tags_url":"https://api.github.com/repos/h2o/h2o/tags","blobs_url":"https://api.github.com/repos/h2o/h2o/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/h2o/h2o/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/h2o/h2o/git/refs{/sha}","trees_url":"https://api.github.com/repos/h2o/h2o/git/trees{/sha}","statuses_url":"https://api.github.com/repos/h2o/h2o/statuses/{sha}","languages_url":"https://api.github.com/repos/h2o/h2o/languages","stargazers_url":"https://api.github.com/repos/h2o/h2o/stargazers","contributors_url":"https://api.github.com/repos/h2o/h2o/contributors","subscribers_url":"https://api.github.com/repos/h2o/h2o/subscribers","subscription_url":"https://api.github.com/repos/h2o/h2o/subscription","commits_url":"https://api.github.com/repos/h2o/h2o/commits{/sha}","git_commits_url":"https://api.github.com/repos/h2o/h2o/git/commits{/sha}","comments_url":"https://api.github.com/repos/h2o/h2o/comments{/number}","issue_comment_url":"https://api.github.com/repos/h2o/h2o/issues/comments{/number}","contents_url":"https://api.github.com/repos/h2o/h2o/contents/{+path}","compare_url":"https://api.github.com/repos/h2o/h2o/compare/{base}...{head}","merges_url":"https://api.github.com/repos/h2o/h2o/merges","archive_url":"https://api.github.com/repos/h2o/h2o/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/h2o/h2o/downloads","issues_url":"https://api.github.com/repos/h2o/h2o/issues{/number}","pulls_url":"https://api.github.com/repos/h2o/h2o/pulls{/number}","milestones_url":"https://api.github.com/repos/h2o/h2o/milestones{/number}","notifications_url":"https://api.github.com/repos/h2o/h2o/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/h2o/h2o/labels{/name}","releases_url":"https://api.github.com/repos/h2o/h2o/releases{/id}","deployments_url":"https://api.github.com/repos/h2o/h2o/deployments","created_at":"2014-08-16T23:59:03Z","updated_at":"2016-10-18T04:58:30Z","pushed_at":"2016-10-18T04:43:23Z","git_url":"git://github.com/h2o/h2o.git","ssh_url":"git@github.com:h2o/h2o.git","clone_url":"https://github.com/h2o/h2o.git","svn_url":"https://github.com/h2o/h2o","homepage":"https://h2o.examp1e.net","size":58377,"stargazers_count":5988,"watchers_count":5988,"language":"C","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":459,"mirror_url":null,"open_issues_count":260,"forks":459,"open_issues":260,"watchers":5988,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/h2o/h2o/pulls/1087"},"html":{"href":"https://github.com/h2o/h2o/pull/1087"},"issue":{"href":"https://api.github.com/repos/h2o/h2o/issues/1087"},"comments":{"href":"https://api.github.com/repos/h2o/h2o/issues/1087/comments"},"review_comments":{"href":"https://api.github.com/repos/h2o/h2o/pulls/1087/comments"},"review_comment":{"href":"https://api.github.com/repos/h2o/h2o/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/h2o/h2o/pulls/1087/commits"},"statuses":{"href":"https://api.github.com/repos/h2o/h2o/statuses/bead9015cda328b45a61dc0e833884c98d76983a"}}}}
{"actor":{"display_login":"kazuho"}}