micro/go-micro · error

not connected

Error message

not connected

What it means

rbroker.Subscribe requires an established connection; if r.conn is nil it returns "not connected" before creating a subscriber. ackSuccess is initialized for the subscribe/ack flow, but without a connection there is nothing to subscribe on.

Source

Thrown at broker/rabbitmq/rabbitmq.go:244

		m.Headers[k] = v
	}

	if r.getWithoutExchange() {
		m.Headers["Micro-Topic"] = topic
	}

	if r.conn == nil {
		return errors.New("connection is nil")
	}

	return r.conn.Publish(r.conn.exchange.Name, topic, m)
}

func (r *rbroker) Subscribe(topic string, handler broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
	var ackSuccess bool

	if r.conn == nil {
		return nil, errors.New("not connected")
	}

	opt := broker.SubscribeOptions{
		AutoAck: true,
	}

	for _, o := range opts {
		o(&opt)
	}

	// Make sure context is setup
	if opt.Context == nil {
		opt.Context = context.Background()
	}

	ctx := opt.Context
	if subscribeContext, ok := ctx.Value(subscribeContextKey{}).(context.Context); ok && subscribeContext != nil {
		ctx = subscribeContext

View on GitHub (pinned to 24529f1404)

Solutions

  1. Connect first: call broker.Connect() and handle its error before Subscribe.
  2. On reconnect, recreate subscriptions since they are tied to the old connection.
  3. Add a readiness gate (e.g. wait for Connect success) before starting consumers.

Example fix

// before
sub, err := b.Subscribe("events", handler) // "not connected"
// after
if err := b.Connect(); err != nil { return err }
sub, err := b.Subscribe("events", handler)
Defensive patterns

Strategy: validation

Validate before calling

if err := b.Connect(); err != nil { return err }
// only now is Subscribe safe

Try / catch

sub, err := b.Subscribe(topic, handler)
if err != nil && strings.Contains(err.Error(), "not connected") {
    if cerr := b.Connect(); cerr != nil { return cerr }
    sub, err = b.Subscribe(topic, handler)
}

Prevention

When it happens

Trigger: Calling broker.Subscribe() before broker.Connect(), after Disconnect(), or on a broker instance whose connection was never established.

Common situations: Consumers started before the broker connection is ready (startup race); resubscribing after a failure without reconnecting; test setups that instantiate the broker without connecting.

Related errors


AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01). Data as JSON: /api/errors/f436290d4be6ac0c. Report an issue: GitHub.