ebook img

Elixir in Action PDF

361 Pages·2015·1.868 MB·english
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Elixir in Action

Elixir in Action SAŠA JURIC´ MANNING SHELTER ISLAND ©2015 by Manning Publications Co. ISBN 9781617292019 Printed in the United States of America brief contents PART 1 THE LANGUAGE.............................................................1 1 ■ First steps 3 2 ■ Building blocks 18 3 ■ Control flow 63 4 ■ Data abstractions 101 PART 2 THE PLATFORM.........................................................131 5 ■ Concurrency primitives 133 6 ■ Generic server processes 164 7 ■ Building a concurrent system 181 8 ■ Fault-tolerance basics 201 9 ■ Isolating error effects 222 10 ■ Sharing state 247 PART 3 PRODUCTION............................................................263 11 ■ Working with components 265 12 ■ Building a distributed system 290 13 ■ Running the system 321 contents preface xv acknowledgments xvii about this book xix about the cover illustration xxiii PART 1 THE LANGUAGE....................................................1 1 First steps 3 1.1 About Erlang 3 High availability 4 ■ Erlang concurrency 5 ■ Server-side systems 7 ■ The development platform 9 1.2 About Elixir 10 Code simplification 11 ■ Composing functions 14 ■ The big picture 15 1.3 Disadvantages 15 Speed 15 ■ Ecosystem 16 1.4 Summary 17 2 Building blocks 18 2.1 The interactive shell 19 2.2 Working with variables 21 2.3 Organizing your code 22 Modules 22 ■ Functions 24 ■ Function arity 27 Function visibility 28 ■ Imports and aliases 29 Module attributes 30 ■ Comments 32 2.4 Understanding the type system 32 Numbers 32 ■ Atoms 33 ■ Tuples 36 ■ Lists 37 Immutability 40 ■ Maps 43 ■ Binaries and bitstrings 44 Strings 45 ■ First-class functions 47 ■ Other built-in types 49 ■ Higher-level types 50 ■ IO lists 54 2.5 Operators 55 2.6 Macros 56 2.7 Understanding the runtime 57 Modules and functions in the runtime 57 ■ Starting the runtime 60 2.8 Summary 62 3 Control flow 63 3.1 Pattern matching 64 The match operator 64 ■ Matching tuples 64 ■ Matching constants 65 ■ Variables in patterns 66 ■ Matching lists 67 Matching maps 68 ■ Matching bitstrings and binaries 69 Compound matches 70 ■ General behavior 72 3.2 Matching with functions 72 Multiclause functions 73 ■ Guards 76 ■ Multiclause lambdas 78 3.3 Conditionals 79 Branching with multiclause functions 79 ■ Classical branching constructs 82 3.4 Loops and iterations 85 Iterating with recursion 85 ■ Tail function calls 87 ■ Higher- order functions 90 ■ Comprehensions 94 ■ Streams 96 3.5 Summary 99 4 Data abstractions 101 4.1 Abstracting with modules 103 Basic abstraction 103 ■ Composing abstractions 105 Structuring data with maps 107 ■ Abstracting with structs 108 Data transparency 112 4.2 Working with hierarchical data 114 Generating IDs 114 ■ Updating entries 118 ■ Immutable hierarchical updates 120 ■ Iterative updates 122 Exercise: importing from a file 123 4.3 Polymorphism with protocols 125 Protocol basics 125 ■ Implementing a protocol 126 Built-in protocols 127 4.4 Summary 129 PART 2 THE PLATFORM................................................131 5 Concurrency primitives 133 5.1 Principles 133 5.2 Working with processes 136 Creating processes 137 ■ Message passing 138 5.3 Stateful server processes 143 Server processes 143 ■ Keeping a process state 148 Mutable state 149 ■ Complex states 153 ■ Registered processes 156 5.4 Runtime considerations 158 A process is a bottleneck 158 ■ Unlimited process mailboxes 159 ■ Shared nothing concurrency 160 Scheduler inner workings 161 5.5 Summary 162 6 Generic server processes 164 6.1 Building a generic server process 165 Plugging in with modules 165 ■ Implementing the generic code 166 ■ Using the generic abstraction 167 ■ Supporting asynchronous requests 169 ■ Exercise: refactoring the to-do server 171 6.2 Using gen_server 171 OTP behaviours 172 ■ Plugging into gen_server 173 Handling requests 174 ■ Handling plain messages 175 Other gen_server features 177 ■ Process life cycle 178 OTP-compliant processes 179 ■ Exercise: gen_server-powered to-do server 180 6.3 Summary 180 7 Building a concurrent system 181 7.1 Working with the mix project 182 7.2 Managing multiple to-do lists 184 Implementing a cache 185 ■ Analyzing process dependencies 187 7.3 Persisting data 189 Encoding and persisting 189 ■ Using the database 190 Analyzing the system 193 ■ Addressing the process bottleneck 194 ■ Exercise: pooling and synchronizing 197 7.4 Reasoning with processes 198 7.5 Summary 199 8 Fault-tolerance basics 201 8.1 Runtime errors 202 Error types 203 ■ Handling errors 204 8.2 Errors in concurrent systems 207 Linking processes 208 ■ Trapping exits 209 Monitors 210 8.3 Supervisors 211 Supervisor behaviour 211 ■ Defining a supervisor 213 Starting a supervisor 215 ■ Linking all processes 218 Restart frequency 220 8.4 Summary 221 9 Isolating error effects 222 9.1 Supervision trees 223 Separating loosely dependent parts 223 ■ Rich process discovery 225 ■ Supervising database workers 229 Removing the database process 232 ■ Starting the system 233 ■ Organizing the supervision tree 234 9.2 Starting workers dynamically 236 simple_one_for_one supervisors 237 ■ Restart strategies 240 9.3 “Let it crash” 242 Error kernel 243 ■ Handling expected errors 244 Preserving the state 245 9.4 Summary 246 10 Sharing state 247 10.1 Single-process bottleneck 247 10.2 ETS tables 250 Basic operations 250 ■ ETS powered page cache 252 Other ETS operations 257 ■ Other use cases for ETS 259 Beyond ETS 260 ■ Exercise: ETS-powered process registry 260 10.3 Summary 262 PART 3 PRODUCTION...................................................263 11 Working with components 265 11.1 OTP applications 266 Describing an application 266 ■ Creating applications with the mix tool 266 ■ The application behaviour 268 Starting the application 269 ■ Library applications 270 Creating a to-do application 270 ■ The application folder structure 272 11.2 Working with dependencies 274 Specifying dependencies 274 ■ Adapting the registry 275 11.3 Building a web server 277 Choosing dependencies 277 ■ Starting the server 278 Handling requests 279 ■ Reasoning about the system 283 11.4 Managing the application environment 286 11.5 Summary 289 12 Building a distributed system 290 12.1 Building a distibuted system 290 12.2 Distribution primitives 292 Starting a cluster 292 ■ Communicating between nodes 294 Process discovery 296 ■ Links and monitors 299 ■ Other distribution services 300 12.3 Building a fault-tolerant cluster 302 Cluster design 303 ■ The distributed to-do cache 303 Implementing a replicated database 308 ■ Testing the system 310 ■ Dealing with partitions 312 ■ Highly available systems 315 12.4 Network considerations 316 Node names 316 ■ Cookies 317 ■ Hidden nodes 317 Firewalls 318 12.5 Summary 319 13 Running the system 321 13.1 Running a system with the mix tool 322 Bypassing the shell 322 ■ Running as a background process 323 Running scripts 324 ■ Compiling for production 325 13.2 OTP releases 327 Building a release with exrm 328 ■ Using a release 329 Release contents 330 13.3 Analyzing system behavior 334 Debugging 334 ■ Logging 336 ■ Interacting with the system 336 ■ Tracing 339 13.4 Summary 341 index 343 preface In 2010, I was given the task of implementing a system to transmit frequent updates to a few thousand connected users in near real time. My company at the time was mostly using Ruby on Rails. But for such a highly concurrent challenge, I needed something more suitable. Following the suggestion of my CTO, I looked into Erlang, read some material, made a prototype, and performed a load test. I was impressed with the initial results and moved on to implement the whole thing in Erlang. A few months later, the system was shipped, and it’s been doing its job ever since. As time passed, I began to increasingly appreciate Erlang and the way it helped me manage such a complex system. Gradually, I came to prefer Erlang over the technolo- gies I had used previously. I began to evangelize the language, first in my company and then at local events, then, finally, at the end of 2012, I started the blog The Erlangelist (http://theerlangelist.com), where I aim to showcase the advantages of Erlang to programmers from OO backgrounds. Because Erlang is an unusual language, I began experimenting with Elixir, hoping it would help me explain the beauty of Erlang in a way that would resonate with OO programmers. Despite the fact that it was at an early stage of development (at the time, it was at version 0.8), I was immediately impressed with Elixir’s maturity and the way it integrated with Erlang. Soon, I started using Elixir to develop new features for my Erlang-based system. A few months later, I was contacted by Michael Stephens of Manning, who asked me if I was interested in writing a book about Elixir. At the time, two Elixir books were already in the making. After some consideration, I decided there was space for another book that would approach the topic from a different angle. In the proposal for this

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.