Working with Erlang records in Elixir
Posted By nick, December 21, 2015
Elixir is a great way to hop on the power of the Erlang runtime with a more friendly syntax. Working with Erlang records can be a bit un-intuitive though, here’s how we are dealing with it.
The Elixir Record module handles interoperating with Erlang records, and we can use it for a quick test:
But how do we use existing Erlang records, defined externally? For example, we are using the erlcloud library to access Amazon S3 resources, some of which can be quite large. The erlcloud library sets a default timeout of 10 seconds for get_object requests, which doesn’t fly with objects that can be 100+ MB in size.
If we request a large enough file and it takes more than 10 seconds to download, we get this timeout:
Each function in the erlcloud library takes an optional Config parameter, which is specified to be an aws_config record. The aws_config record is defined here, and includes a timeout field we can set, here’s a snippet:
In an existing project, we can define a module to house records, or use them in an existing module, for example purposes we’ll make a new one. We use Record.extract/2 to pull in the existing definition.
Where the from_lib path is the location of our erlcloud dependency’s include file mentioned above, relative to the library’s path.
Now we can use this to define a default record:
erlcloud will also offer up its default configuration, one with more useful values if we want to override just the timeout value. We get this via :erlcloud_aws.default_config:
From here we can make a new copy w/the timeout value changed to 30 seconds:
Notice the 30000 where there once was 10000. From here, we can pass it on to the get_object function and have a little more patient request: