[erlang-questions] diameter callback module

Anders Svensson <>
Tue Apr 30 11:04:57 CEST 2013


Hi Samuel.

On Tue, Apr 30, 2013 at 4:35 AM, S X <> wrote:
> Hi Anders,
>
> Thanks for your explanation. Setting additional options in
> diameter:add_transport and retrieving them with diameter:service_info from
> the peer_up callback work well. And the descriptions about the peer states
> according to RFC3539 and 6733 make a lot of sense.
>
> The case I am trying to resolve is:
> Start multiple processes on one pc, each process tries to connect to one
> same server (obviously I will get already_connected warning, but it can keep
> going), in each process, then I try to send a diameter message to the
> server, I used to insert a certain amount of sleep time before I send the
> message after connecting to the server and it works ok. But I wanted to use
> the peer up callback to behave like a handshaking procedure among the client
> and the server, so the server is supposed to be ready when the client
> process sends the message.
>
> Now, the fact is that only one peer up callback is invoked for multiple
> processes. Then I tried to notify all the processes in the peer up callback
> when invoked and the processes send out the diameter messages. However, the
> processes don't receive any responses. Or sometimes only part of the
> processes get responses. Why inserting an amount of sleep time always works.
>
> This is a little bit confusing me, how peer up should be used properly to
> get the notification done right for multiple processes.

It's difficult to say what your problem is without seeing code but
here are a few points.

It's not your client processes that connect to the server. A transport
connection results from someone calling diameter:add_transport/2. Once
diameter establishes the connection, successfully performs
capabilities exchange, and makes peer_up callbacks, that transport
connection is available to any process that wants to send to the peer
in question. In your case, you have multiple processes that want to do
so, but if you're calling diameter:add_transport/2 with the same
config from each of those processes then you're effectively telling
diameter to establish multiple transport connections, only one of
which will succeed by default. If you want to allow multiple transport
connections per peer then you need to configure your service with
{restrict_connections, false}, but the peer need not support this (see
2.1 in RFC 6733) so may reject all but one CER. Without
restrict_connections it's diameter that disallows multiple
connections.

Given that you want multiple client processes to send from (not wrong,
but why?), I would treat these as workers that have nothing to do with
configuration. That is, call diameter:start_service/2 and
diameter:add_transport/2 from elsewhere and have your workers kick
into action after capabilities exchange.

Like you said, your workers want to know when the peer becomes
available. There are two ways to get notification: a peer_up callback
or a diameter event. The latter are subscribed to with
diameter:subscribe/1 so one way is for each worker to subscribe to
events and react to them. If you want to use peer_up as a trigger then
you need to code some way for the callback to notify your worker
processes. There are plenty of ways to do this, none of which are
diameter-specific. For example: maintain your worker pids in an ets
table that the callback examines; have workers register with a
registered gen_server that finds out about callbacks; spawn workers as
a result of peer_up. Sleep is never a reliable solution.

Anders


