-
Notifications
You must be signed in to change notification settings - Fork 67
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
make WriteTo non-blocking #525
Draft
aldernero
wants to merge
1
commit into
main
Choose a base branch
from
aldernero/nonblocking-memberlist-tcp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,7 +52,10 @@ type TCPTransportConfig struct { | |
// Timeout for writing packet data. Zero = no timeout. | ||
PacketWriteTimeout time.Duration `yaml:"packet_write_timeout" category:"advanced"` | ||
|
||
// Transport logs lot of messages at debug level, so it deserves an extra flag for turning it on | ||
// Maximum number of concurrent writes to other nodes. | ||
MaxConcurrentWrites int `yaml:"max_concurrent_writes" category:"advanced"` | ||
|
||
// Transport logs lots of messages at debug level, so it deserves an extra flag for turning it on | ||
TransportDebug bool `yaml:"-" category:"advanced"` | ||
|
||
// Where to put custom metrics. nil = don't register. | ||
|
@@ -73,6 +76,7 @@ func (cfg *TCPTransportConfig) RegisterFlagsWithPrefix(f *flag.FlagSet, prefix s | |
f.IntVar(&cfg.BindPort, prefix+"memberlist.bind-port", 7946, "Port to listen on for gossip messages.") | ||
f.DurationVar(&cfg.PacketDialTimeout, prefix+"memberlist.packet-dial-timeout", 2*time.Second, "Timeout used when connecting to other nodes to send packet.") | ||
f.DurationVar(&cfg.PacketWriteTimeout, prefix+"memberlist.packet-write-timeout", 5*time.Second, "Timeout for writing 'packet' data.") | ||
f.IntVar(&cfg.MaxConcurrentWrites, prefix+"memberlist.max-concurrent-writes", 1, "Maximum number of concurrent writes to other nodes.") | ||
f.BoolVar(&cfg.TransportDebug, prefix+"memberlist.transport-debug", false, "Log debug transport messages. Note: global log.level must be at debug level as well.") | ||
|
||
f.BoolVar(&cfg.TLSEnabled, prefix+"memberlist.tls-enabled", false, "Enable TLS on the memberlist transport layer.") | ||
|
@@ -88,6 +92,7 @@ type TCPTransport struct { | |
packetCh chan *memberlist.Packet | ||
connCh chan net.Conn | ||
wg sync.WaitGroup | ||
writeCh chan struct{} | ||
tcpListeners []net.Listener | ||
tlsConfig *tls.Config | ||
|
||
|
@@ -124,6 +129,7 @@ func NewTCPTransport(config TCPTransportConfig, logger log.Logger, registerer pr | |
logger: log.With(logger, "component", "memberlist TCPTransport"), | ||
packetCh: make(chan *memberlist.Packet), | ||
connCh: make(chan net.Conn), | ||
writeCh: make(chan struct{}, config.MaxConcurrentWrites), | ||
} | ||
|
||
var err error | ||
|
@@ -426,7 +432,15 @@ func (t *TCPTransport) getAdvertisedAddr() string { | |
func (t *TCPTransport) WriteTo(b []byte, addr string) (time.Time, error) { | ||
t.sentPackets.Inc() | ||
t.sentPacketsBytes.Add(float64(len(b))) | ||
t.writeCh <- struct{}{} | ||
go func() { | ||
defer func() { <-t.writeCh }() | ||
t.writeToAsync(b, addr) | ||
}() | ||
Comment on lines
+435
to
+439
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of starting new goroutine for each write, shall we have a pool of writing goroutines, and pass them messages to write via channel? |
||
return time.Now(), nil | ||
} | ||
|
||
func (t *TCPTransport) writeToAsync(b []byte, addr string) { | ||
err := t.writeTo(b, addr) | ||
if err != nil { | ||
t.sentPacketsErrors.Inc() | ||
|
@@ -441,10 +455,7 @@ func (t *TCPTransport) WriteTo(b []byte, addr string) (time.Time, error) { | |
|
||
// WriteTo is used to send "UDP" packets. Since we use TCP, we can detect more errors, | ||
// but memberlist library doesn't seem to cope with that very well. That is why we return nil instead. | ||
return time.Now(), nil | ||
} | ||
|
||
return time.Now(), nil | ||
} | ||
|
||
func (t *TCPTransport) writeTo(b []byte, addr string) error { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
WriteTo
only called from single goroutine? If not, it just became more blocking.