{"id":5387,"date":"2011-10-25T17:27:01","date_gmt":"2011-10-25T22:27:01","guid":{"rendered":"http:\/\/bobbeaty.com\/wp\/?p=5387"},"modified":"2011-10-26T07:53:34","modified_gmt":"2011-10-26T12:53:34","slug":"creating-a-solid-reliable-c-wrapper-for-hiredis-library","status":"publish","type":"post","link":"https:\/\/bobbeaty.com\/wp\/archives\/5387","title":{"rendered":"Creating a Solid, Reliable C++ Wrapper for hiredis Library"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/bobbeaty.com\/wp\/wp-content\/uploads\/2011\/10\/redis.jpg\" alt=\"Redis Database\" title=\"redis.jpg\" border=\"0\" width=\"125\" height=\"104\" style=\"float:right;\" \/><\/p>\n<p>Most of today has been spent trying to get my simple C++ wrapper around the <a href=\"https:\/\/github.com\/antirez\/hiredis\">hiredis<\/a> C library for <a href=\"http:\/\/redis.io\/\">redis<\/a> working in a way that allows for a significantly more robust usage pattern than I originally had. Specifically, all was fine until I shut off the <a href=\"http:\/\/redis.io\/\">redis<\/a> server, and then my client would try to recover and reconnect and end up dumping core. The problems are only made worse by the fact that I really had <em>no support docs<\/em> on the <a href=\"https:\/\/github.com\/antirez\/hiredis\">hiredis<\/a> site - only the source code, which is <em>optimistic in the extreme<\/em>. No argument checks, etc. make it ripe for a problem if it's not used <strong><em>exactly right<\/em><\/strong>.<\/p>\n<p>Clearly, I wasn't using it <em>exactly right<\/em>, and those misusage patterns were what was causing the code dumps. So the first thing was to track down what I was doing wrong, and that meant that I really needed to become <em>much<\/em> more familiar with the <a href=\"https:\/\/github.com\/antirez\/hiredis\">hiredis<\/a> source code. To be fair, it's a decent open source library, but it's missing so much that would have added so little to the runtime load and would have made it far more robust to the kinds of misusage patterns I had in place. After all, my code worked, so it's not that it was totally wrong, it's just that when things started to go badly, the things that you needed to do become <em>far more important<\/em> than when things are going well.<\/p>\n<p>For example, if I wanted to send multiple commands to the <a href=\"http:\/\/redis.io\/\">redis<\/a> server at once, you can run several <tt>redisAppendCommand()<\/tt> calls, but each really needs to be checked for it's return value. This isn't clear in the code, but it's very important in the actual system. Then there's the calls to <tt>redisGetReply()<\/tt> - typically one for each call to <tt>redisAppendCommand()<\/tt> - but not always. Again, you need to check for the <strong><em>critical<\/em><\/strong> <tt>REDIS_ERR_IO<\/tt> error that indicates that the <a href=\"http:\/\/redis.io\/\">redis<\/a> context (connection object) is now so far gone that it has to be abandoned.<\/p>\n<p>Then there's the reconnection logic. It's not horrible, but you have to be careful that you don't pass in any NULLs. There simply is no checking on the <a href=\"https:\/\/github.com\/antirez\/hiredis\">hiredis<\/a> code to ensure that NULL arguments are skipped. It's simple to do, but it's not there - not at all.<\/p>\n<p>In the end, I got something working, but it was <strong>hours<\/strong> of code dissection and <tt>gdb<\/tt> work to figure out what was going wrong and what needed to be done to handle the disconnected server and then the proper reconnection. Not fun, and several times I was wondering if it just wouldn't be easier to write my own as it's all TCP\/telnet based anyway\u2026 but I kept going and in the end I have something that's reliable and solid. But it was <strong>nasty<\/strong> to get here.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most of today has been spent trying to get my simple C++ wrapper around the hiredis C library for redis working in a way that allows for a significantly more robust usage pattern than I originally had. Specifically, all was fine until I shut off the redis server, and then my client would try to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6,4],"tags":[],"class_list":["post-5387","post","type-post","status-publish","format-standard","hentry","category-coding","category-cube-life","category-open-source-software"],"_links":{"self":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/5387","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/comments?post=5387"}],"version-history":[{"count":1,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/5387\/revisions"}],"predecessor-version":[{"id":5388,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/5387\/revisions\/5388"}],"wp:attachment":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/media?parent=5387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/categories?post=5387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/tags?post=5387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}