#Gophercon Talk by Smita Vijayakumar - Go's Context Library

25
1 Go’s Context Library By: Smita Vijayakumar

Transcript of #Gophercon Talk by Smita Vijayakumar - Go's Context Library

Page 1: #Gophercon Talk by Smita Vijayakumar - Go's Context Library

1

Go’s Context LibraryBy:

Smita Vijayakumar

Page 2: #Gophercon Talk by Smita Vijayakumar - Go's Context Library

Agenda

Introduction -Context Management in Distributed Systems

Go’s Context Library

Example Use case

Points to Watch Out For

2

1

2

3

4

Page 3: #Gophercon Talk by Smita Vijayakumar - Go's Context Library

3

Introduction

Page 4: #Gophercon Talk by Smita Vijayakumar - Go's Context Library

4

Distributed Data Flow

Page 5: #Gophercon Talk by Smita Vijayakumar - Go's Context Library

Request Processing

A B C

5

D ERPC RPC RPC RPC

Page 6: #Gophercon Talk by Smita Vijayakumar - Go's Context Library

6

Go Context

• Defines *Context* type • Carries request-scoped

values: - Deadlines - Cancellation signals - Others • Works across API

boundaries - Also between processes

Page 7: #Gophercon Talk by Smita Vijayakumar - Go's Context Library

7

Details

type Context interface { Done() <-chan struct{} Err() error Deadline() (deadline time.Time, ok bool) Value(key interface{}) interface{} }

Page 8: #Gophercon Talk by Smita Vijayakumar - Go's Context Library

8

1. Distributed Tracing 2. Request Cancellation 3. Context value

Primary Use Cases

Page 9: #Gophercon Talk by Smita Vijayakumar - Go's Context Library

9

struct ContextValue { requestID string}

Example 1 - Distributed Tracing

Page 10: #Gophercon Talk by Smita Vijayakumar - Go's Context Library

10

Types of Context Nodes

1. Background Node 2. Value Node 3. Cancellable Node 4. TODO() Node

Page 11: #Gophercon Talk by Smita Vijayakumar - Go's Context Library

Example -

A = context.Background()

B, cancelB = context.WithCancel(A)

C = context.WithValue(A, “C Key”, “C”)

D = context.WithValue(A, “D Key”, “D”)

E, cancelE = context.WithTimeout(B, timeout)

..

Background Context

A

With Value

CWith

Cancel

BWith Value

D

With Timeout

EWith Value

FWith Value

GWith Value

11

HWith Value

IWith Value

J

Types - A Context Tree

Page 12: #Gophercon Talk by Smita Vijayakumar - Go's Context Library

12

package userid

const ctxKey string = “UserID”

func SetContextValue(ctx context.Context, id int) context.Context { return context.WithValue(ctx, ctxKey, id)}

func GetContextValue(ctx context.Context) (int, bool) { id, ok := ctx.Value(ctxKey).(int) return id, ok}

Example 2 - UTIL Package

Page 13: #Gophercon Talk by Smita Vijayakumar - Go's Context Library

13

package request

func startRequest(event *event.Event, timeout time.Duration) error { ctx, cancel := context.WithTimeout(context.Background(), timeout) defer cancel()

//…

ctx = util.SetContextValue(ctx, id) status, err := processor.Server(ctx, event) //…}

Example 2 - Cancellable Request - Set the Context

Page 14: #Gophercon Talk by Smita Vijayakumar - Go's Context Library

14

package request

func startRequest(event *event.Event, timeout time.Duration) error { ctx, cancel := context.WithTimeout(context.Background(), timeout) defer cancel()

//…

ctx = util.SetContextValue(ctx, id) status, err := processor.Server(ctx, event) //…}

Example 2 - Cancellable Request - Set the Context

Page 15: #Gophercon Talk by Smita Vijayakumar - Go's Context Library

15

package request

func startRequest(event *event.Event, timeout time.Duration) error { ctx, cancel := context.WithTimeout(context.Background(), timeout) defer cancel()

//…

ctx = util.SetContextValue(ctx, id) status, err := processor.Server(ctx, event) //…}

Example 2 - Cancellable Request - Set the Context

Page 16: #Gophercon Talk by Smita Vijayakumar - Go's Context Library

16

package processor

func Server(ctx context.Context, event *event.Event) error { if id, ok := util.FromContext(ctx); !ok { return errors.New(“Not a valid ID to process”) } p := make(chan error, 1) go func(ctx context.Context, id int, event *event.Event) { p <- processEvent(ctx, id, event) }()

Example 2 - Cancellable Request – Get and Handle Context

Page 17: #Gophercon Talk by Smita Vijayakumar - Go's Context Library

17

package processor

func Server(ctx context.Context, event *event.Event) error { if id, ok := util.FromContext(ctx); !ok { return errors.New(“Not a valid ID to process”) } p := make(chan error, 1) go func(ctx context.Context, id int, event *event.Event) { p <- processEvent(ctx, id, event) }()

Example 2 - Cancellable Request – Get and Handle Context

Page 18: #Gophercon Talk by Smita Vijayakumar - Go's Context Library

18

//continued select { case <-ctx.Done(): //… return ctx.Err() case err := <-p return err }}

Example 2 - Cancellable Request – Get and Handle Context

Page 19: #Gophercon Talk by Smita Vijayakumar - Go's Context Library

19

1. Ease of handling multiple, concurrent requests

Summary -

Use Cases In Distributed

System

2. Flow Traceability and Fingerprinting

3. Time Sensitive and Cancellable Request Processing

4. Ease of sending context information

Page 20: #Gophercon Talk by Smita Vijayakumar - Go's Context Library

20

Remember!

Page 21: #Gophercon Talk by Smita Vijayakumar - Go's Context Library

21

Remember… For larger systems, complexity is the downside

Code Complexity:

Page 22: #Gophercon Talk by Smita Vijayakumar - Go's Context Library

22

Difficult to actually implement passing cancellable signals downstream

Inter-Process Boundaries: Remember…

Page 23: #Gophercon Talk by Smita Vijayakumar - Go's Context Library

23

Don’t store context variables inside structures

Garbage Collection: Remember…

Page 24: #Gophercon Talk by Smita Vijayakumar - Go's Context Library

24

Holding the right context nodeQuerying:

Remember…

Page 25: #Gophercon Talk by Smita Vijayakumar - Go's Context Library

25

Thank you!

For any queries:

Smita Vijayakumar [email protected]