Skip to content

Commit

Permalink
Avoid 'router' terminology (#25)
Browse files Browse the repository at this point in the history
Fixes #20
  • Loading branch information
jackkleeman authored Aug 8, 2024
1 parent 0b88e90 commit adafb6e
Show file tree
Hide file tree
Showing 18 changed files with 97 additions and 97 deletions.
4 changes: 2 additions & 2 deletions example/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
)

var health = restate.
NewServiceRouter("health").
NewService("health").
Handler("ping", restate.NewServiceHandler(
func(restate.Context, restate.Void) (restate.Void, error) {
return restate.Void{}, nil
}))

var bigCounter = restate.
NewObjectRouter("bigCounter").
NewObject("bigCounter").
Handler("add", restate.NewObjectHandler(
func(ctx restate.ObjectContext, deltaText string) (string, error) {
delta, ok := big.NewInt(0).SetString(deltaText, 10)
Expand Down
12 changes: 6 additions & 6 deletions internal/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,18 @@ type ObjectHandlerOption interface {
BeforeObjectHandler(*ObjectHandlerOptions)
}

type ServiceRouterOptions struct {
type ServiceOptions struct {
DefaultCodec encoding.PayloadCodec
}

type ServiceRouterOption interface {
BeforeServiceRouter(*ServiceRouterOptions)
type ServiceOption interface {
BeforeService(*ServiceOptions)
}

type ObjectRouterOptions struct {
type ObjectOptions struct {
DefaultCodec encoding.PayloadCodec
}

type ObjectRouterOption interface {
BeforeObjectRouter(*ObjectRouterOptions)
type ObjectOption interface {
BeforeObject(*ObjectOptions)
}
10 changes: 5 additions & 5 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,24 @@ type withPayloadCodec struct {
}

var _ options.ServiceHandlerOption = withPayloadCodec{}
var _ options.ServiceRouterOption = withPayloadCodec{}
var _ options.ServiceOption = withPayloadCodec{}
var _ options.ObjectHandlerOption = withPayloadCodec{}
var _ options.ObjectRouterOption = withPayloadCodec{}
var _ options.ObjectOption = withPayloadCodec{}

func (w withPayloadCodec) BeforeServiceHandler(opts *options.ServiceHandlerOptions) {
opts.Codec = w.codec
}
func (w withPayloadCodec) BeforeObjectHandler(opts *options.ObjectHandlerOptions) {
opts.Codec = w.codec
}
func (w withPayloadCodec) BeforeServiceRouter(opts *options.ServiceRouterOptions) {
func (w withPayloadCodec) BeforeService(opts *options.ServiceOptions) {
opts.DefaultCodec = w.codec
}
func (w withPayloadCodec) BeforeObjectRouter(opts *options.ObjectRouterOptions) {
func (w withPayloadCodec) BeforeObject(opts *options.ObjectOptions) {
opts.DefaultCodec = w.codec
}

// withPayloadCodec is an option that can be provided to handler/router options
// withPayloadCodec is an option that can be provided to handler/service options
// in order to specify a custom [encoding.PayloadCodec] with which to (de)serialise and
// set content-types instead of the default of JSON.
//
Expand Down
16 changes: 8 additions & 8 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var (
// in which case no input bytes or content type may be sent.
// Output types will be serialised with the provided codec (defaults to JSON) except when they are restate.Void,
// in which case no data will be sent and no content type set.
func Object(object any, opts ...options.ObjectRouterOption) *ObjectRouter {
func Object(object any, opts ...options.ObjectOption) *object {
typ := reflect.TypeOf(object)
val := reflect.ValueOf(object)
var name string
Expand All @@ -40,7 +40,7 @@ func Object(object any, opts ...options.ObjectRouterOption) *ObjectRouter {
} else {
name = reflect.Indirect(val).Type().Name()
}
router := NewObjectRouter(name, opts...)
definition := NewObject(name, opts...)

for m := 0; m < typ.NumMethod(); m++ {
method := typ.Method(m)
Expand Down Expand Up @@ -80,7 +80,7 @@ func Object(object any, opts ...options.ObjectRouterOption) *ObjectRouter {
input := mtype.In(2)
output := mtype.Out(0)

router.Handler(mname, &objectReflectHandler{
definition.Handler(mname, &objectReflectHandler{
options.ObjectHandlerOptions{},
handlerType,
reflectHandler{
Expand All @@ -92,7 +92,7 @@ func Object(object any, opts ...options.ObjectRouterOption) *ObjectRouter {
})
}

return router
return definition
}

// Service converts a struct with methods into a Restate Service where each correctly-typed
Expand All @@ -104,7 +104,7 @@ func Object(object any, opts ...options.ObjectRouterOption) *ObjectRouter {
// in which case no input bytes or content type may be sent.
// Output types will be serialised with the provided codec (defaults to JSON) except when they are restate.Void,
// in which case no data will be sent and no content type set.
func Service(service any, opts ...options.ServiceRouterOption) *ServiceRouter {
func Service(service any, opts ...options.ServiceOption) *service {
typ := reflect.TypeOf(service)
val := reflect.ValueOf(service)
var name string
Expand All @@ -113,7 +113,7 @@ func Service(service any, opts ...options.ServiceRouterOption) *ServiceRouter {
} else {
name = reflect.Indirect(val).Type().Name()
}
router := NewServiceRouter(name, opts...)
definition := NewService(name, opts...)

for m := 0; m < typ.NumMethod(); m++ {
method := typ.Method(m)
Expand Down Expand Up @@ -147,7 +147,7 @@ func Service(service any, opts ...options.ServiceRouterOption) *ServiceRouter {
input := mtype.In(2)
output := mtype.Out(0)

router.Handler(mname, &serviceReflectHandler{
definition.Handler(mname, &serviceReflectHandler{
options.ServiceHandlerOptions{},
reflectHandler{
fn: method.Func,
Expand All @@ -158,7 +158,7 @@ func Service(service any, opts ...options.ServiceRouterOption) *ServiceRouter {
})
}

return router
return definition
}

type reflectHandler struct {
Expand Down
62 changes: 31 additions & 31 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,46 @@ import (
"github.com/restatedev/sdk-go/internal/options"
)

// Router is the set of methods implemented by both services and virtual objects
type Router interface {
// ServiceDefinition is the set of methods implemented by both services and virtual objects
type ServiceDefinition interface {
Name() string
Type() internal.ServiceType
// Set of handlers associated with this router
// Set of handlers associated with this service definition
Handlers() map[string]Handler
}

// ServiceRouter stores a list of handlers under a named Service
type ServiceRouter struct {
// service stores a list of handlers under a named Service
type service struct {
name string
handlers map[string]Handler
options options.ServiceRouterOptions
options options.ServiceOptions
}

var _ Router = &ServiceRouter{}
var _ ServiceDefinition = &service{}

// NewServiceRouter creates a new named Service
func NewServiceRouter(name string, opts ...options.ServiceRouterOption) *ServiceRouter {
o := options.ServiceRouterOptions{}
// NewService creates a new named Service
func NewService(name string, opts ...options.ServiceOption) *service {
o := options.ServiceOptions{}
for _, opt := range opts {
opt.BeforeServiceRouter(&o)
opt.BeforeService(&o)
}
if o.DefaultCodec == nil {
o.DefaultCodec = encoding.JSONCodec
}
return &ServiceRouter{
return &service{
name: name,
handlers: make(map[string]Handler),
options: o,
}
}

// Name returns the name of this Service
func (r *ServiceRouter) Name() string {
func (r *service) Name() string {
return r.name
}

// Handler registers a new Service handler by name
func (r *ServiceRouter) Handler(name string, handler ServiceHandler) *ServiceRouter {
func (r *service) Handler(name string, handler ServiceHandler) *service {
if handler.getOptions().Codec == nil {
handler.getOptions().Codec = r.options.DefaultCodec
}
Expand All @@ -54,47 +54,47 @@ func (r *ServiceRouter) Handler(name string, handler ServiceHandler) *ServiceRou
}

// Handlers returns the list of handlers in this Service
func (r *ServiceRouter) Handlers() map[string]Handler {
func (r *service) Handlers() map[string]Handler {
return r.handlers
}

// Type implements [Router] by returning [internal.ServiceType_SERVICE]
func (r *ServiceRouter) Type() internal.ServiceType {
// Type implements [ServiceDefinition] by returning [internal.ServiceType_SERVICE]
func (r *service) Type() internal.ServiceType {
return internal.ServiceType_SERVICE
}

// ObjectRouter stores a list of handlers under a named Virtual Object
type ObjectRouter struct {
// object stores a list of handlers under a named Virtual Object
type object struct {
name string
handlers map[string]Handler
options options.ObjectRouterOptions
options options.ObjectOptions
}

var _ Router = &ObjectRouter{}
var _ ServiceDefinition = &object{}

// NewObjectRouter creates a new named Virtual Object
func NewObjectRouter(name string, opts ...options.ObjectRouterOption) *ObjectRouter {
o := options.ObjectRouterOptions{}
// NewObject creates a new named Virtual Object
func NewObject(name string, opts ...options.ObjectOption) *object {
o := options.ObjectOptions{}
for _, opt := range opts {
opt.BeforeObjectRouter(&o)
opt.BeforeObject(&o)
}
if o.DefaultCodec == nil {
o.DefaultCodec = encoding.JSONCodec
}
return &ObjectRouter{
return &object{
name: name,
handlers: make(map[string]Handler),
options: o,
}
}

// Name returns the name of this Virtual Object
func (r *ObjectRouter) Name() string {
func (r *object) Name() string {
return r.name
}

// Handler registers a new Virtual Object handler by name
func (r *ObjectRouter) Handler(name string, handler ObjectHandler) *ObjectRouter {
func (r *object) Handler(name string, handler ObjectHandler) *object {
if handler.getOptions().Codec == nil {
handler.getOptions().Codec = r.options.DefaultCodec
}
Expand All @@ -103,11 +103,11 @@ func (r *ObjectRouter) Handler(name string, handler ObjectHandler) *ObjectRouter
}

// Handlers returns the list of handlers in this Virtual Object
func (r *ObjectRouter) Handlers() map[string]Handler {
func (r *object) Handlers() map[string]Handler {
return r.handlers
}

// Type implements [Router] by returning [internal.ServiceType_VIRTUAL_OBJECT]
func (r *ObjectRouter) Type() internal.ServiceType {
// Type implements [ServiceDefinition] by returning [internal.ServiceType_VIRTUAL_OBJECT]
func (r *object) Type() internal.ServiceType {
return internal.ServiceType_VIRTUAL_OBJECT
}
34 changes: 17 additions & 17 deletions server/restate.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type Restate struct {
logHandler slog.Handler
dropReplayLogs bool
systemLog *slog.Logger
routers map[string]restate.Router
definitions map[string]restate.ServiceDefinition
keyIDs []string
keySet identity.KeySetV1
protocolMode internal.ProtocolMode
Expand All @@ -58,7 +58,7 @@ func NewRestate() *Restate {
logHandler: handler,
systemLog: slog.New(log.NewRestateContextHandler(handler)),
dropReplayLogs: true,
routers: make(map[string]restate.Router),
definitions: make(map[string]restate.ServiceDefinition),
protocolMode: internal.ProtocolMode_BIDI_STREAM,
}
}
Expand Down Expand Up @@ -95,15 +95,15 @@ func (r *Restate) Bidirectional(bidi bool) *Restate {
return r
}

// Bind attaches a Router (a Service or Virtual Object) to this server
func (r *Restate) Bind(router restate.Router) *Restate {
if _, ok := r.routers[router.Name()]; ok {
// Bind attaches a Service Definition (a Service or Virtual Object) to this server
func (r *Restate) Bind(definition restate.ServiceDefinition) *Restate {
if _, ok := r.definitions[definition.Name()]; ok {
// panic because this is a programming error
// to register multiple router with the same name
panic("router with the same name exists")
// to register multiple definitions with the same name
panic("service definition with the same name exists")
}

r.routers[router.Name()] = router
r.definitions[definition.Name()] = definition

return r
}
Expand All @@ -113,17 +113,17 @@ func (r *Restate) discover() (resource *internal.Endpoint, err error) {
ProtocolMode: r.protocolMode,
MinProtocolVersion: int32(minServiceProtocolVersion),
MaxProtocolVersion: int32(maxServiceDiscoveryProtocolVersion),
Services: make([]internal.Service, 0, len(r.routers)),
Services: make([]internal.Service, 0, len(r.definitions)),
}

for name, router := range r.routers {
for name, definition := range r.definitions {
service := internal.Service{
Name: name,
Ty: router.Type(),
Handlers: make([]internal.Handler, 0, len(router.Handlers())),
Ty: definition.Type(),
Handlers: make([]internal.Handler, 0, len(definition.Handlers())),
}

for name, handler := range router.Handlers() {
for name, handler := range definition.Handlers() {
service.Handlers = append(service.Handlers, internal.Handler{
Name: name,
Input: handler.InputPayload(),
Expand Down Expand Up @@ -244,13 +244,13 @@ func (r *Restate) callHandler(serviceProtocolVersion protocol.ServiceProtocolVer
writer.Header().Add("x-restate-server", xRestateServer)
writer.Header().Add("content-type", serviceProtocolVersionToHeaderValue(serviceProtocolVersion))

router, ok := r.routers[service]
definition, ok := r.definitions[service]
if !ok {
logger.WarnContext(request.Context(), "Service not found")
writer.WriteHeader(http.StatusNotFound)
return
}
handler, ok := router.Handlers()[method]
handler, ok := definition.Handlers()[method]
if !ok {
logger.WarnContext(request.Context(), "Method not found on service")
writer.WriteHeader(http.StatusNotFound)
Expand Down Expand Up @@ -339,7 +339,7 @@ func (r *Restate) handler(writer http.ResponseWriter, request *http.Request) {
r.callHandler(serviceProtocolVersion, parts[0], parts[1], writer, request)
}

// Handler obtains a [http.HandlerFunc] representing the bound routers which can be passed to other types of server.
// Handler obtains a [http.HandlerFunc] representing the bound services which can be passed to other types of server.
// Ensure that .Bidirectional(false) is set when serving over a channel that doesn't support full-duplex request and response.
func (r *Restate) Handler() (http.HandlerFunc, error) {
if r.keyIDs == nil {
Expand All @@ -356,7 +356,7 @@ func (r *Restate) Handler() (http.HandlerFunc, error) {
return http.HandlerFunc(r.handler), nil
}

// Start starts a HTTP2 server serving the bound routers
// Start starts a HTTP2 server serving the bound services
func (r *Restate) Start(ctx context.Context, address string) error {
handler, err := r.Handler()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions test-services/awakeableholder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
const ID_KEY = "id"

func init() {
REGISTRY.AddRouter(
restate.NewObjectRouter("AwakeableHolder").
REGISTRY.AddDefinition(
restate.NewObject("AwakeableHolder").
Handler("hold", restate.NewObjectHandler(
func(ctx restate.ObjectContext, id string) (restate.Void, error) {
if err := ctx.Set(ID_KEY, id); err != nil {
Expand Down
Loading

0 comments on commit adafb6e

Please sign in to comment.