From 632458f01c284ec1b1b0b0c7fb1c6c4024fe08b3 Mon Sep 17 00:00:00 2001 From: Vladimir Dementyev Date: Wed, 10 Jun 2020 22:20:44 +0300 Subject: [PATCH] feat: add --disable_disconnect parameter --- CHANGELOG.md | 4 ++++ Makefile | 1 - cli/cli.go | 2 ++ cmd/anycable-go/main.go | 10 ++++++++-- config/config.go | 33 +++++++++++++++++---------------- node/disconnector.go | 40 ++++++++++++++++++++++++++++++++++++++++ node/node.go | 8 -------- 7 files changed, 71 insertions(+), 27 deletions(-) create mode 100644 node/disconnector.go diff --git a/CHANGELOG.md b/CHANGELOG.md index e029d47a..7d8a8b9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## 1.0.0-dev +- Add `--disable_disconnect` option. ([@palkan][]) + +Allows you to avoid calling `Disconnect` RPC method completely if you don't need it. + - Add channel state support. ([@palkan][]) - Add stopped streams support. ([@palkan][]) diff --git a/Makefile b/Makefile index c34e4470..116f5e61 100644 --- a/Makefile +++ b/Makefile @@ -89,7 +89,6 @@ test-conformance-ssl: tmp/anycable-go-test BUNDLE_GEMFILE=.circleci/Gemfile bundle exec anyt -c "tmp/anycable-go-test --headers=cookie,x-api-token --ssl_key=etc/ssl/server.key --ssl_cert=etc/ssl/server.crt --port=8443" --target-url="wss://localhost:8443/cable" test-conformance-http: tmp/anycable-go-test - go build -tags mrb -o tmp/anycable-go-test cmd/anycable-go/main.go BUNDLE_GEMFILE=.circleci/Gemfile ANYCABLE_BROADCAST_ADAPTER=http ANYCABLE_HTTP_BROADCAST_SECRET=any_secret bundle exec anyt -c "tmp/anycable-go-test --headers=cookie,x-api-token" --target-url="ws://localhost:8080/cable" test-conformance-all: test-conformance test-conformance-ssl test-conformance-http diff --git a/cli/cli.go b/cli/cli.go index c59ffdab..5ebc1855 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -75,6 +75,7 @@ func init() { fs.IntVar(&defaults.DisconnectQueue.Rate, "disconnect_rate", 100, "") fs.IntVar(&defaults.DisconnectQueue.ShutdownTimeout, "disconnect_timeout", 5, "") + fs.BoolVar(&defaults.DisconnectorDisabled, "disable_disconnect", false, "") fs.StringVar(&defaults.LogLevel, "log_level", "info", "") fs.StringVar(&defaults.LogFormat, "log_format", "text", "") @@ -149,6 +150,7 @@ OPTIONS --disconnect_rate Max number of Disconnect calls per second, default: 100, env: ANYCABLE_DISCONNECT_RATE --disconnect_timeout Graceful shutdown timeouts (in seconds), default: 5, env: ANYCABLE_DISCONNECT_TIMEOUT + --disable_disconnect Disable calling Disconnect callback, default: false, env: ANYCABLE_DISABLE_DISCONNECT --log_level Set logging level (debug/info/warn/error/fatal), default: info, env: ANYCABLE_LOG_LEVEL --log_format Set logging format (text, json), default: text, env: ANYCABLE_LOG_FORMAT diff --git a/cmd/anycable-go/main.go b/cmd/anycable-go/main.go index 41e9e9c0..f367e9bd 100644 --- a/cmd/anycable-go/main.go +++ b/cmd/anycable-go/main.go @@ -73,9 +73,15 @@ func main() { appNode := node.NewNode(controller, metrics) appNode.Start() - disconnector := node.NewDisconnectQueue(appNode, &config.DisconnectQueue) - go disconnector.Run() // nolint:errcheck + var disconnector node.Disconnector + + if config.DisconnectorDisabled { + disconnector = node.NewNoopDisconnector() + } else { + disconnector = node.NewDisconnectQueue(appNode, &config.DisconnectQueue) + } + go disconnector.Run() // nolint:errcheck appNode.SetDisconnector(disconnector) subscriber, err := pubsub.NewSubscriber(appNode, config.BroadcastAdapter, &config.Redis, &config.HTTPPubSub) diff --git a/config/config.go b/config/config.go index 92cc36e8..19d06930 100644 --- a/config/config.go +++ b/config/config.go @@ -10,22 +10,23 @@ import ( // Config contains main application configuration type Config struct { - RPC rpc.Config - Redis pubsub.RedisConfig - HTTPPubSub pubsub.HTTPConfig - Host string - Port int - BroadcastAdapter string - Path string - HealthPath string - Headers []string - SSL server.SSLConfig - WS node.WSConfig - MaxMessageSize int64 - DisconnectQueue node.DisconnectQueueConfig - LogLevel string - LogFormat string - Metrics metrics.Config + RPC rpc.Config + Redis pubsub.RedisConfig + HTTPPubSub pubsub.HTTPConfig + Host string + Port int + BroadcastAdapter string + Path string + HealthPath string + Headers []string + SSL server.SSLConfig + WS node.WSConfig + MaxMessageSize int64 + DisconnectorDisabled bool + DisconnectQueue node.DisconnectQueueConfig + LogLevel string + LogFormat string + Metrics metrics.Config } // New returns a new empty config diff --git a/node/disconnector.go b/node/disconnector.go new file mode 100644 index 00000000..0d2032c4 --- /dev/null +++ b/node/disconnector.go @@ -0,0 +1,40 @@ +package node + +import "github.com/apex/log" + +// Disconnector is an interface for disconnect queue implementation +type Disconnector interface { + Run() error + Shutdown() error + Enqueue(*Session) error + Size() int +} + +// NoopDisconnectQueue is non-operational disconnect queue implementation +type NoopDisconnectQueue struct{} + +// Run does nothing +func (d *NoopDisconnectQueue) Run() error { + log.WithField("context", "disconnector").Info("Disconnect events are turned off") + return nil +} + +// Shutdown does nothing +func (d *NoopDisconnectQueue) Shutdown() error { + return nil +} + +// Size returns 0 +func (d *NoopDisconnectQueue) Size() int { + return 0 +} + +// Enqueue does nothing +func (d *NoopDisconnectQueue) Enqueue(s *Session) error { + return nil +} + +// NewNoopDisconnector returns new NoopDisconnectQueue +func NewNoopDisconnector() *NoopDisconnectQueue { + return &NoopDisconnectQueue{} +} diff --git a/node/node.go b/node/node.go index 7ac76190..e7d75be9 100644 --- a/node/node.go +++ b/node/node.go @@ -47,14 +47,6 @@ func (d *disconnectMessage) toJSON() []byte { return jsonStr } -// Disconnector is an interface for disconnect queue implementation -type Disconnector interface { - Run() error - Shutdown() error - Enqueue(*Session) error - Size() int -} - // AppNode describes a basic node interface type AppNode interface { HandlePubSub(msg []byte)