{"record":{"id":"ba55c7754427219b","repo":"micro/go-micro","slug":"not-connected-ba55c7","errorCode":null,"errorMessage":"not connected","messagePattern":"not connected","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"broker/nats/nats.go","lineNumber":247,"sourceCode":"\n\t// Use connection pool if enabled\n\tif n.pool != nil {\n\t\tpoolConn, err := n.pool.Get()\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tdefer func() { _ = n.pool.Put(poolConn) }()\n\n\t\tconn := poolConn.Conn()\n\t\tif conn == nil {\n\t\t\treturn errors.New(\"invalid connection from pool\")\n\t\t}\n\t\treturn conn.Publish(topic, b)\n\t}\n\n\t// Use single connection (original behavior)\n\tif n.conn == nil {\n\t\treturn errors.New(\"not connected\")\n\t}\n\n\treturn n.conn.Publish(topic, b)\n}\n\nfunc (n *natsBroker) Subscribe(topic string, handler broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {\n\tn.RLock()\n\thasConnection := n.conn != nil || n.pool != nil\n\tn.RUnlock()\n\n\tif !hasConnection {\n\t\treturn nil, errors.New(\"not connected\")\n\t}\n\n\topt := broker.SubscribeOptions{\n\t\tAutoAck: true,\n\t\tContext: context.Background(),\n\t}","sourceCodeStart":229,"sourceCodeEnd":265,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/broker/nats/nats.go#L229-L265","documentation":"In non-pooled mode the NATS broker publishes through its single stored connection `n.conn`. If that field is nil, there is no active NATS connection, so Publish returns this error. It is the classic 'publish before connect' or 'connection lost' signal for the single-connection path.","triggerScenarios":"Calling Publish on a natsBroker when n.conn == nil — i.e., before Connect() succeeds, after Disconnect()/Close(), or if the client connection was lost and never re-established (broker/nats/nats.go:247).","commonSituations":"Publishing before Connect() in application startup; NATS server outage causing a permanently closed connection when reconnect options are exhausted; tests that forget to connect a shared broker fixture.","solutions":["Ensure broker.Connect(context) is called and succeeded before any Publish.","Configure NATS reconnect options (nats.MaxReconnects, retry on failed connect) so transient outages restore n.conn automatically.","On this error, check connectivity to the NATS server (host/port, credentials) and re-run Connect().","Verify no code path calls Disconnect() while publishers are still active."],"exampleFix":"// before\nb := nats.NewBroker(broker.Addrs(addr))\n_ = b.Publish(ctx, \"events\", msg) // panics into 'not connected'\n\n// after\nb := nats.NewBroker(broker.Addrs(addr))\nif err := b.Connect(ctx); err != nil {\n    log.Fatal(err)\n}\nif err := b.Publish(ctx, \"events\", msg); err != nil {\n    log.Fatal(err)\n}","handlingStrategy":"retry","validationCode":"if err := br.Connect(ctx); err != nil {\n    return fmt.Errorf(\"broker connect failed: %w\", err)\n}\n// safe to Publish now","typeGuard":"func isNotConnected(err error) bool {\n    return err != nil && strings.Contains(err.Error(), \"not connected\")\n}","tryCatchPattern":"if err := br.Publish(ctx, topic, msg); err != nil {\n    if strings.Contains(err.Error(), \"not connected\") {\n        _ = br.Connect(ctx) // or reconnect with backoff\n        err = br.Publish(ctx, topic, msg)\n    }\n    return err\n}","preventionTips":["Always pair nats.NewBroker with a checked Connect() call at startup.","Enable NATS client reconnect options (nats.MaxReconnects(-1), RetryOnFailedConnect) to survive server restarts.","Health-check the broker before publishing in long-running loops.","Gate Disconnect() behind application shutdown so publishers are finished first."],"tags":["go","nats","connectivity","publish"],"backgroundTag":"broker-not-connected","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}