Skip to content

Commit c39388d

Browse files
authored
Merge pull request #6263 from oasisprotocol/peternose/trivial/refactor-notifier
go/runtime/registry/notifier: Refactor notifier
2 parents fd95d58 + 33a4b58 commit c39388d

26 files changed

Lines changed: 1184 additions & 794 deletions

File tree

.changelog/6263.trivial.md

Whitespace-only changes.

go/common/service/group.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package service
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"golang.org/x/sync/errgroup"
8+
)
9+
10+
// Group is responsible for concurrently starting a group of services.
11+
type Group struct {
12+
services []Service
13+
}
14+
15+
// NewGroup creates a new service group with the given list of services.
16+
func NewGroup(services ...Service) *Group {
17+
return &Group{
18+
services: services,
19+
}
20+
}
21+
22+
// Add appends one or more services to the group.
23+
func (g *Group) Add(services ...Service) {
24+
g.services = append(g.services, services...)
25+
}
26+
27+
// Serve starts all services concurrently and blocks until they all complete,
28+
// one returns an error, or the context is canceled.
29+
func (g *Group) Serve(ctx context.Context) error {
30+
group, ctx := errgroup.WithContext(ctx)
31+
32+
for _, s := range g.services {
33+
group.Go(func() error {
34+
if err := s.Serve(ctx); err != nil {
35+
if ns, ok := s.(NamedService); ok {
36+
return fmt.Errorf("%s stopped: %w", ns.Name(), err)
37+
}
38+
return fmt.Errorf("service stopped: %w", err)
39+
}
40+
return nil
41+
})
42+
}
43+
44+
return group.Wait()
45+
}

go/common/service/service.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@ import (
77
"github.com/oasisprotocol/oasis-core/go/common/logging"
88
)
99

10+
// Service is a blocking service.
11+
type Service interface {
12+
// Serve runs the service and blocks until it completes, an error occurs,
13+
// or the context is canceled.
14+
Serve(ctx context.Context) error
15+
}
16+
17+
// NamedService is a blocking service with an associated name.
18+
type NamedService interface {
19+
Service
20+
21+
// Name returns the name of the service.
22+
Name() string
23+
}
24+
1025
// CleanupAble provides a Cleanup method.
1126
type CleanupAble interface {
1227
// Cleanup performs the service specific post-termination cleanup.

go/runtime/host/protocol/connection.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,6 @@ type Handler interface {
3737
Handle(ctx context.Context, body *Body) (*Body, error)
3838
}
3939

40-
// Notifier is a protocol runtime notifier interface.
41-
type Notifier interface {
42-
// Start the notifier.
43-
Start()
44-
45-
// Stop the notifier.
46-
Stop()
47-
}
48-
49-
// NoOpNotifier is the default no-op runtime notifier implementation.
50-
type NoOpNotifier struct{}
51-
52-
// Start the no-op notifier.
53-
func (n *NoOpNotifier) Start() error {
54-
return nil
55-
}
56-
57-
// Stop the no-op notifier.
58-
func (n *NoOpNotifier) Stop() {
59-
}
60-
6140
// Connection is a Runtime Host Protocol connection interface.
6241
type Connection interface {
6342
// Close closes the connection.

go/runtime/registry/handler.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ type RuntimeHostHandlerEnvironment interface {
4040

4141
// GetRuntimeRegistry returns the runtime registry.
4242
GetRuntimeRegistry() Registry
43+
44+
// GetROFLNotifier returns the ROFL notifier.
45+
GetROFLNotifier() (*ROFLNotifier, error)
4346
}
4447

4548
// RuntimeHostHandler is a runtime host handler suitable for compute runtimes. It provides the
@@ -124,7 +127,11 @@ func (h *runtimeHostHandler) Handle(ctx context.Context, rq *protocol.Body) (*pr
124127
func (h *runtimeHostHandler) NewSubHandler(comp *bundle.ExplodedComponent) (host.RuntimeHandler, error) {
125128
switch comp.Kind {
126129
case component.ROFL:
127-
return newSubHandlerROFL(comp, h)
130+
roflNotifier, err := h.env.GetROFLNotifier()
131+
if err != nil {
132+
return nil, err
133+
}
134+
return newSubHandlerROFL(comp, h, roflNotifier)
128135
default:
129136
return nil, fmt.Errorf("cannot create sub-handler for component '%s'", comp.Kind)
130137
}

0 commit comments

Comments
 (0)