Menu

1 / 2

Context Cancellation in Go: Propagating Deadlines Across Service Calls

DEV Community: microservices·Andrii B.·3 days ago
#vGcrbQJ1
Reading 0:00
15s threshold

Here's a client method that looks like it was written by someone careful:

func ( c * InventoryClient ) Reserve ( ctx context . Context , sku string , qty int ) error { ctx , cancel := context . WithTimeout ( context . Background (), 5 * time . Second ) defer cancel () return c . post ( ctx , "/reserve" , reserveReq { SKU : sku , Qty : qty }) }

Enter fullscreen mode

Exit fullscreen mode

It takes a ctx . It sets a timeout. It calls cancel . It compiles, and go vet has nothing to say about it. And it throws away every deadline the caller ever had. That first line overwrites the incoming ctx with a brand-new one rooted in context.Background() , so the 800ms budget your edge gateway gave this request is gone. From here on, this call has 5 seconds, no matter what.

That's the whole topic in one line. Go's context makes deadline propagation almost free, and "almost" is where the outages come from.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.