GithubHelp home page GithubHelp logo

huboh / go-backpressure Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 5 KB

simple backpressure implementation in Golang with buffered channels

License: MIT License

Dockerfile 4.41% Go 95.59%
backpressure buffered-channel channels concurrency go requests

go-backpressure's Introduction

go-backpressure

This repository demonstrates backpressure implementation with Go buffered channels.

As you may know, more concurrrency does not make programs faster, It is counterintuitive but systems perform better overall when their components limit the amount of work they are willing to perform. Go programs can spawn hundreds, thousands, even tens of thousands of simultaneous goroutines. but the general rule is to use as little concurrency as your program need otherwise your code becomes harder to understand

The RequestGauge struct uses a buffered channel to limit the amount of simultaneous requests our application is handling to prevent it from falling behind and becoming overwhelmed.

Each empty spot in our buffered channel represent a running goroutine. Every time a goroutine calls RequestGauge.run, either of these scenarios occurs:

  • successfully read from the channel thereby removing an element from the channel. It then invokes its callback and writes a token back to the channel when the callback exits, allowing other goroutine to run when they call the run function.
  • the default case in our select statement runs indicating the buffered channel is empty (there are still running goroutines).
type RequestGauge struct {
    channel chan struct{}
}

func (g *RequestGauge) run(callback func()) error {
    select {
    case <-g.channel:
      callback()
      g.channel <- struct{}{}
      return nil

    default:
      return errors.New("request gauge capacity exceeded")
    }
}

The code in main.go uses this implementation with the built-in HTTP server

func getHandler(requestGauge *RequestGauge) http.HandlerFunc {
  return func(w http.ResponseWriter, r *http.Request) {
    err := requestGauge.run(func() {
      sendJson(w, Payload{Error: "", ServerTime: getServerTimeAfter(requestDuration)})
    })

    if err != nil {
      w.WriteHeader(http.StatusTooManyRequests)
      sendJson(w, Payload{Error: "too many request"})
    }
  }
}

Docker

run this example locally with docker

build docker image

docker build -t go-backpressure .

run docker image

docker run -it -p 4000:4000 go-backpressure

Contributing

if you have any suggestion or find an issue with this implementation, please help out

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.