geektutu/7days-golang · error

invalid codec type %s

Error message

invalid codec type %s

What it means

NewClient looks up the codec constructor in codec.NewCodecFuncMap by opt.CodecType and fails fast when no codec is registered for that type. Only codec types registered on both client and server (e.g. gee-gob) are supported. This prevents attempting to speak an unknown wire protocol.

Source

Thrown at gee-rpc/day6-load-balance/client.go:216

	// if opts is nil or pass nil as parameter
	if len(opts) == 0 || opts[0] == nil {
		return DefaultOption, nil
	}
	if len(opts) != 1 {
		return nil, errors.New("number of options is more than 1")
	}
	opt := opts[0]
	opt.MagicNumber = DefaultOption.MagicNumber
	if opt.CodecType == "" {
		opt.CodecType = DefaultOption.CodecType
	}
	return opt, nil
}

func NewClient(conn net.Conn, opt *Option) (*Client, error) {
	f := codec.NewCodecFuncMap[opt.CodecType]
	if f == nil {
		err := fmt.Errorf("invalid codec type %s", opt.CodecType)
		log.Println("rpc client: codec error:", err)
		return nil, err
	}
	// send options with server
	if err := json.NewEncoder(conn).Encode(opt); err != nil {
		log.Println("rpc client: options error: ", err)
		_ = conn.Close()
		return nil, err
	}
	return newClientCodec(f(conn), opt), nil
}

func newClientCodec(cc codec.Codec, opt *Option) *Client {
	client := &Client{
		seq:     1, // seq starts with 1, 0 means invalid call
		cc:      cc,
		opt:     opt,
		pending: make(map[uint64]*Call),

View on GitHub (pinned to cf36443821)

Solutions

  1. Use geerpc.DefaultOption or a properly initialized Option whose CodecType matches a registered codec (default 'gee-gob').
  2. Ensure the codec package's init() registering the codec in NewCodecFuncMap is imported on the client.
  3. Make client and server agree on the same CodecType string.

Example fix

// before
opt := &geerpc.Option{} // CodecType empty
client, _ := geerpc.NewClient(conn, opt)
// after
opt := geerpc.DefaultOption
client, _ := geerpc.NewClient(conn, opt)
Defensive patterns

Strategy: validation

Validate before calling

if codec.NewCodecFuncMap == nil || func() bool {
    _, ok := codec.NewCodecFuncMap[opt.CodecType]
    return !ok
}() {
    // use DefaultOption or fix CodecType before NewClient
}
// simpler: always start from a default option
opt := geerpc.DefaultOption

Try / catch

client, err := geerpc.NewClient(conn, opt)
if err != nil {
    if strings.Contains(err.Error(), "invalid codec type") {
        log.Fatalf("codec %q not registered — import the codec package or use DefaultOption", opt.CodecType)
    }
    return err
}

Prevention

When it happens

Trigger: Calling NewClient (directly or via Dial/DialHTTP) with an Option whose CodecType is empty or names an unregistered codec (e.g. 'gob' instead of the registered 'gee-gob').

Common situations: Constructing &Option{} manually and forgetting to set CodecType or call default initialization; using NewHTTPCodec default opts whose codec differs from server; version mismatch where client/server register different codec names.

Related errors


AI-assisted analysis of geektutu/7days-golang@cf36443821 (2026-09-03). Data as JSON: /api/errors/b605ad608b09609e. Report an issue: GitHub.