Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remote polish #147

Merged
merged 2 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ compiler.
Let's go through a Hello world message. The complete example is available in the
[hello world](examples/helloworld) folder. Let's start in main:
```go
engine, err := actor.NewEngine(nil)
engine, err := actor.NewEngine(actor.NewEngineConfig())
```
This creates a new engine. The engine is the core of Hollywood. It's responsible for spawning actors, sending messages
and handling the lifecycle of actors. If Hollywood fails to create the engine it'll return an error. For development
Expand Down Expand Up @@ -212,21 +212,18 @@ This works the same as local actors but "over the wire". Hollywood supports seri

### Configuration

remote.New() takes a remote.Config struct. This struct contains the following fields:
- ListenAddr string
- TlsConfig *tls.Config
remote.New() takes a listen address and a remote.Config struct.

You'll instantiate a new remote with the following code:
```go
var engine *actor.Engine
remote := remote.New("0.0.0.0:2222",
&remote.Config{TlsConfig: &tls.Config{
Certificates: []tls.Certificate{cert},
},
}
})
var err error
engine, err = actor.NewEngine(actor.EngineOptRemote(remote))
tlsConfig := TlsConfig: &tls.Config{
Certificates: []tls.Certificate{cert},
}

config := remote.NewConfig().WithTLS(tlsConfig)
remote := remote.New("0.0.0.0:2222", config)

engine, err := actor.NewEngine(actor.NewEngineConfig().WithRemote(remote))
```

Look at the [Remote actor examples](examples/remote) and the [Chat client & Server](examples/chat) for more information.
Expand Down
2 changes: 1 addition & 1 deletion cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ type Cluster struct {
// New returns a new cluster given a Config.
func New(config Config) (*Cluster, error) {
if config.engine == nil {
remote := remote.New(config.listenAddr, nil)
remote := remote.New(config.listenAddr, remote.NewConfig())
e, err := actor.NewEngine(actor.NewEngineConfig().WithRemote(remote))
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func TestMemberLeave(t *testing.T) {
c1Addr := getRandomLocalhostAddr()
c2Addr := getRandomLocalhostAddr()

remote := remote.New(c2Addr, nil)
remote := remote.New(c2Addr, remote.NewConfig())
e, err := actor.NewEngine(actor.NewEngineConfig().WithRemote(remote))
if err != nil {
log.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion examples/chat/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func main() {
if *listenAt == "" {
*listenAt = fmt.Sprintf("127.0.0.1:%d", rand.Int31n(50000)+10000)
}
rem := remote.New(*listenAt, nil)
rem := remote.New(*listenAt, remote.NewConfig())
e, err := actor.NewEngine(actor.NewEngineConfig().WithRemote(rem))
if err != nil {
slog.Error("failed to create engine", "err", err)
Expand Down
2 changes: 1 addition & 1 deletion examples/chat/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func main() {
listenAt = flag.String("listen", "127.0.0.1:4000", "")
)
flag.Parse()
rem := remote.New(*listenAt, nil)
rem := remote.New(*listenAt, remote.NewConfig())
e, err := actor.NewEngine(actor.NewEngineConfig().WithRemote(rem))
if err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion examples/mdns/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func main() {
// pick a random port, 2000 and up.
*port = rand.Intn(10000) + 2000
}
rem := remote.New(fmt.Sprintf("%s:%d", *ip, *port), nil)
rem := remote.New(fmt.Sprintf("%s:%d", *ip, *port), remote.NewConfig())
engine, err := actor.NewEngine(actor.NewEngineConfig().WithRemote(rem))
if err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion examples/remote/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func main() {
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug})))
r := remote.New("127.0.0.1:3000", nil)
r := remote.New("127.0.0.1:3000", remote.NewConfig())
e, err := actor.NewEngine(actor.NewEngineConfig().WithRemote(r))
if err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion examples/remote/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (f *server) Receive(ctx *actor.Context) {

func main() {
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug})))
r := remote.New("127.0.0.1:4000", nil)
r := remote.New("127.0.0.1:4000", remote.NewConfig())
e, err := actor.NewEngine(actor.NewEngineConfig().WithRemote(r))
if err != nil {
panic(err)
Expand Down
35 changes: 22 additions & 13 deletions remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,24 @@ import (

// Config holds the remote configuration.
type Config struct {
TlsConfig *tls.Config
Wg *sync.WaitGroup
TLSConfig *tls.Config
// Wg *sync.WaitGroup
}

// NewConfig returns a new default remote configuration.
func NewConfig() Config {
return Config{}
}

// WithTLS sets the TLS config of the remote which will set
// the transport of the Remote to TLS.
func (c Config) WithTLS(tlsconf *tls.Config) Config {
c.TLSConfig = tlsconf
return c
}

type Remote struct {
addr string
tlsConfig *tls.Config
wg *sync.WaitGroup
engine *actor.Engine
config Config
streamReader *streamReader
Expand All @@ -41,13 +51,10 @@ const (
)

// New creates a new "Remote" object given a Config.
func New(addr string, cfg *Config) *Remote {
func New(addr string, config Config) *Remote {
r := &Remote{
addr: addr,
}
if cfg != nil {
r.tlsConfig = cfg.TlsConfig
r.wg = cfg.Wg
addr: addr,
config: config,
}
r.state.Store(stateInitialized)
r.streamReader = newStreamReader(r)
Expand All @@ -62,12 +69,12 @@ func (r *Remote) Start(e *actor.Engine) error {
r.engine = e
var ln net.Listener
var err error
switch r.config.TlsConfig {
switch r.config.TLSConfig {
case nil:
ln, err = net.Listen("tcp", r.addr)
default:
slog.Debug("remote using TLS for listening")
ln, err = tls.Listen("tcp", r.addr, r.config.TlsConfig)
ln, err = tls.Listen("tcp", r.addr, r.config.TLSConfig)
}
if err != nil {
return fmt.Errorf("remote failed to listen: %w", err)
Expand All @@ -80,7 +87,9 @@ func (r *Remote) Start(e *actor.Engine) error {
}
s := drpcserver.New(mux)

r.streamRouterPID = r.engine.Spawn(newStreamRouter(r.engine, r.config.TlsConfig), "router", actor.WithInboxSize(1024*1024))
r.streamRouterPID = r.engine.Spawn(
newStreamRouter(r.engine, r.config.TLSConfig),
"router", actor.WithInboxSize(1024*1024))
slog.Debug("server started", "listenAddr", r.addr)
r.stopWg = &sync.WaitGroup{}
r.stopWg.Add(1)
Expand Down
2 changes: 1 addition & 1 deletion remote/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func TestWeird(t *testing.T) {

func makeRemoteEngine(listenAddr string) (*actor.Engine, *Remote, error) {
var e *actor.Engine
r := New(listenAddr, nil)
r := New(listenAddr, NewConfig())
var err error
e, err = actor.NewEngine(actor.NewEngineConfig().WithRemote(r))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion remote/remote_tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func generateCert(ca *x509.Certificate, caKey *ecdsa.PrivateKey) (*tls.Certifica
func makeRemoteEngineTls(listenAddr string, config *tls.Config) (*actor.Engine, *Remote, error) {
var eng *actor.Engine
var err error
rem := New(listenAddr, &Config{TlsConfig: config})
rem := New(listenAddr, NewConfig().WithTLS(config))
eng, err = actor.NewEngine(actor.NewEngineConfig().WithRemote(rem))
if err != nil {
return nil, nil, fmt.Errorf("actor.NewEngine: %w", err)
Expand Down