redo pubsub
All checks were successful
Build and Publish / BuildAndDeployAmd64 (push) Successful in 34s
Build and Publish / BuildAndDeployArm64 (push) Successful in 3m49s

This commit is contained in:
matst80
2025-11-25 23:06:36 +01:00
parent f640cd7d2c
commit d5d2b3e711
6 changed files with 80 additions and 142 deletions

View File

@@ -17,12 +17,12 @@ type SimpleGrainPool[V any] struct {
grains map[uint64]Grain[V]
mutationRegistry MutationRegistry
spawn func(ctx context.Context, id uint64) (Grain[V], error)
destroy func(grain Grain[V]) error
spawnHost func(host string) (Host, error)
listeners []LogListener
storage LogStorage[V]
ttl time.Duration
poolSize int
pubsub *PubSub
// Cluster coordination --------------------------------------------------
hostname string
@@ -39,6 +39,7 @@ type GrainPoolConfig[V any] struct {
Hostname string
Spawn func(ctx context.Context, id uint64) (Grain[V], error)
SpawnHost func(host string) (Host, error)
Destroy func(grain Grain[V]) error
TTL time.Duration
PoolSize int
MutationRegistry MutationRegistry
@@ -52,6 +53,7 @@ func NewSimpleGrainPool[V any](config GrainPoolConfig[V]) (*SimpleGrainPool[V],
storage: config.Storage,
spawn: config.Spawn,
spawnHost: config.SpawnHost,
destroy: config.Destroy,
ttl: config.TTL,
poolSize: config.PoolSize,
hostname: config.Hostname,
@@ -89,9 +91,10 @@ func (p *SimpleGrainPool[V]) purge() {
for id, grain := range p.grains {
if grain.GetLastAccess().Before(purgeLimit) {
purgedIds = append(purgedIds, id)
if p.pubsub != nil {
p.pubsub.UnsubscribeAll(id)
if err := p.destroy(grain); err != nil {
log.Printf("failed to destroy grain %d: %v", id, err)
}
delete(p.grains, id)
}
}
@@ -417,11 +420,7 @@ func (p *SimpleGrainPool[V]) Apply(ctx context.Context, id uint64, mutation ...p
if err != nil {
return nil, err
}
if p.pubsub != nil {
if sub, ok := any(grain).(Subscribable); ok {
sub.UpdateSubscriptions(p.pubsub)
}
}
return &MutationResult[*V]{
Result: result,
Mutations: mutations,
@@ -450,21 +449,6 @@ func (p *SimpleGrainPool[V]) Hostname() string {
return p.hostname
}
// GetPubSub returns the pubsub instance.
func (p *SimpleGrainPool[V]) GetPubSub() *PubSub {
return p.pubsub
}
func (p *SimpleGrainPool[V]) SetPubSub(pubsub *PubSub) {
p.pubsub = pubsub
}
func (p *SimpleGrainPool[V]) Publish(event Event) {
if p.pubsub != nil {
p.pubsub.Publish(event)
}
}
// Close notifies remotes that this host is shutting down.
func (p *SimpleGrainPool[V]) Close() {