agentleFS
muratmirgunmuratmirgun/gophers/skills/go-grpc/SKILL.md

go-grpc

Skill9 starsChanged 4 months agoMITView on GitHub

Use when implementing or reviewing gRPC servers/clients in Go. Covers .proto organisation, code generation with protoc/buf, server bootstrap (interceptors, health, graceful shutdown), client patterns (reuse, deadlines, retries), status.Code error handling, streaming, TLS/mTLS, and bufconn testing. Apply when writing .proto files, adding interceptors, or auditing a service for production readiness.

2 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Treat gRPC as a transport.

    line 11

≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Go gRPC

Treat gRPC as a transport. Keep .proto-generated code and business logic separated. The official Go implementation is google.golang.org/grpc; pair it with protoc-gen-go + protoc-gen-go-grpc (or buf generate).

Core Rules

  1. One concern per layer. .proto defines the contract; generated code lives in gen/; service implementation lives in internal/. Never edit generated files.
  2. Always wrap RPC arguments in Request/Response messages. Bare scalars (string, int32) cannot be evolved without breaking callers.
  3. Return typed status codes, never raw errors. A fmt.Errorf becomes codes.Unknown on the wire — the client cannot decide whether to retry.
  4. Every client call has a deadline. No context.Background() to a remote service. Set context.WithTimeout per call.
  5. Reuse connections. HTTP/2 multiplexes; creating a new grpc.ClientConn per request is a TLS handshake leak.
  6. Disable reflection in production. Reflection is a developer convenience that doubles as an API enumeration tool for attackers.

When to Use What

NeedUse
Define service.proto file in proto/<service>/v1/
Generate stubsbuf generate or protoc --go_out --go-grpc_out
Cross-cutting (auth, logging, recovery)grpc.ChainUnaryInterceptor / ChainStreamInterceptor
Health probes (Kubernetes)grpc_health_v1 from google.golang.org/grpc/health
Errors with detailsstatus.Errorf(codes.X, ...) + WithDetails(errdetails.BadRequest{...})
Testsgoogle.golang.org/grpc/test/bufconn
Service mesh / mTLScredentials.NewTLS or delegate to Istio/Linkerd

Read references/proto-and-codegen.md when organizing .proto packages or wiring buf. Read references/status-and-errors.md when mapping domain errors to gRPC codes.

Server Bootstrap

import (
    "google.golang.org/grpc"
    "google.golang.org/grpc/health"
    healthpb "google.golang.org/grpc/health/grpc_health_v1"
)

srv := grpc.NewServer(
    grpc.ChainUnaryInterceptor(recoveryUnary, loggingUnary, authUnary),
    grpc.ChainStreamInterceptor(recoveryStream, loggingStream),
)
pb.RegisterUserServiceServer(srv, &userService{...})
healthpb.RegisterHealthServer(srv, health.NewServer())

go func() { _ = srv.Serve(lis) }()

// Graceful shutdown bounded by a hard timeout.
<-shutdownSignal
stopped := make(chan struct{})
go func() { srv.GracefulStop(); close(stopped) }()
select {
case <-stopped:
case <-time.After(15 * time.Second):
    srv.Stop()
}

Three pieces are non-negotiable: interceptors for cross-cutting concerns, health service for Kubernetes probes, and a bounded graceful shutdown.

Client Bootstrap

conn, _ := grpc.NewClient("dns:///user-service:50051",
    grpc.WithTransportCredentials(credentials.NewTLS(tlsCfg)),
    grpc.WithDefaultServiceConfig(`{
      "loadBalancingPolicy": "round_robin",
      "methodConfig": [{
        "name": [{"service": "user.v1.UserService"}],
        "timeout": "5s",
        "retryPolicy": {
          "maxAttempts": 3, "initialBackoff": "0.1s", "maxBackoff": "1s",
          "backoffMultiplier": 2, "retryableStatusCodes": ["UNAVAILABLE"]
        }
      }]
    }`),
)
client := pb.NewUserServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second); defer cancel()
resp, err := client.GetUser(ctx, &pb.GetUserRequest{Id: id})

