0%

Book Description

Discover and harness Go's powerful concurrency features to develop and build fast, scalable network systems

In Detail

This book will take you through the history of concurrency, how Go utilizes it, how Go differs from other languages, and the features and structures of Go's concurrency core. Each step of the way, the book will present real, usable examples with detailed descriptions of the methodologies used. By the end, you will feel comfortable designing a safe, data-consistent, high-performance concurrent application in Go.

The focus of this book is on showing you how Go can be used to program high-performance, robust concurrent programs with Go's unique form of multithreading, which employs goroutines that communicate and synchronize across channels. Designed for any curious developer or systems administrator with an interest in fast, non-blocking, and resource-thrifty systems applications, this book is an invaluable resource to help you understand Go's powerful concurrency focus.

What You Will Learn

  • Create goroutines as the building blocks of concurrency in Go
  • Extend the goroutines to include channels to open up communication
  • Design concurrent patterns to apply toward our examples and future applications
  • Explore methods to ensure data consistency remains paramount in concurrent and multi-core Go applications
  • Tune performance to squeeze the most out of existing hardware to keep your applications below resource thresholds
  • Utilize advanced concurrency patterns and best practices to stay low-level without compromising the simplicity of Go itself
  • Build several servers in Go that strongly utilize concurrency features

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Mastering Concurrency in Go
    1. Table of Contents
    2. Mastering Concurrency in Go
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. An Introduction to Concurrency in Go
      1. Introducing goroutines
        1. A patient goroutine
      2. Implementing the defer control mechanism
        1. Using Go's scheduler
        2. Using system variables
      3. Understanding goroutines versus coroutines
      4. Implementing channels
        1. Channel-based sorting at the letter capitalization factory
        2. Cleaning up our goroutines
          1. Buffered or unbuffered channels
        3. Using the select statement
      5. Closures and goroutines
      6. Building a web spider using goroutines and channels
      7. Summary
    9. 2. Understanding the Concurrency Model
      1. Understanding the working of goroutines
      2. Synchronous versus asynchronous goroutines
        1. Designing the web server plan
      3. Visualizing concurrency
      4. RSS in action
        1. An RSS reader with self diagnostics
        2. Imposing a timeout
      5. A little bit about CSP
        1. The dining philosophers problem
      6. Go and the actor model
      7. Object orientation
        1. Demonstrating simple polymorphism in Go
      8. Using concurrency
      9. Managing threads
      10. Using sync and mutexes to lock data
      11. Summary
    10. 3. Developing a Concurrent Strategy
      1. Applying efficiency in complex concurrency
      2. Identifying race conditions with race detection
        1. Using mutual exclusions
        2. Exploring timeouts
          1. Importance of consistency
      3. Synchronizing our concurrent operations
      4. The project – multiuser appointment calendar
        1. Visualizing a concurrent pattern
        2. Developing our server requirements
          1. Web server
            1. The Gorilla toolkit
          2. Using templates
          3. Time
        3. Endpoints
        4. Custom structs
      5. A multiuser Appointments Calendar
      6. A note on style
      7. A note on immutability
      8. Summary
    11. 4. Data Integrity in an Application
      1. Getting deeper with mutexes and sync
      2. The cost of goroutines
      3. Working with files
      4. Getting low – implementing C
        1. Touching memory in cgo
        2. The structure of cgo
        3. The other way around
          1. Getting even lower – assembly in Go
      5. Distributed Go
      6. Some common consistency models
        1. Distributed shared memory
        2. First-in-first-out – PRAM
        3. Looking at the master-slave model
        4. The producer-consumer problem
        5. Looking at the leader-follower model
        6. Atomic consistency / mutual exclusion
        7. Release consistency
      7. Using memcached
        1. Circuit
      8. Summary
    12. 5. Locks, Blocks, and Better Channels
      1. Understanding blocking methods in Go
        1. Blocking method 1 – a listening, waiting channel
          1. Sending more data types via channels
            1. Creating a function channel
            2. Using an interface channel
            3. Using structs, interfaces, and more complex channels
          2. The net package – a chat server with interfaced channels
            1. Handling direct messages
        2. Examining our client
        3. Blocking method 2 – the select statement in a loop
      2. Cleaning up goroutines
        1. Blocking method 3 – network connections and reads
      3. Creating channels of channels
      4. Pprof – yet another awesome tool
      5. Handling deadlocks and errors
      6. Summary
    13. 6. C10K – A Non-blocking Web Server in Go
      1. Attacking the C10K problem
        1. Failing of servers at 10,000 concurrent connections
        2. Using concurrency to attack C10K
        3. Taking another approach
      2. Building our C10K web server
        1. Benchmarking against a blocking web server
        2. Handling requests
        3. Routing requests
      3. Serving pages
        1. Parsing our template
        2. External dependencies
          1. Connecting to MySQL
      4. Multithreading and leveraging multiple cores
      5. Exploring our web server
        1. Timing out and moving on
      6. Summary
    14. 7. Performance and Scalability
      1. High performance in Go
        1. Getting deeper into pprof
        2. Parallelism's and concurrency's impact on I/O pprof
      2. Using the App Engine
      3. Distributed Go
        1. Types of topologies
          1. Type 1 – star
          2. Type 2 – mesh
          3. The Publish and Subscribe model
          4. Serialized data
          5. Remote code execution
          6. Other topologies
          7. Message Passing Interface
      4. Some helpful libraries
        1. Nitro profiler
        2. Heka
        3. GoFlow
      5. Memory preservation
        1. Garbage collection in Go
      6. Summary
    15. 8. Concurrent Application Architecture
      1. Designing our concurrent application
      2. Identifying our requirements
      3. Using NoSQL as a data store in Go
        1. MongoDB
        2. Redis
        3. Tiedot
        4. CouchDB
        5. Cassandra
        6. Couchbase
        7. Setting up our data store
      4. Monitoring filesystem changes
      5. Managing logfiles
      6. Handling configuration files
      7. Detecting file changes
        1. Sending changes to clients
        2. Checking records against Couchbase
      8. Backing up our files
      9. Designing our web interface
      10. Reverting a file's history – command line
        1. Using Go in daemons and as a service
      11. Checking the health of our server
      12. Summary
    16. 9. Logging and Testing Concurrency in Go
      1. Handling errors and logging
        1. Breaking out goroutine logs
        2. Using the LiteIDE for richer and easier debugging
        3. Sending errors to screen
        4. Logging errors to file
        5. Logging errors to memory
      2. Using the log4go package for robust logging
        1. Panicking
        2. Recovering
          1. Logging our panics
        3. Catching stack traces with concurrent code
      3. Using the runtime package for granular stack traces
      4. Summary
    17. 10. Advanced Concurrency and Best Practices
      1. Going beyond the basics with channels
      2. Building workers
      3. Implementing nil channel blocks
        1. Using nil channels
      4. Implementing more granular control over goroutines with tomb
      5. Timing out with channels
      6. Building a load balancer with concurrent patterns
      7. Choosing unidirectional and bidirectional channels
        1. Using receive-only or send-only channels
      8. Using an indeterminate channel type
      9. Using Go with unit testing
        1. GoCheck
        2. Ginkgo and Gomega
      10. Using Google App Engine
      11. Utilizing best practices
        1. Structuring your code
        2. Documenting your code
        3. Making your code available via go get
        4. Keeping concurrency out of your packages
      12. Summary
    18. Index
18.227.102.50