{"id":"12fe11a4c0591759","repo":"gofiber/fiber","slug":"fasthttp-lbclient-must-not-be-nil","errorCode":null,"errorMessage":"fasthttp.LBClient must not be nil","messagePattern":"fasthttp\\.LBClient must not be nil","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"client/client.go","lineNumber":952,"sourceCode":"func NewWithClient(c *fasthttp.Client) *Client {\n\tif c == nil {\n\t\tpanic(\"fasthttp.Client must not be nil\")\n\t}\n\treturn newClient(newStandardClientTransport(c))\n}\n\n// NewWithHostClient creates and returns a new Client object from an existing fasthttp.HostClient.\nfunc NewWithHostClient(c *fasthttp.HostClient) *Client {\n\tif c == nil {\n\t\tpanic(\"fasthttp.HostClient must not be nil\")\n\t}\n\treturn newClient(newHostClientTransport(c))\n}\n\n// NewWithLBClient creates and returns a new Client object from an existing fasthttp.LBClient.\nfunc NewWithLBClient(c *fasthttp.LBClient) *Client {\n\tif c == nil {\n\t\tpanic(\"fasthttp.LBClient must not be nil\")\n\t}\n\treturn newClient(newLBClientTransport(c))\n}\n\nfunc newClient(transport httpClientTransport) *Client {\n\treturn &Client{\n\t\ttransport: transport,\n\t\theader: &Header{\n\t\t\tRequestHeader: &fasthttp.RequestHeader{},\n\t\t},\n\t\tparams: &QueryParam{\n\t\t\tArgs: fasthttp.AcquireArgs(),\n\t\t},\n\t\tcookies: &Cookie{},\n\t\tpath:    &PathParam{},\n\n\t\tuserRequestHooks:     []RequestHook{},\n\t\tbuiltinRequestHooks:  []RequestHook{parserRequestURL, parserRequestHeader, parserRequestBody},","sourceCodeStart":934,"sourceCodeEnd":970,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/client/client.go#L934-L970","documentation":"Panics from client/client.go:952 inside NewWithLBClient when the supplied *fasthttp.LBClient is nil. An LBClient is fasthttp's client-side load balancer over multiple backends; a nil one cannot dial or rotate backends, so fiber refuses to build a client on top of it. As with the other transport constructors, this is a fail-fast guard for a nil transport.","triggerScenarios":"Calling client.NewWithLBClient(nil), or passing an LBClient whose .Clients field was never populated (the struct itself non-nil is fine, but a nil pointer is not). Common when the LBClient is built from a config-driven list of backends that happened to be empty and the variable stayed nil.","commonSituations":"Dynamic backend discovery that returns no backends and leaves the LBClient pointer unset; shared/global LBClient initialized in an init() that ran after the client was constructed; tests that pass nil to skip real load balancing.","solutions":["Allocate the LBClient and populate its Clients before passing it: client.NewWithLBClient(&fasthttp.LBClient{Clients: clients, Timeout: 5*time.Second}).","If backend discovery can legitimately yield nothing, short-circuit with client.New() or skip client creation and log the empty-backend condition.","Initialize the LBClient pointer in the same function that builds the fiber client so they cannot get out of sync."],"exampleFix":"// before\napp := client.NewWithLBClient(lb) // lb may be nil\n\n// after\nif lb == nil || len(lb.Clients) == 0 {\n    return fmt.Errorf(\"no backends configured\")\n}\napp := client.NewWithLBClient(lb)","handlingStrategy":"validation","validationCode":"if lb == nil || len(lb.Clients) == 0 {\n    return fmt.Errorf(\"LBClient not configured with backends\")\n}\napp := client.NewWithLBClient(lb)","typeGuard":"func isLBClientReady(c *fasthttp.LBClient) bool {\n    return c != nil && len(c.Clients) > 0\n}","tryCatchPattern":null,"preventionTips":["Populate LBClient.Clients from discovery before constructing the fiber client.","Treat an empty backend list as a fatal config error, not a nil pass-through.","Initialize the LBClient in the same function that builds the fiber client."],"tags":["client","nil-check","constructor","load-balancing","fasthttp"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}