The service config is the right place for retries — let the library handle the loop, backoff, and UNAVAILABLE-only filter.

Errors

A raw Go error returned from an RPC becomes codes.Unknown. The client cannot tell a 404 from a 500. Always use status.Errorf:

if errors.Is(err, ErrNotFound) {
    return nil, status.Errorf(codes.NotFound, "user %q not found", req.Id)
}
if errors.As(err, &validationErr) {
    st, _ := status.New(codes.InvalidArgument, "validation").WithDetails(
        &errdetails.BadRequest{FieldViolations: violations(validationErr)},
    )
    return nil, st.Err()
}
return nil, status.Errorf(codes.Internal, "lookup: %v", err)

Quick map:

DomainCode
Missing/invalid fieldInvalidArgument
Not foundNotFound
Already existsAlreadyExists
UnauthenticatedUnauthenticated
Authenticated but forbiddenPermissionDenied
Rate-limitedResourceExhausted
Dependency down, retriableUnavailable
Bug, unexpectedInternal

Streaming

PatternUse case
Server streamingLog tailing, paginated result sets, server-sent events
Client streamingFile upload, batch ingest
BidirectionalChat, real-time sync

Streams must respect ctx.Done(). A goroutine reading from a stream after the client disconnects is a slow leak.

func (s *server) ListUsers(req *pb.ListUsersRequest, stream pb.UserService_ListUsersServer) error {
    for _, u := range s.repo.All(stream.Context()) {
        if err := stream.Send(toProto(u)); err != nil {
            return err // includes ctx canceled
        }
    }
    return nil
}

Testing with bufconn

bufconn is an in-memory net.Listener. It exercises the real gRPC stack — interceptors, marshaling, metadata — without binding a TCP port. See references/testing.md for the full harness plus table-driven status-code assertions, metadata injection, and stream testing.

Security Notes

  • TLS in production. Plaintext is only acceptable behind a confirmed-private network (and even then mTLS is preferable).
  • For service-to-service auth, prefer a mesh (Istio/Linkerd) over hand-rolled token validation.
  • For user auth, implement credentials.PerRPCCredentials to attach a token and validate inside an auth interceptor.
  • Reflection: enable in dev, disable in prod via build tag or env flag.

Anti-Patterns

Anti-patternWhy it hurtsDo this instead
return fmt.Errorf("not found")Wire code is Unknown, clients can't retry-discriminatestatus.Errorf(codes.NotFound, ...)
context.Background() to a client callNo deadline → goroutines pile up on a slow dependencycontext.WithTimeout(parent, 5s)
New ClientConn per requestTLS handshake every call; sockets exhaustOne grpc.NewClient at startup, reuse
Bare string as RPC argumentCannot add fields without breaking callersAlways Request/Response messages
Reflection on in productionLets attackers enumerate every methodCompile-out with build tag in prod
codes.Internal for all errorsClient retry config can't distinguish bugs from outagesMap domain → specific codes
No health serviceKubernetes can't gate traffic; rolling deploys breakRegister grpc_health_v1
Ignoring stream.Context().Done()Goroutines run after client disconnectSelect on ctx.Done() in stream loops

Verification Checklist

  • .proto packages are versioned (pkg/v1, not pkg)
  • All RPCs take Request and return Response messages
  • Generated code is in a separate directory, never edited
  • Every error return uses status.Errorf with a specific code
  • Every client call has a deadline via context.WithTimeout
  • Server registers grpc_health_v1
  • GracefulStop is bounded by a time.After fallback
  • Reflection is gated to non-production builds
  • Tests use bufconn and assert status.Code(err)

References

  • references/proto-and-codegen.md — .proto layout, buf.yaml, codegen flags
  • references/status-and-errors.md — code mapping, rich details with errdetails
  • references/testing.md — bufconn, metadata, streaming assertions
  • references/anti-patterns.md — detailed walkthrough of each anti-pattern

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.