forked from asynkron/protoactor-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port over cluster pubsub feat from .net asynkron#599
- Loading branch information
Showing
20 changed files
with
2,976 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
protoc -I=../actor --go_out=. --go_opt=paths=source_relative --proto_path=. cluster.proto | ||
protoc -I=../actor --go_out=. --go_opt=paths=source_relative --proto_path=. gossip.proto | ||
protoc -I=../actor --go_out=. --go_opt=paths=source_relative --proto_path=. grain.proto | ||
protoc -I=../actor --go_out=. --go_opt=paths=source_relative --proto_path=. pubsub.proto | ||
|
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 @@ | ||
package cluster | ||
|
||
import "golang.org/x/net/context" | ||
|
||
// KeyValueStore is a distributed key value store | ||
type KeyValueStore[T any] interface { | ||
// Set the value for the given key. | ||
Set(ctx context.Context, key string, value T) error | ||
// Get the value for the given key.. | ||
Get(ctx context.Context, key string) (T, error) | ||
// Clear the value for the given key. | ||
Clear(ctx context.Context, key string) error | ||
} | ||
|
||
// EmptyKeyValueStore is a key value store that does nothing. | ||
type EmptyKeyValueStore[T any] struct{} | ||
|
||
func (e *EmptyKeyValueStore[T]) Set(_ context.Context, _ string, _ T) error { return nil } | ||
|
||
func (e *EmptyKeyValueStore[T]) Get(_ context.Context, _ string) (T, error) { | ||
var r T | ||
return r, nil | ||
} | ||
|
||
func (e *EmptyKeyValueStore[T]) Clear(_ context.Context, _ string) error { return nil } |
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,57 @@ | ||
package cluster | ||
|
||
import ( | ||
"github.com/asynkron/protoactor-go/actor" | ||
"github.com/asynkron/protoactor-go/extensions" | ||
"time" | ||
) | ||
|
||
const PubSubDeliveryName = "$pubsub-delivery" | ||
|
||
var pubsubExtensionID = extensions.NextExtensionID() | ||
|
||
type PubSub struct { | ||
cluster *Cluster | ||
} | ||
|
||
func NewPubSub(cluster *Cluster) *PubSub { | ||
p := &PubSub{ | ||
cluster: cluster, | ||
} | ||
cluster.ActorSystem.Extensions.Register(p) | ||
return p | ||
} | ||
|
||
// Start the PubSubMemberDeliveryActor | ||
func (p *PubSub) Start() { | ||
props := actor.PropsFromProducer(func() actor.Actor { | ||
return NewPubSubMemberDeliveryActor(p.cluster.Config.PubSubConfig.SubscriberTimeout) | ||
}) | ||
_, err := p.cluster.ActorSystem.Root.SpawnNamed(props, PubSubDeliveryName) | ||
if err != nil { | ||
panic(err) // let it crash | ||
} | ||
} | ||
|
||
func (p *PubSub) ExtensionID() extensions.ExtensionID { | ||
return pubsubExtensionID | ||
} | ||
|
||
type PubSubConfig struct { | ||
// SubscriberTimeout is a timeout used when delivering a message batch to a subscriber. Default is 5s. | ||
// | ||
// This value gets rounded to seconds for optimization of cancellation token creation. Note that internally, | ||
// cluster request is used to deliver messages to ClusterIdentity subscribers. | ||
SubscriberTimeout time.Duration | ||
} | ||
|
||
func newPubSubConfig() *PubSubConfig { | ||
return &PubSubConfig{ | ||
SubscriberTimeout: 5 * time.Second, | ||
} | ||
} | ||
|
||
// GetPubSub returns the PubSub extension from the actor system | ||
func GetPubSub(system *actor.ActorSystem) *PubSub { | ||
return system.Extensions.Get(pubsubExtensionID).(*PubSub) | ||
} |
Oops, something went wrong.