表更改后立即使用 gRPC 传输数据库数据

表更改后立即使用 grpc 传输数据库数据

问题内容

我正在使用 go、grpc 和 postgres 制作任务列表。

如何在调用 postitem 插入新数据时自动流式传输数据?我需要订阅 postgres 还是可以在不订阅或发布订阅的情况下完成此操作?

// protobuf 架构

syntax = "proto3";package tasklist;import "google/protobuf/empty.proto";service todolist {  rpc gettasks(google.protobuf.empty) returns (stream gettasksresponse) {}  rpc postitem(postitemrequest) returns (posttaskrequest) {}}message task {  int64 id = 1;  string name = 2;}message gettasksresponse {  task task = 1;}message posttaskrequest {  task task = 1;}message postitemresponse {  bool result = 1;}

登录后复制

// postgres 表架构

create table task (  id integer not null primary key,  name varchar(10) not null);

登录后复制

// 继续

func (s *server) GetTasks(_ *empty.Empty, stream pb.TaskList_GetTasksServer) error {    // How can I steam data as soon as `PostTask` is called to update db? 

正确答案


我猜 s.requests 类似于 chan task。因此,在成功 // update postgres here 之后,您可以在 chan 中发送您的请求。

func (s *server) PostTask(ctx context.Context, r *pb.PostTaskRequest) (*pb.PostTaskResponse, error) {    postTask := toDomain(r)    err := s.service.UpdateTask(ctx, postTask)    if err != nil {        return nil, status.Error(codes.Internal, err.Error())    }    s.requests 

登录后复制

以上就是表更改后立即使用 gRPC 传输数据库数据的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2485843.html

(0)
上一篇 2025年3月4日 21:52:19
下一篇 2025年2月24日 00:46:53

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论