Slide 1

Slide 1 text

Async/Await functions in Ruby Sat, Dec. 14th, 2019 @平成Ruby会議01

Slide 2

Slide 2 text

@sat0yu - Software engineer at - Building microservices with gRPC and GraphQL - Three years of

Slide 3

Slide 3 text

What’s takeaway of this session 1. The basic use of Promise and Fiber 2. ★ The combination of Promise and Fiber performs like Async/Await syntax 3. ★★ The naive Async/Await syntax sadly does NOT work efficiently in ★: hard for @sat0yu to explain

Slide 4

Slide 4 text

GraphQL as an integrator *Note: just a part of the whole Quipper services

Slide 5

Slide 5 text

N+1 problems with GraphQL

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

ref. https://github.com/rmosolgo/graphql-ruby/blob/master/guides/schema/lazy_execution.md

Slide 8

Slide 8 text

How to use Graphql-batch - Build a loader so as inherit GraphQL::Batch::Loader - which need implementing perform(...)fetching data in bulk - Generate field values through the loader X.rb Y.rb ref. https://github.com/Shopify/graphql-batch

Slide 9

Slide 9 text

Nested loaders ref. https://github.com/Shopify/graphql-batch

Slide 10

Slide 10 text

“Promise” in Graphql-batch ref. https://github.com/Shopify/graphql-batch

Slide 11

Slide 11 text

- A representation of an eventual value - The value won’t be calculated until evaluating it (lazy-evaluation) - Chain-able with other promises through .then(...) What is “Promise”? fulfill reject pending fulfilled rejected ※ .load(...) returns a promise

Slide 12

Slide 12 text

Async/Await syntax with Promise and Genarator JavaScript

Slide 13

Slide 13 text

Async/Await in JavaScript - Declared in ES2017(ES8); Almost all browsers have compatibility - Convenient to write async processing with a synchronized-ish syntax JavaScript

Slide 14

Slide 14 text

Pause and Resume for promises - Declared in ES2017(ES8); Almost all browsers have compatibility - Convenient to write async processing with a synchronized-ish syntax sleeping... Pause Resume JavaScript

Slide 15

Slide 15 text

Promise + Generator ≈ Async/Await ≈ JavaScript

Slide 16

Slide 16 text

Generator pretends Pause and Resume JavaScript

Slide 17

Slide 17 text

Generator pretends Pause and Resume JavaScript How can we use Pause & Resume in Ruby?

Slide 18

Slide 18 text

Generator pretends Pause and Resume JavaScript Fiber (1.9~)

Slide 19

Slide 19 text

What is “Fiber”? ⏸

Slide 20

Slide 20 text

⏩ What is “Fiber”?

Slide 21

Slide 21 text

⏩ What is “Fiber”?

Slide 22

Slide 22 text

⏩ What is “Fiber”?

Slide 23

Slide 23 text

⏸ What is “Fiber”?

Slide 24

Slide 24 text

⏩ What is “Fiber”?

Slide 25

Slide 25 text

⏩ What is “Fiber”?

Slide 26

Slide 26 text

⏩ What is “Fiber”?

Slide 27

Slide 27 text

⏸ What is “Fiber”?

Slide 28

Slide 28 text

Async/Await syntax with Promise and Genarator Fiber

Slide 29

Slide 29 text

2. Rewrite nested loaders with Fiber 1. Nested loaders implementation 3. Rewrite nested “resume”s

Slide 30

Slide 30 text

1. Nested loaders implementation 2. Rewrite nested loaders with Fiber 3. Rewrite nested “resume”s

Slide 31

Slide 31 text

1. Nested loaders implementation

Slide 32

Slide 32 text

1. Nested loaders implementation

Slide 33

Slide 33 text

1. Nested loaders implementation

Slide 34

Slide 34 text

1. Nested loaders implementation

Slide 35

Slide 35 text

