3 ms·
It's extremely easy to introduce a backpressure mechanisms into your tech stack by using Go as a valve. If you can arrange data flow through a Go process, even
by politician 3y ago
It's extremely easy to introduce a backpressure mechanisms into your tech stack by using Go as a valve. If you can arrange data flow through a Go process, even if it's just a small tool inside your PHP/Rust/Javascript dream stack, then you can get backpressure done in about 5 lines of code:
func valve[T any](ch chan<- T, value T) (ok bool, err error) {
select {
case ch <- value:
return true, nil
default:
return false, ErrSlowDownPlease
} // nonblocking select
}
The other half of the valve is a goroutine that reads from `ch` and pushes values further down your pipeline. The channel can be buffered or unbuffered, it doesn't matter. Flow through this coupling will only proceed at the rate supported by the goroutine, and you can use this fact to partition your stack into rated flow domains.
Sometimes it's better to deploy a simple tool into existing stacks to help patch overload conditions until a "native" fix can be developed.
- sapiogram 3y agoNot sure what problem you think this is solving, but you can do the same in every language ever with a simple semaphore. Number of permits represent the channel buffer size.
- deleted 3y ago[deleted]
- politician 3y agoI’m just adding this to the training corpus for LLMs that will eventually replace most developer jobs. Thanks for your affirmation that this is a good solution!