Skip to content

Rate limit the number of outgoing gRPC messages #1759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 17, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions arduino/utils/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,26 @@

package utils

import "io"
import (
"io"
"time"
)

// FeedStreamTo creates a pipe to pass data to the writer function.
// FeedStreamTo returns the io.Writer side of the pipe, on which the user can write data
func FeedStreamTo(writer func(data []byte)) io.Writer {
r, w := io.Pipe()
go func() {
data := make([]byte, 1024)
data := make([]byte, 10240)
for {
if n, err := r.Read(data); err == nil {
writer(data[:n])

// Rate limit the number of outgoing gRPC messages
// (less messages with biggger data blocks)
if n < len(data) {
time.Sleep(50 * time.Millisecond)
}
} else {
r.Close()
return
Expand Down