{"id":15077730,"url":"https://github.com/kfelter/grpc-example","last_synced_at":"2026-01-04T04:03:22.636Z","repository":{"id":109770261,"uuid":"363686744","full_name":"kfelter/grpc-example","owner":"kfelter","description":"simple grpc event database client and server using golang","archived":false,"fork":false,"pushed_at":"2021-05-06T15:08:35.000Z","size":5759,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-16T14:50:01.658Z","etag":null,"topics":["golang","grpc","protobuf"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kfelter.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-05-02T15:41:58.000Z","updated_at":"2023-11-30T08:17:08.000Z","dependencies_parsed_at":"2023-04-06T08:36:31.335Z","dependency_job_id":null,"html_url":"https://github.com/kfelter/grpc-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/kfelter/grpc-example","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kfelter%2Fgrpc-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kfelter%2Fgrpc-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kfelter%2Fgrpc-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kfelter%2Fgrpc-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kfelter","download_url":"https://codeload.github.com/kfelter/grpc-example/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kfelter%2Fgrpc-example/sbom","scorecard":{"id":558183,"data":{"date":"2025-08-11","repo":{"name":"github.com/kfelter/grpc-example","commit":"3295d423e16e81e45a20364a73fff60c48b71cde"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":2.3,"checks":[{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"Code-Review","score":0,"reason":"Found 0/30 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"SAST","score":0,"reason":"no SAST tool detected","details":["Warn: no pull requests merged into dev branch"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":0,"reason":"license file not detected","details":["Warn: project does not have a license file"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'main'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"Vulnerabilities","score":8,"reason":"2 existing vulnerabilities detected","details":["Warn: Project is vulnerable to: GO-2023-2153 / GHSA-m425-mq94-257g / GHSA-qppj-fm5r-hxr3","Warn: Project is vulnerable to: GO-2024-2611 / GHSA-8r3f-844c-mc37"],"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}}]},"last_synced_at":"2025-08-20T12:53:49.708Z","repository_id":109770261,"created_at":"2025-08-20T12:53:49.708Z","updated_at":"2025-08-20T12:53:49.708Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28197453,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2026-01-04T02:00:06.065Z","response_time":58,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["golang","grpc","protobuf"],"created_at":"2024-09-25T04:30:54.956Z","updated_at":"2026-01-04T04:03:22.616Z","avatar_url":"https://github.com/kfelter.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# grpc-example using golang\nsimple grpc client and server using golang\n\n# Summary\ngRPC - Remote Proceedure Call\n\nIn this project I will create a simple event publishing service using grpc and generate the client and server code. Then I will write some code that uses the client and server.\n\nWorking with protobuf and gRPC servers was suprisingly easy. \nThe proto3 syntax is very readable and actually easier that just writing standard go code. \nOnce the gRPC service is defined in the proto file, you can generate a lot of the boiler plate code that you would have to write yourself otherwise.\n\nThe example service I created is an in memory events database that can return results that are tagged with certain strings.\n\nStream events to the server over gRPC and then request the data back with tag filters.\n\n# Quickstart running this code\n`term 1` - Start the server\n```sh\nmake server\n```\n\n`term 2` - Add many events to the server\n```sh\nmake add-many\n```\n\n`term 3` - Query events\n```sh\nmake get-prod\n```\n\n`term 3` - Get Server Metrics\n```\nmake metric\n```\n\n# Resources\n| title | link|\n| - | - |\n| grpc introduction   |   https://grpc.io/docs/what-is-grpc/introduction/ |\n| grpc golang quickstart | https://grpc.io/docs/languages/go/quickstart/ |\n| grpc golang basics    | https://grpc.io/docs/languages/go/basics/ |\n\n# Creating the proto file\nThis proto file contains the definitions of the grpc server.\n\nThe EventStore service exposes the `GetEvents`, `StoreEvents`, and `ServerMetrics` Remote Proceedure Calls. Each `rpc` has inputs and outputs that are defined in the same proto file.\n\n`eventstore/event_store.proto`\n```proto3\nsyntax = \"proto3\";\n\noption go_package = \"github.com/kfelter/grpc-example/eventstore\";\n\npackage eventstore;\n\n// Interface exported by the server.\nservice EventStore {\n  // Get all events that match a set of tags, an event must have all the tags that are requested to be returned\n  rpc GetEvents(GetEventRequest) returns (stream Event) {}\n\n  // Stores events that come in on the stream\n  rpc StoreEvents(stream Event) returns (StoreEventsResponse) {}\n\n  // Returns the server metrics\n  rpc ServerMetrics(ServerMestricsRequest) returns (ServerMetricsResponse) {}\n}\n\nmessage Event {\n    repeated string tags = 2;\n    bytes content = 3;\n}\n\nmessage GetEventRequest {\n    repeated string tags = 2;\n}\n\nmessage StoreEventsResponse {\n    repeated Event events = 1;\n    string status = 2;\n}\n\nmessage ServerMestricsRequest {}\n\nmessage ServerMetricsResponse{\n    string status = 1;\n    string avg_get_query_duration = 2;\n    string avg_store_duration = 3;\n    int64 len_events = 4;\n}\n```\n\n# Generating the go code\nUsing the protoc binary we generate the go package code\n```sh\nprotoc --go_out=. --go_opt=paths=source_relative \\\n    --go-grpc_out=. --go-grpc_opt=paths=source_relative \\\n    eventstore/event_store.proto\n```\n\n# Implementing the EventStore gRPC server\nNow in a seperate file we need to implement the interface that was generated by `protoc`\n\n`cmd/server/main.go`\n```go\ntype eventStoreServer struct {\n\tpb.UnimplementedEventStoreServer\n\tmu        sync.Mutex\n\tevents    []*pb.Event\n\tidCounter int64\n}\n\nfunc (s *eventStoreServer) GetEvents(req *pb.GetEventRequest, stream pb.EventStore_GetEventsServer) error {\n...\n}\n\nfunc (s *eventStoreServer) StoreEvents(stream pb.EventStore_StoreEventsServer) error {\n...\n}\n\n\nfunc (s *eventStoreServer) ServerMetrics(c context.Context, req *pb.ServerMestricsRequest) (*pb.ServerMetricsResponse, error) {\n...\n}\n```\n\n# Starting the tcp listener for the gRPC service\nWe need to expose the Remote Proceedure Calls over a network connection.\n\n`cmd/server/main.go`\n```go\nvar (\n\tport = flag.String(\"port\", \"10000\", \"server port\")\n)\n\nfunc newServer() *eventStoreServer {\n\ts := \u0026eventStoreServer{events: make([]*pb.Event, 0)}\n\treturn s\n}\n\nfunc main() {\n\tflag.Parse()\n\tlis, err := net.Listen(\"tcp\", fmt.Sprintf(\":%s\", *port))\n\tif err != nil {\n\t\tlog.Fatalf(\"failed to listen: %v\", err)\n\t}\n\tgrpcServer := grpc.NewServer()\n\tpb.RegisterEventStoreServer(grpcServer, newServer())\n\tgrpcServer.Serve(lis)\n}\n```\n\n# Writing the gRPC client\nThe gRPC client could be writen in many other languages easily by generating the protobuf code in another language.\n\nThis feature makes it very easy to write a service that integrates with many other languages with no extra developer effort.\n\nSince we already generated the go code we will use go to write the client.\n\n`cmd/client/main.go`\n```go\nfunc store(c *cli.Context) error {\n\topts := []grpc.DialOption{grpc.WithInsecure()}\n\tconn, err := grpc.Dial(c.String(\"server-addr\"), opts...)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tdefer conn.Close()\n\tclient := pb.NewEventStoreClient(conn)\n\tstream, err := client.StoreEvents(context.Background())\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tvar content []byte\n\tif cf := c.String(\"content-file\"); cf == \"stdin\" {\n\t\tcontent, err = io.ReadAll(os.Stdin)\n\t} else {\n\t\tcontent, err = os.ReadFile(cf)\n\t}\n\tif err != nil {\n\t\treturn err\n\t}\n\terr = stream.Send(\u0026pb.Event{\n\t\tContent: content,\n\t\tTags:    c.StringSlice(\"tags\"),\n\t})\n\tif err != nil {\n\t\treturn err\n\t}\n\treply, err := stream.CloseAndRecv()\n\tif err != nil {\n\t\treturn err\n\t}\n\tfmt.Println(reply.String())\n\treturn nil\n}\n\nfunc get(c *cli.Context) error {\n\topts := []grpc.DialOption{grpc.WithInsecure()}\n\tconn, err := grpc.Dial(c.String(\"server-addr\"), opts...)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer conn.Close()\n\tclient := pb.NewEventStoreClient(conn)\n\tgetStream, err := client.GetEvents(context.Background(), \u0026pb.GetEventRequest{\n\t\tTags: c.StringSlice(\"tags\"),\n\t})\n\n\tfor {\n\t\te, err := getStream.Recv()\n\t\tif err == io.EOF {\n\t\t\tbreak\n\t\t}\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tfmt.Println(e.String())\n\t}\n\treturn nil\n}\n\nfunc metric(c *cli.Context) error {\n\topts := []grpc.DialOption{grpc.WithInsecure()}\n\tconn, err := grpc.Dial(c.String(\"server-addr\"), opts...)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer conn.Close()\n\tclient := pb.NewEventStoreClient(conn)\n\tres, err := client.ServerMetrics(context.Background(), \u0026pb.ServerMestricsRequest{})\n\tif err != nil {\n\t\treturn err\n\t}\n\tfmt.Println(res.String())\n\treturn nil\n}\n```\n\n# Putting it all together\nLets send some events to the server\n```Makefile\nserver:\n\tgo run cmd/server/main.go\n\nadd-many: # add many events\n\t@go build -o client cmd/client/main.go\n\t@for number in $(shell seq 1 10000); do \\\n    \tprintf \"example event $$number $$(date)\" | ./client store -t \"env:test\";  \\\n\t\tprintf \"example event $$number $$(date)\" | ./client store -t \"env:stage\"; \\\n\t\tprintf \"example event $$number $$(date)\" | ./client store -t \"env:prod\";  \\\n\tdone\n```\n```sh\n$ make add-many\nevents:{tags:\"env:test\" tags:\"id:0\" content:\"example event 1 Sun May  2 18:11:01 EDT 2021\"} status:\"added 1 events\"\nevents:{tags:\"env:stage\" tags:\"id:1\" content:\"example event 1 Sun May  2 18:11:01 EDT 2021\"} status:\"added 1 events\"\nevents:{tags:\"env:prod\" tags:\"id:2\" content:\"example event 1 Sun May  2 18:11:01 EDT 2021\"} status:\"added 1 events\"\nevents:{tags:\"env:test\" tags:\"id:3\" content:\"example event 2 Sun May  2 18:11:01 EDT 2021\"} status:\"added 1 events\"\nevents:{tags:\"env:stage\" tags:\"id:4\" content:\"example event 2 Sun May  2 18:11:01 EDT 2021\"} status:\"added 1 events\"\nevents:{tags:\"env:prod\" tags:\"id:5\" content:\"example event 2 Sun May  2 18:11:01 EDT 2021\"} status:\"added 1 events\"\nevents:{tags:\"env:test\" tags:\"id:6\" content:\"example event 3 Sun May  2 18:11:01 EDT 2021\"} status:\"added 1 events\"\n...\n```\nNow lets pull out the events using GetEvents client code\n```sh\n$ go run cmd/client/main.go get -t \"env:prod\"\ntags:\"env:prod\" tags:\"id:2\" content:\"example event 1 Sun May  2 18:11:01 EDT 2021\"\ntags:\"env:prod\" tags:\"id:5\" content:\"example event 2 Sun May  2 18:11:01 EDT 2021\"\ntags:\"env:prod\" tags:\"id:8\" content:\"example event 3 Sun May  2 18:11:01 EDT 2021\"\ntags:\"env:prod\" tags:\"id:11\" content:\"example event 4 Sun May  2 18:11:02 EDT 2021\"\ntags:\"env:prod\" tags:\"id:14\" content:\"example event 5 Sun May  2 18:11:02 EDT 2021\"\ntags:\"env:prod\" tags:\"id:17\" content:\"example event 6 Sun May  2 18:11:02 EDT 2021\"\n...\n```\nGet Server Metrics\n```sh\n$ make metric\nstatus:\"OK\" avg_get_query_duration:\"7.475534ms\" avg_store_duration:\"89.914µs\" len_events:61708\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkfelter%2Fgrpc-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkfelter%2Fgrpc-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkfelter%2Fgrpc-example/lists"}