{"record":{"id":"0f38048073667582","repo":"micro/go-micro","slug":"not-connected","errorCode":null,"errorMessage":"not connected","messagePattern":"not connected","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"broker/memory.go","lineNumber":95,"sourceCode":"\t}\n\n\tm.connected = false\n\n\treturn nil\n}\n\nfunc (m *memoryBroker) Init(opts ...Option) error {\n\tfor _, o := range opts {\n\t\to(m.opts)\n\t}\n\treturn nil\n}\n\nfunc (m *memoryBroker) Publish(topic string, msg *Message, opts ...PublishOption) error {\n\tm.RLock()\n\tif !m.connected {\n\t\tm.RUnlock()\n\t\treturn errors.New(\"not connected\")\n\t}\n\n\tsubs, ok := m.Subscribers[topic]\n\tm.RUnlock()\n\tif !ok {\n\t\treturn nil\n\t}\n\n\tvar v interface{}\n\tif m.opts.Codec != nil {\n\t\tbuf, err := m.opts.Codec.Marshal(msg)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tv = buf\n\t} else {\n\t\tv = msg\n\t}","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/broker/memory.go#L77-L113","documentation":"The in-memory broker's Publish method refuses to publish when the broker has not been connected (or has been disconnected). memoryBroker tracks connectivity with a `connected` flag guarded by an RWMutex; Publish checks it first and short-circuits with this error. The in-memory broker still requires an explicit Connect() call before it will accept messages, unlike a naive expectation that it is always ready.","triggerScenarios":"Calling broker.Publish(topic, msg) before calling broker.Connect(), or after calling Disconnect(). Any Publish issued on a `memoryBroker` whose `m.connected` is false returns exactly this error from broker/memory.go:95.","commonSituations":"Scripts that construct a memory broker and immediately publish without Connect(); tests that share a broker across subtests where one subtest called Disconnect(); publishing from a goroutine that starts before broker initialization completes.","solutions":["Call broker.Connect(context) before the first Publish and check its error.","If the broker may have been disconnected, call Connect() again (it is idempotent for the memory broker).","Ensure no code path calls Disconnect() while publishing is still expected; coordinate shutdown ordering.","Wrap Publish in a retry that calls Connect() on 'not connected' and re-publishes."],"exampleFix":"// before\nbroker := memory.NewBroker()\n_ = broker.Publish(context, \"events\", msg)\n\n// after\nbroker := memory.NewBroker()\nif err := broker.Connect(context); err != nil {\n    log.Fatal(err)\n}\nif err := broker.Publish(context, \"events\", msg); err != nil {\n    log.Fatal(err)\n}","handlingStrategy":"validation","validationCode":"type connectable interface {\n    Connect(context.Context) error\n    Connected() bool // if exposed; otherwise track locally\n}\nfunc ensureConnected(b broker.Broker, ctx context.Context) error {\n    if c, ok := b.(connectable); ok {\n        return c.Connect(ctx)\n    }\n    return nil\n}","typeGuard":"func isConnected(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        if cerr := br.Connect(ctx); cerr == nil {\n            err = br.Publish(ctx, topic, msg)\n        } else {\n            err = cerr\n        }\n    }\n    return err\n}","preventionTips":["Always call Connect() immediately after constructing the broker and treat it as required, even for the memory broker.","Centralize broker lifecycle (connect on startup, disconnect on shutdown) in one place.","Never call Disconnect() while publishers are still active; use wait groups to drain publishers first.","Add a startup test asserting Connect() succeeds before any publish path runs."],"tags":["go","broker","memory-broker","lifecycle"],"backgroundTag":"broker-not-connected","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}