{"record":{"id":"593f1739ca448937","repo":"redis/go-redis","slug":"memoryusage-expects-single-sample-count","errorCode":null,"errorMessage":"MemoryUsage expects single sample count","messagePattern":"MemoryUsage expects single sample count","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"commands.go","lineNumber":856,"sourceCode":"\nfunc (c cmdable) Time(ctx context.Context) *TimeCmd {\n\tcmd := NewTimeCmd(ctx, \"time\")\n\t_ = c(ctx, cmd)\n\treturn cmd\n}\n\nfunc (c cmdable) DebugObject(ctx context.Context, key string) *StringCmd {\n\tcmd := NewStringCmd(ctx, \"debug\", \"object\", key)\n\t_ = c(ctx, cmd)\n\treturn cmd\n}\n\nfunc (c cmdable) MemoryUsage(ctx context.Context, key string, samples ...int) *IntCmd {\n\targs := []interface{}{\"memory\", \"usage\", key}\n\tif len(samples) > 0 {\n\t\tif len(samples) != 1 {\n\t\t\tcmd := NewIntCmd(ctx)\n\t\t\tcmd.SetErr(errors.New(\"MemoryUsage expects single sample count\"))\n\t\t\treturn cmd\n\t\t}\n\t\targs = append(args, \"SAMPLES\", samples[0])\n\t}\n\tcmd := NewIntCmd(ctx, args...)\n\tcmd.SetFirstKeyPos(2)\n\t_ = c(ctx, cmd)\n\treturn cmd\n}\n\n//------------------------------------------------------------------------------\n\n// ModuleLoadexConfig struct is used to specify the arguments for the MODULE LOADEX command of redis.\n// `MODULE LOADEX path [CONFIG name value [CONFIG name value ...]] [ARGS args [args ...]]`\ntype ModuleLoadexConfig struct {\n\tPath string\n\tConf map[string]interface{}\n\tArgs []interface{}","sourceCodeStart":838,"sourceCodeEnd":874,"githubUrl":"https://github.com/redis/go-redis/blob/c5cad058c72f58370553b48566302303cf8a2e89/commands.go#L838-L874","documentation":"MemoryUsage's variadic samples parameter accepts at most one SAMPLES count, matching the MEMORY USAGE key SAMPLES n syntax. Passing more than one value is rejected locally and returned as the command's error.","triggerScenarios":"Calling rdb.MemoryUsage(ctx, key, 1, 2) or otherwise passing two or more ints in the samples variadic slot.","commonSituations":"Spreading a slice of sample counts (samples...) collected elsewhere; misreading the variadic as a list of per-sample counts.","solutions":["Pass exactly one sample count: rdb.MemoryUsage(ctx, key, 5).","If no SAMPLES argument is needed, call without any variadic values.","If you have a slice, check len==1 and pass only samples[0]."],"exampleFix":"// before\nsamples := []int{1, 5}\nrdb.MemoryUsage(ctx, key, samples...)\n// after\nif len(samples) == 1 {\n    rdb.MemoryUsage(ctx, key, samples[0])\n}","handlingStrategy":"validation","validationCode":"func canCallMemoryUsage(samples []int) bool {\n    return len(samples) <= 1\n}","typeGuard":null,"tryCatchPattern":"cmd := rdb.MemoryUsage(ctx, key, samples...)\nif err := cmd.Err(); err != nil {\n    if strings.Contains(err.Error(), \"expects single sample count\") {\n        cmd = rdb.MemoryUsage(ctx, key, samples[0])\n    }\n}","preventionTips":["Never spread multi-element slices into the samples variadic.","Pass at most one int; omit it entirely for default sampling."],"tags":["memory-usage","validation","arguments"],"backgroundTag":"invalid-command-arguments","analyzedSha":"c5cad058c72f58370553b48566302303cf8a2e89","analyzedAt":"2026-09-01T06:50:53.388Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}