1. Nested loaders implementation 2. Rewrite nested loaders with Fiber 3. Rewrite nested “resume”s

Slide 36

Slide 36 text

2. Rewrite nested loaders with Fiber

Slide 37

Slide 37 text

⏸ 2. Rewrite nested loaders with Fiber

Slide 38

Slide 38 text

⏩ 2. Rewrite nested loaders with Fiber

Slide 39

Slide 39 text

⏩ p1 2. Rewrite nested loaders with Fiber

Slide 40

Slide 40 text

⏸ p1 next p1 2. Rewrite nested loaders with Fiber

Slide 41

Slide 41 text

⏸ p1 next p1 2. Rewrite nested loaders with Fiber

Slide 42

Slide 42 text

⏩ p1 next p1 2. Rewrite nested loaders with Fiber

Slide 43

Slide 43 text

⏩ p1 next p1 p2 2. Rewrite nested loaders with Fiber

Slide 44

Slide 44 text

⏸ p1 next p1 p2 next p2 2. Rewrite nested loaders with Fiber

Slide 45

Slide 45 text

⏸ p1 next p1 p2 next p2 2. Rewrite nested loaders with Fiber

Slide 46

Slide 46 text

⏩ p1 next p1 p2 next p2 2. Rewrite nested loaders with Fiber

Slide 47

Slide 47 text

p1 next p1 p2 next p2 X 2. Rewrite nested loaders with Fiber

Slide 48

Slide 48 text

p1 next p1 p2 next p2 X 2. Rewrite nested loaders with Fiber

Slide 49

Slide 49 text

Looks working 2. Rewrite nested loaders with Fiber

Slide 50

Slide 50 text

1. Nested loaders implementation 2. Rewrite nested loaders with Fiber 3. Rewrite nested “resume”s

Slide 51

Slide 51 text

3. Rewrite nested “resume”s

Slide 52

Slide 52 text

3. Rewrite nested “resume”s

Slide 53

Slide 53 text

3. Rewrite nested “resume”s

Slide 54

Slide 54 text

3. Rewrite nested “resume”s

Slide 55

Slide 55 text

3. Rewrite nested “resume”s

Slide 56

Slide 56 text

3. Rewrite nested “resume”s

Slide 57

Slide 57 text

3. Rewrite nested “resume”s

Slide 58

Slide 58 text

Looks working 3. Rewrite nested “resume”s

Slide 59

Slide 59 text

Let’s apply the naive Async/Await syntax to xx

Slide 60

Slide 60 text

Async/Await in

Slide 61

Slide 61 text

When fetching A and B fields

Slide 62

Slide 62 text

When fetching C and D fields N+1…

Slide 63

Slide 63 text

What’s wrong with the naive Async/Await? The naive Async/Await impl. returns a value, not a promise as a result Graphql-batch allows fields to return a promise in place of a value

Slide 64

Slide 64 text

Is it possible to fix it? - It makes me feel it’s do-able… but it does not work even if modifying it as returning a promise - The main difficulty comes from the difference in the promise life-cycle between Graphql-batch and the handcrafted Async/Await implementation

Slide 65

Slide 65 text

Summary - Explained the basic usage of Promise and Fiber - Confirmed the combination of Promise and Fiber works as Async/Await syntax - Observed that the naive impl. is not enough to work along with Graphql-batch - Code available: https://github.com/sat0yu/heisei-ruby-kaigi01 Thank you for your time and attention

Slide 66

Slide 66 text

appendix

Slide 67

Slide 67 text

Promise + Generator = Async/Await https://hackernoon.com/async-await-generators-promises-51f1a6ceede2 JavaScript

Slide 68

Slide 68 text

What is “Fiber”? https://www.slideshare.net/KoichiSasada/rubys-concurrency-management-now-and-future

Slide 69

Slide 69 text

Graphql-batch gives an example with concurrent-ruby https://github.com/Shopify/graphql-batch/blob/master/examples/http_loader.rb