Introduction xviiGetting Started xxiChapter 1 Writing Command-Line Applications 1Your First Application 1Writing Unit Tests 8Using the Flag Package 14Testing the Parsing Logic 20Improving the User Interface 22Removing Duplicate Error Messages 23Customizing Usage Message 24Accept Name via a Positional Argument 25Updating the Unit Tests 28Summary 32Chapter 2 Advanced Command-Line Applications 33Implementing Sub-commands 33An Architecture for Sub-command-Driven Applications 37Testing the Main Package 43Testing the Cmd Package 45Making Your Applications Robust 47User Input with Deadlines 48Handling User Signals 52Summary 56Chapter 3 Writing HTTP Clients 57Downloading Data 57Testing the Data Downloader 59Deserializing Received Data 61Sending Data 66Working with Binary Data 72Summary 80Chapter 4 Advanced HTTP Clients 81Using a Custom HTTP Client 81Downloading from an Overloaded Server 81Testing the Time-Out Behavior 85Configuring the Redirect Behavior 88Customizing Your Requests 91Implementing Client Middleware 92Understanding the RoundTripper Interface 93A Logging Middleware 94Add a Header to All Requests 96Connection Pooling 99Configuring the Connection Pool 103Summary 104Chapter 5 Building HTTP Servers 105Your First HTTP Server 105Setting Up Request Handlers 108Handler Functions 109Testing Your Server 112The Request Struct 114Method 115URL 115Proto, ProtoMajor, and ProtoMinor 116Header 116Host 116Body 116Form, PostForm 116MultipartForm 117Attaching Metadata to a Request 118Processing Streaming Requests 121Streaming Data as Responses 126Summary 132Chapter 6 Advanced HTTP Server Applications 133The Handler Type 133Sharing Data across Handler Functions 134Writing Server Middleware 139Custom HTTP Handler Technique 139The HandlerFunc Technique 140Chaining Middleware 142Writing Tests for Complex Server Applications 147Code Organization 147Testing the Handler Functions 153Testing the Middleware 155Testing the Server Startup 157Summary 159Chapter 7 Production-Ready HTTP Servers 161Aborting Request Handling 161Strategies to Abort Request Processing 165Handling Client Disconnects 169Server-Wide Time-Outs 173Implement a Time-Out for All Handler Functions 173Implementing Server Time-Out 174Implementing Graceful Shutdown 179Securing Communication with TLS 184Configuring TLS and HTTP/2 184Testing TLS Servers 188Summary 192Chapter 8 Building RPC Applications with gRPC 193gRPC and Protocol Buffers 193Writing Your First Service 197Writing the Server 198Writing a Client 203Testing the Server 207Testing the Client 211A Detour into Protobuf Messages 214Marshalling and Unmarshalling 214Forward and Backward Compatibility 219Multiple Services 220Error Handling 226Summary 228Chapter 9 Advanced gRPC Applications 229Streaming Communication 229Server-SideStreaming 230Client-SideStreaming 237Bidirectional Streaming 239Receiving and Sending Arbitrary Bytes 247Implementing Middleware Using Interceptors 256Client-Side Interceptors 257Server-Side Interceptors 263Wrapping Streams 269Chaining Interceptors 271Summary 272Chapter 10 Production-Ready gRPC Applications 275Securing Communication with TLS 275Robustness in Servers 278Implementing Health Checks 278Handling Runtime Errors 286Aborting Request Processing 289Robustness in Clients 297Improving Connection Setup 298Handling Transient Failures 300Setting Time-Outs for Method Calls 305Connection Management 306Summary 309Chapter 11 Working with Data Stores 311Working with Object Stores 312Integration with Package Server 313Testing Package Uploads 323Accessing Underlying Driver Types 325Working with Relational Databases 327Integration with Package Server 328Testing Data Storage 339Data Type Conversions 343Using Database Transactions 346Summary 348Appendix A Making Your Applications Observable 349Logs, Metrics, and Traces 349Emitting Telemetry Data 352Command-Line Applications 352HTTP Applications 360gRPC Applications 364Summary 366Appendix B Deploying Applications 367Managing Configuration 367Distributing Your Application 370Deploying Server Applications 372Summary 373Index 375
Amit Saha is a software engineer at Atlassian, located in Sydney, Australia. He has written Doing Math with Python: Use Programming to Explore Algebra, Statistics, Calculus, and More! (No Starch Press, 2015) and Write Your First Program (PHI Learning, 2013). His other writings have been published in technical magazines, conference proceedings, and research journals. He can be found online at https://echorand.me