Skip to content

Commit

Permalink
Simplify vat.
Browse files Browse the repository at this point in the history
  • Loading branch information
lthibault committed Jun 1, 2024
1 parent fd2f1cb commit abce4dc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 28 deletions.
27 changes: 3 additions & 24 deletions vat/vat.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,18 @@ type Bootable interface {

type NetConfig struct {
Host host.Host
Guest api.Module
System Bootable
Proto protocol.ID
DialTimeout time.Duration
}

func (c NetConfig) Proto() protocol.ID {
return ProtoFromModule(c.Guest)
}

func (c NetConfig) Build(ctx context.Context) Network {
if c.DialTimeout <= 0 {
c.DialTimeout = time.Second * 10
}

l := ListenConfig{
Host: c.Host,
}.Listen(ctx, c.Proto())
}.Listen(ctx, c.Proto)

return Network{
NetConfig: c,
Expand All @@ -63,22 +58,6 @@ func (n Network) LocalID() rpc.PeerID {
return rpc.PeerID{Value: n.Host.ID()}
}

func (n Network) Serve(ctx context.Context) error {
c := n.System.Boot(n.Guest)
defer c.Release()

for {
if conn, err := n.Accept(ctx, &rpc.Options{
BootstrapClient: c.AddRef(),
Network: n,
}); err == nil {
go n.ServeConn(ctx, conn)
} else {
return err
}
}
}

func (n Network) ServeConn(ctx context.Context, conn *rpc.Conn) {
defer conn.Close()
slog.InfoContext(ctx, "accepted",
Expand Down Expand Up @@ -117,7 +96,7 @@ func (n Network) Dial(id rpc.PeerID, opt *rpc.Options) (*rpc.Conn, error) {

pid := id.Value.(peer.ID)

stream, err := n.Host.NewStream(ctx, pid, n.Proto())
stream, err := n.Host.NewStream(ctx, pid, n.Proto)
if err != nil {
return nil, err
}
Expand Down
22 changes: 18 additions & 4 deletions ww.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log/slog"

"capnproto.org/go/capnp/v3/rpc"
"github.com/ipfs/boxo/path"
iface "github.com/ipfs/kubo/core/coreiface"
"github.com/libp2p/go-libp2p/core/host"
Expand Down Expand Up @@ -95,12 +96,25 @@ func (c Cluster) Serve(ctx context.Context) error {
}
defer mod.Close(ctx)

// Obtain the system client. This gives us an API to our root
// process.
client := sys.Boot(mod)
defer client.Release()

net := vat.NetConfig{
Host: c.Host,
System: sys,
Guest: mod,
Host: c.Host,
Proto: vat.ProtoFromModule(mod),
}.Build(ctx)
defer net.Release()

return net.Serve(ctx)
for {
if conn, err := net.Accept(ctx, &rpc.Options{
BootstrapClient: client.AddRef(),
Network: net,
}); err == nil {
go net.ServeConn(ctx, conn)
} else {
return err
}
}
}

0 comments on commit abce4dc

Please sign in to comment.