>
> Thanks for any suggestions!
>
> Samuel
>
>
>
>
> On Thu, Apr 25, 2013 at 5:40 AM, Anders Svensson <>
> wrote:
>>
>> On Wed, Apr 24, 2013 at 5:23 PM, S X <> wrote:
>> > Thanks a lot for your comments.
>> >
>> > The diameter:call function allows to send extra arguments. It works fine
>> > after changing the callback function signatures accordingly. This is
>> > very
>> > useful when I want to notify the other process to do the next thing.
>> >
>> > Before sending a diameter message, we call diameter:add_transport to
>> > connect
>> > to a peer and peform CER/CEA capability information exchange. On the
>> > caller
>> > side, let's say the client callback module, the peer_up will be invoked
>> > when
>> > CER/CEA is completed. Why it doesn't have the similar mechanism like
>> > diameter:call to allow insert additional arguments so we can utilize
>> > them,
>> > for example, notify the others to send diameter messages?
>>
>> No particular reason aside from history and that the need hasn't come
>> up. As it is, you can pass arbitrary options to
>> diameter:add_transport/2 (history again) and retrieve these in a
>> callback with diameter:service_info(PeerRef), so that can be used a
>> substitute for extra arguments in this case.
>>
>> > diameter:add_transport is a sync call, but it doesn't mean you can send
>>
>> add_transport *doesn't* wait for the config it's passed to result in a
>> connection before returning. (It might never happen for one.)
>>
>> > diameter messages successfully when the function returns, i.e. it
>> > usually
>> > gets {error, no_connection} or { error, timeout }(this might because of
>> > the
>> > server side) if you call diameter:call right after
>> > diameter:add_transport.
>>
>> This is because the peer won't be ready to respond to requests until
>> capabilities exchange has completed (at least). If you call before the
>> relevant peer_up callback then the result is {error, no_connection}.
>> After peer_up the result will be {error, timeout} if the peer doesn't
>> answer. One case in which this is expected is after a connection has
>> been reestablished following an exchange of 3 x DWR/DWA. (ie. RFC 3539
>> DOWN -> REOPEN -> OKAY.) Since both ends of the connection do this,
>> the client can consider the connection to be reestablished before the
>> server. If the client sends a request before the server is done with
>> it's exchange (ie. reached OKAY) then RFC 6733 says it should discard
>> the request, resulting in a timeout on the client end.
>>
>> > So is there any reason why not allowing to add additional arguments and
>> > use
>> > them in peer_up callback function? Since it means capability exchange is
>> > done and the peer is ready, at this point it should be safe to send
>> > diameter
>> > messages to the peer.
>>
>> See above.
>>
>> Anders
>>
>>
>> >
>> > Any suggestions?
>> >
>> > Thanks!
>> >
>> > Samuel
>> >
>> >
>> > On Tue, Apr 16, 2013 at 7:22 AM, Anders Svensson <>
>> > wrote:
>> >>
>> >> On Sun, Apr 14, 2013 at 7:01 PM, S X <> wrote:
>> >> > Hello,
>> >> >
>> >> > Based on the erlang diameter library and the sample code, I want to
>> >> > start
>> >> > multiple diameter client processes in one erlang node(one client IP),
>> >> > and
>> >> > the client needs to define a diameter_app callback module for certain
>> >> > application, for example:
>> >> >
>> >> > -define(SERVICE(Name), [{'Origin-Host', ?L(Name) ++ ".example.com"},
>> >> >                         {'Origin-Realm', "example.com"},
>> >> >                         {'Vendor-Id', 193},
>> >> >                         {'Product-Name', "Client"},
>> >> >                         {'Auth-Application-Id',
>> >> > [?DIAMETER_APP_ID_COMMON,
>> >> > ?DIAMETER_APP_ID_CCRA]},
>> >> >                         {application, [{alias, ?APP_CCR_ALIAS},
>> >> >                                        {dictionary,
>> >> > ?DIAMETER_DICT_CCRA},
>> >> >                                        {module, client_cb_ccra}]}]).
>> >> >
>> >> >
>> >> > First question:
>> >> > how the diameter library handles this situation? Will all diameter
>> >> > client
>> >> > processes share one single diameter_app callback module
>> >> > "client_cb_ccra"
>> >> > or
>> >> > it will automatically attach different instance of the callback
>> >> > module
>> >> > (process) to the different client process by using spawn_monitor? So
>> >> > from
>> >> > the callback handle_answer in "client_cb_ccra" I can notify the
>> >> > proper
>> >> > client proce by just calling client:notify() something.
>> >>
>> >> diameter doesn't know anything about your client process. If you want
>> >> a callback to be able to contact the client process associated with
>> >> the service in question (ie. the service whose name the callback gets
>> >> as an argument) then you need to give the callback the means to do so.
>> >> One way would be to map the service name to your process (eg. it's the
>> >> registered name of your client process), another would be to pass some
>> >> identification as extra arguments to the callbacks. (Eg. {module,
>> >> [client_cb_ccra, X]} in the config above.)
>> >>
>> >> > Second question:
>> >> > Notice that diameter:call allows to set extra arguments, so I was
>> >> > wondering
>> >> > I could set some data like:
>> >> > diameter:call(Name, ?APP_CCR_ALIAS, CCR, [{extra, [self()]}]).
>> >> > which sets the client process ID and I hope to deliver to the
>> >> > callback
>> >> > module "client_cb_ccra" and when the callback module knows which
>> >> > client
>> >> > process sends request and response accordingly, for example using the
>> >> > client
>> >> > process ID in the callback function handle_answer.
>> >>
>> >> You can do that, but what is it you're trying to accomplish? if it's
>> >> handle_answer that's supposed to communicate something then it already
>> >> does: the return value of handle_answer is returned by diameter:call.
>> >>
>> >> > However, I don't want pack the extra arguments into the diameter
>> >> > packet
>> >> > since the diameter server doesn't know what they are and the extra
>> >> > ones
>> >> > are
>> >> > not part of standard diameter packet.
>> >>
>> >> Not sure what you mean here. There's no way for your client to send
>> >> the server anything other than a Diameter message.
>> >>
>> >> > Now I changed the pick_peer callback signatures to allow extra
>> >> > arguments,
>> >> > pick_peer([Peer | _], _, _SvcName, _State, A)
>> >> >
>> >> > But I got encoding error in the callback prepare_request
>> >> >
>> >> > =ERROR REPORT==== 14-Apr-2013::11:40:41 ===
>> >> > Error in process <0.175.0> with exit value:
>> >> > {undef,[{client_cb_ccra,prepare_request,[{diameter_packet,
>> >>
>> >> The arity of your callback probably doesn't agree with the number of
>> >> extra arguments you've specified: prepare_request will also get your
>> >> extra arguments.
>> >>
>> >>
>> >> >
>> >> > > >{diameter_header,1,undefined,undefined,undefined,3958849953,3958849953,undefined,undefined,undefined,undefined},undefined,{diameter_rfc4006_cc_CCR,["who",";","142745567...
>> >> >
>> >> > {error,encode}
>> >> >
>> >> > In overall, I read the online documents, which have limited
>> >> > information
>> >> > on
>> >> > how to use the extra arguments in the library and  don't quite get
>> >> > how
>> >> > to
>> >> > utilize the extra arguments to do something tricky,
>> >> >
>> >> > Are there any suggestions on how to deal with multiple client
>> >> > processes?
>> >>
>> >> Not sure I understand what problem it is you're trying to solve.
>> >> Multiple processes invoking diameter:call is nothing strange. You
>> >> typically just return something useful (eg. the answer message from
>> >> the peer) from handle_answer, which diameter:call then returns for the
>> >> caller to deal with.
>> >>
>> >> Anders
>> >>
>> >>
>> >> > Thanks a lot!
>> >> >
>> >> > Samuel
>> >> >
>> >
>> >
>
>


More information about the erlang-questions mailing list