-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from grafana/grpc
Add gRPC support + two gRPC endpoints + misc fixes
- Loading branch information
Showing
40 changed files
with
596 additions
and
4,720 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Client, StatusOK } from 'k6/net/grpc'; | ||
import { check, sleep } from 'k6'; | ||
|
||
const BASE_URL = 'localhost:3334'; | ||
|
||
const client = new Client(); | ||
client.load(['definitions'], '../../../proto/quickpizza.proto'); | ||
|
||
export default () => { | ||
client.connect(BASE_URL, { | ||
plaintext: true | ||
}); | ||
|
||
const data = { ingredients: ["Pepperoni", "Mozzarella"], dough: "Stuffed" }; | ||
const response = client.invoke('quickpizza.GRPC/EvaluatePizza', data); | ||
|
||
check(response, { | ||
'status is OK': (r) => r && r.status === StatusOK, | ||
}); | ||
|
||
console.log(JSON.stringify(response.message)); | ||
|
||
client.close(); | ||
sleep(1); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package grpc | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"math/rand" | ||
"net" | ||
|
||
pb "github.com/grafana/quickpizza/pkg/grpc/quickpizza" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
type serverImplementation struct { | ||
pb.UnimplementedGRPCServer | ||
} | ||
|
||
type Server struct { | ||
grpcServer *grpc.Server | ||
listen string | ||
} | ||
|
||
func (s *serverImplementation) Status(_ context.Context, in *pb.StatusRequest) (*pb.StatusResponse, error) { | ||
return &pb.StatusResponse{Ready: true}, nil | ||
} | ||
|
||
func (s *serverImplementation) EvaluatePizza(_ context.Context, in *pb.PizzaEvaluationRequest) (*pb.PizzaEvaluationResponse, error) { | ||
var rating int32 | ||
if len(in.Ingredients) > 0 && in.Dough != "" { | ||
rating = rand.Int31n(6) | ||
} | ||
return &pb.PizzaEvaluationResponse{ | ||
StarsRating: rating, | ||
}, nil | ||
} | ||
|
||
func NewServer(listen string) *Server { | ||
s := grpc.NewServer() | ||
pb.RegisterGRPCServer(s, &serverImplementation{}) | ||
|
||
return &Server{grpcServer: s, listen: listen} | ||
} | ||
|
||
func (s *Server) ListenAndServe() error { | ||
lis, err := net.Listen("tcp", s.listen) | ||
if err != nil { | ||
return fmt.Errorf("failed to listen on port: %w", err) | ||
} | ||
|
||
slog.Info("Starting QuickPizza gRPC", "listenAddress", s.listen) | ||
return s.grpcServer.Serve(lis) | ||
} |
Oops, something went wrong.