Open AlexDaniel opened 6 years ago
save-auth-data
was used to store the access token and the transaction id (txn_id
) so it can persist between runs.
The access token one should be stored by the application and not by the library. That's why I've made the $!access-token
public.
The transaction ID (txn_id
) is an unique ID across requests with the same access token used to send or redact events. It doesn't matter what is as long it is unique. In case you send two events with the same txn_id
, the server might drop the event.
In https://github.com/matiaslina/perl6-matrix-client/commit/1e3e664d19f4808baeecc72784610daef76730bc I've changed the initialization of the txn_id
from 0
to now.Int
. This way is ensured that the txn_id
is unique between requests, so now it won't necessary to save the txn_id
.
Answering the question: it's no necessary to call save-auth-data
anymore.
Note: The examples are outdated. I'll update them with the documentation.
In 1e3e664 I've changed the initialization of the txn_id from 0 to now.Int.
So what if I do two requests within the same second?
There's no problem there. The time is only used as initialization.
i.e.:
# This will send a message with a txn-id of 1528554486 for example
$client.send('!someroom:matrix.org', 'hello');
# Calling again will send a message with a txn-id 1528554487
$client.send('!someroom:matrix.org', 'hello again');
Why not use a random number instead?
A random number has the problem of dropping random messages. With this aproach there's no duplicated IDs while the client is alive.
Is true that there will be problems if the transaction ID goes above now.Int
and you need to reset an application.
The python client actually does this to calculate the next ID
def _make_txn_id(self):
txn_id = str(self.txn_id) + str(int(time() * 1000))
self.txn_id += 1
return txn_id
Which is better than the current implementation or a random number.
Looking at the bot.p6 example: https://github.com/matiaslina/perl6-matrix-client/blob/0a36a8735d99a5aafac7a522affccf403efa6e28/examples/bot.p6#L25-L27
So save-auth-data is called pretty much every time the script is exited.
However, looking at the source for save-auth-data: https://github.com/matiaslina/perl6-matrix-client/blob/0a36a8735d99a5aafac7a522affccf403efa6e28/lib/Matrix/Client.pm6#L48-L56
As far as I understand
access_token
,user_id
, anddevice_id
should stay the same every run. I'm not sure abouttxn_id
, is it used to make sure we don't receive events that were already received? If so, and if I'm writing a bot that actively responds to commands, wouldn't it be better to save the file every time transaction id is changed? Otherwise in case of an unhandled crash (e.g. a segfault) the bot will respond to the same event twice?