Question

This question is a continuation of Including the Erlang client lib which was asked before.

I was able to include the library by changing:

{mimetypes,     ".*",   {git, "git://github.com/spawngrid/mimetypes",   {branch, master}}},

%% Uncomment the following lines and comment the bottom lines with specific
%% tags to always pull the latest versions
{nitrogen_core, ".*",   {git, "git://github.com/nitrogen/nitrogen_core",{branch, master}}},

to:

{mimetypes,     ".*",   {git, "git://github.com/spawngrid/mimetypes",   {branch, master}}},

{riakc, "1.4.1",
          {git, "git://github.com/basho/riak-erlang-client", 
                       {tag, "1.4.1"}}},


%% Uncomment the following lines and comment the bottom lines with specific
%% tags to always pull the latest versions
{nitrogen_core, ".*",   {git, "git://github.com/nitrogen/nitrogen_core",{branch, master}}},

in rel/nitrogen/rebar.config and recompiling with make

Now that I have those installed under the lib folder, I'm not sure where I should be implementing the riakc_pb_socket lib as suggested in the docs

I've tried putting

{ok, Pid} = riakc_pb_socket:start_link("127.0.0.1", 8087),

into nitrogen_sup:init() but i get this error message:

application: nitrogen
exited: {{{badmatch,{error,{tcp,econnrefused}}},
          [{nitrogen_sup,init,1,
                         [{file,"/home/neil/proj/nitrogen/rel/nitrogen/site/src/nitrogen_sup.erl"},
                          {line,43}]},
           {supervisor,init,1,[{file,"supervisor.erl"},{line,239}]},
           {gen_server,init_it,6,[{file,"gen_server.erl"},{line,304}]},
           {proc_lib,init_p_do_apply,3,
                     [{file,"proc_lib.erl"},{line,239}]}]},
         {nitrogen_app,start,[normal,[]]}}
type: temporary

Am I supposed to make this connection to riak database once during the app initialization, or often as new requests are handled. I saw some talk of connection pools, would this be something that I setup once during app initialization, then link to with new processes.

I'm new to erlang/OTP and this framework, so any direction will be greatly appreciated.

added:

When I run the nitrogen app via bin/nitrogen console, I am able to run {ok, Pid} = riakc_pb_socket:start_link("127.0.0.1",8087). and I do manage to get a pong back from riakc_pb_socket:ping(Pid).

I guess the question now is: In which files does one generally setup/manage riak connections with respect to queries/reads/writes?

Was it helpful?

Solution

A way to get-things-going is:

  1. Create a gen_server which you add to the supervisor tree of an application. Preferably your own application which you include.
  2. When the gen_server initializes, it sets up a Riak connection. Whenever you want to use the connection, you call the gen_server and since it has the connection, it will be able to issue queries.
  3. Handle reconnects. If the connection is lost, you crash the gen_server. If the connection is refused upon initialization, you wait a bit and try again.

Often, you will see a separate application running the "backend" stuff of an Erlang system and then another application, like nitrogen, to handle the Web-stuff. Your gen_server would belong to the "backend" part.

Once this basis works you can extend it further:

  • Your gen_server will be living in a module named foo. Whenever you want to use riak, you call foo:f(...). Rewrite foo:f(...) to use http://github.com/devinus/poolboy or something such to support connection pooling toward the Riak cluster. But only do this once it proves that a single connection is too slow.

This neatly shows the idea of loose coupling in Erlang programs. The foo module acts like an interface, but you don't know the implementation behind that interface. You can change it later for something faster should the need arise. The only thing you need to do is to implement the correct protocol, which is a function call in this case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top