创建 gRPC 客户端请求时出现无效的内存地址,其中包含重复字段。

huangapple go评论95阅读模式
英文:

invalid memory address when creating grpc client request with repeated field

问题

我有一个关于我的grpc服务器和客户端的问题,我正在尝试在grpc中创建一个重复字段,并将其发送给客户端。但是在错误上失败了,我在我的grpc客户端/服务器上做错了什么?我尝试将RequestsCommand作为值发送,但这没有起作用。

panic: 运行时错误:无效的内存地址或空指针解引用
[信号SIGSEGV:分段违规代码=0x1 addr=0x20 pc=0x165cf0e]

goroutine 40 [运行中]:
main.(*Server).RunCommands(0x172b960?,{0x18be540?,0xc00031c030?},0x107a360?)
<autogenerated>:1 +0x2e
github.com/mygithub/some-package/proto._ExecuterService_RunCommands_Handler({0x1701a20?,0xc0002430d0},{0x18be540,0xc00031c030},0xc000318070,0x0)
..../executer_grpc.pb.go:93 +0x170

executer_grpc.pb.go:93 是:

in := new(CommandsRequest)
return srv.(ExecuterServiceServer).RunCommands(ctx, in)

在服务器端,我有以下内容:

grpc_server.go:

package main

import (
"fmt"
pb "github.com/mygithub/some-package/proto"
"google.golang.org/grpc"
)

type Server struct {
pb.ExecuterServiceServer
}

func startServer() {
addr := GetAgentAddress()
lis, err := net.Listen("tcp", addr)

if err != nil {
    log.Fatalf("Faild to listen on %v\n", err)
}
s := grpc.NewServer()
pb.RegisterExecuterServiceServer(s, &Server{})

if err = s.Serve(lis); err != nil {
    log.Fatalf("Failed to serve: %v\n", err)
}

}

grpc_client.go:

package main

import (
"context";
"encoding/json"
"fmt"
v1 "github.com/mygithub/some-package/api/v1"
pb "github.com/mygithub/some-package/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

func sendCommandsWithClient() (string, error) {
command := v1.CommandSpec{
BashCommand: "ls",
Async: false,
}

addr := "localhost:4000"
conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
    return "", err
}

defer conn.Close()
client := pb.NewExecuterServiceClient(conn)
if err != nil {
    return "", err
}

commandRequest := &pb.CommandRequest{
    BashCommand: command.BashCommand,
    Slug:        "test",
    Async:       command.Async,
}

req := &pb.CommandsRequest{}
req.Commands = append(req.Commands, commandRequest)
aAsJsonString, _ := json.Marshal(req)
fmt.Println(string(aAsJsonString))
res, err := client.RunCommands(context.Background(), req)

if err != nil {
    return "", err
}
return res.Content, nil

}

func main() {
_, err := sendCommandsWithClient()
if err != nil {
return
}
}

RunCommands:

func (s *Server) RunCommands(ctx context.Context, in *pb.CommandsRequest) (*pb.CommandsResponse, error) {
log.Println("test")
command := v1.CommandSpec{BashCommand: "ls", Slug: "test", Async: true}
CommandsQueue := append(CommandsQueue, command)
size := len(CommandsQueue)
return &pb.CommandsResponse{Status: 0, Content: strconv.Itoa(size)}, nil
}

go版本:go version go1.18.2 darwin/amd64
protoc:proto3

英文:

I have an issue with my grpc server and client, I'm trying to create a repeated field in the grpc and send it to the client. but it failed on error, what am I doing wrong on my grpc client/server? i tried send the RequestsCommand as value but this didn't work to.

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x165cf0e]

goroutine 40 [running]:
main.(*Server).RunCommands(0x172b960?, {0x18be540?, 0xc00031c030?}, 0x107a360?)
        &lt;autogenerated&gt;:1 +0x2e
github.com/mygithub/some-package/proto._ExecuterService_RunCommands_Handler({0x1701a20?, 0xc0002430d0}, {0x18be540, 0xc00031c030}, 0xc000318070, 0x0)
        ..../executer_grpc.pb.go:93 +0x170

executer_grpc.pb.go:93 is :

in := new(CommandsRequest)
return srv.(ExecuterServiceServer).RunCommands(ctx, in)

on the server side I have the following:

grpc_server.go:

package main

import (
	&quot;fmt&quot;
	pb &quot;github.com/mygithub/some-package/proto&quot;
	&quot;google.golang.org/grpc&quot;
)


type Server struct {
	pb.ExecuterServiceServer
}

func startServer() {
	addr := GetAgentAddress()
	lis, err := net.Listen(&quot;tcp&quot;, addr)

	if err != nil {
		log.Fatalf(&quot;Faild to listen on %v\n&quot;, err)
	}
	s := grpc.NewServer()
	pb.RegisterExecuterServiceServer(s, &amp;Server{})

	if err = s.Serve(lis); err != nil {
		log.Fatalf(&quot;Failed to serve: %v\n&quot;, err)
	}

}

grpc_client.go:

package main

import (
	&quot;context&quot;
	&quot;encoding/json&quot;
	&quot;fmt&quot;
	v1 &quot;github.com/mygithub/some-package/api/v1&quot;
	pb &quot;github.com/mygithub/some-package/proto&quot;
	&quot;google.golang.org/grpc&quot;
	&quot;google.golang.org/grpc/credentials/insecure&quot;
)

func sendCommandsWithClient() (string, error) {
	command := v1.CommandSpec{
		BashCommand: &quot;ls&quot;,
		Async:       false,
	}

	addr := &quot;localhost:4000&quot;
	conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		return &quot;&quot;, err
	}

	defer conn.Close()
	client := pb.NewExecuterServiceClient(conn)
	if err != nil {
		return &quot;&quot;, err
	}

	commandRequest := &amp;pb.CommandRequest{
		BashCommand: command.BashCommand,
		Slug:        &quot;test&quot;,
		Async:       command.Async,
	}

	req := &amp;pb.CommandsRequest{}
	req.Commands = append(req.Commands, commandRequest)
	aAsJsonString, _ := json.Marshal(req)
	fmt.Println(string(aAsJsonString))
	res, err := client.RunCommands(context.Background(), req)

	if err != nil {
		return &quot;&quot;, err
	}
	return res.Content, nil
}

func main() {
	_, err := sendCommandsWithClient()
	if err != nil {
		return
	}
}

RunCommands:

func (s *Server) RunCommands(ctx context.Context, in *pb.CommandsRequest) (*pb.CommandsResponse, error) {
	log.Println(&quot;test&quot;)
	command := v1.CommandSpec{BashCommand: &quot;ls&quot;, Slug: &quot;test&quot;, Async: true}
	CommandsQueue := append(CommandsQueue, command)
	size := len(CommandsQueue)
	return &amp;pb.CommandsResponse{Status: 0, Content: strconv.Itoa(size)}, nil
}

go version: go version go1.18.2 darwin/amd64
protoc: proto3

答案1

得分: 0

你应该使用"go build"或"go run"命令来编译或运行你的所有Go文件。

go run main.go model.go somefile.go
英文:

you should go build or go run with all of your go files.

go run main.go model.go somefile.go

huangapple
  • 本文由 发表于 2022年11月6日 21:33:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/74336331.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定