1 Star 0 Fork 0

jack/protoactor-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
pubsub_publisher.go 1.96 KB
一键复制 编辑 原始数据 按行查看 历史
490689386@qq.com 提交于 2025-05-19 14:50 +08:00 . 初始化
package cluster
import (
"context"
"log/slog"
"time"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/durationpb"
)
type PublisherConfig struct {
IdleTimeout time.Duration
}
type Publisher interface {
// Initialize the internal mechanisms of this publisher.
Initialize(ctx context.Context, topic string, config PublisherConfig) (*Acknowledge, error)
// PublishBatch publishes a batch of messages to the topic.
PublishBatch(ctx context.Context, topic string, batch *PubSubBatch, opts ...GrainCallOption) (*PublishResponse, error)
// Publish publishes a single message to the topic.
Publish(ctx context.Context, topic string, message proto.Message, opts ...GrainCallOption) (*PublishResponse, error)
Logger() *slog.Logger
}
type defaultPublisher struct {
cluster *Cluster
}
func NewPublisher(cluster *Cluster) Publisher {
return &defaultPublisher{
cluster: cluster,
}
}
func (p *defaultPublisher) Logger() *slog.Logger {
return p.cluster.Logger()
}
func (p *defaultPublisher) Initialize(ctx context.Context, topic string, config PublisherConfig) (*Acknowledge, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
res, err := p.cluster.Request(topic, TopicActorKind, &Initialize{
IdleTimeout: durationpb.New(config.IdleTimeout),
})
if err != nil {
return nil, err
}
return res.(*Acknowledge), err
}
}
func (p *defaultPublisher) PublishBatch(ctx context.Context, topic string, batch *PubSubBatch, opts ...GrainCallOption) (*PublishResponse, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
res, err := p.cluster.Request(topic, TopicActorKind, batch, opts...)
if err != nil {
return nil, err
}
return res.(*PublishResponse), err
}
}
func (p *defaultPublisher) Publish(ctx context.Context, topic string, message proto.Message, opts ...GrainCallOption) (*PublishResponse, error) {
return p.PublishBatch(ctx, topic, &PubSubBatch{
Envelopes: []proto.Message{message},
}, opts...)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wujianhai/protoactor-go.git
git@gitee.com:wujianhai/protoactor-go.git
wujianhai
protoactor-go
protoactor-go
5633fe2499dd

搜索帮助