summaryrefslogtreecommitdiff
path: root/plugins/rpc
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2020-10-25 15:55:51 +0300
committerWolfy-J <[email protected]>2020-10-25 15:55:51 +0300
commitba5c562f9038ba434e655fb82c44597fcccaff16 (patch)
treeff112b9dcffda63bc40094a57d0df61622368445 /plugins/rpc
parent3bdf7d02d83d1ff4726f3fbb01a45d016f39abec (diff)
- massive update in roadrunner 2.0 abstractions
Diffstat (limited to 'plugins/rpc')
-rw-r--r--plugins/rpc/config.go3
-rw-r--r--plugins/rpc/rpc.go36
2 files changed, 17 insertions, 22 deletions
diff --git a/plugins/rpc/config.go b/plugins/rpc/config.go
index 1039ee5e..719fd5e3 100644
--- a/plugins/rpc/config.go
+++ b/plugins/rpc/config.go
@@ -12,6 +12,9 @@ import (
type Config struct {
// Listen string
Listen string
+
+ // Disabled disables RPC service.
+ Disabled bool
}
// InitDefaults allows to init blank config with pre-defined set of default values.
diff --git a/plugins/rpc/rpc.go b/plugins/rpc/rpc.go
index ef8e82e4..7b47682f 100644
--- a/plugins/rpc/rpc.go
+++ b/plugins/rpc/rpc.go
@@ -1,8 +1,7 @@
package rpc
import (
- "errors"
-
+ "github.com/spiral/endure/errors"
"github.com/spiral/goridge/v2"
"github.com/spiral/roadrunner/v2/plugins/config"
@@ -14,8 +13,8 @@ type RPCService interface {
RPCService() (interface{}, error)
}
-// ID contains default service name.
-const ID = "rpc"
+// ServiceName contains default service name.
+const ServiceName = "rpc"
type services struct {
service interface{}
@@ -32,14 +31,19 @@ type Service struct {
// Init rpc service. Must return true if service is enabled.
func (s *Service) Init(cfg config.Provider) error {
- err := cfg.UnmarshalKey(ID, &s.config)
+ if !cfg.Has(ServiceName) {
+ return errors.E(errors.Disabled)
+ }
+
+ err := cfg.UnmarshalKey(ServiceName, &s.config)
if err != nil {
return err
}
-
s.config.InitDefaults()
- // todo: handle disabled
+ if s.config.Disabled {
+ return errors.E(errors.Disabled)
+ }
return s.config.Valid()
}
@@ -47,27 +51,15 @@ func (s *Service) Init(cfg config.Provider) error {
// Serve serves the service.
func (s *Service) Serve() chan error {
s.close = make(chan struct{})
-
errCh := make(chan error, 1)
- server := rpc.NewServer()
- if server == nil {
- errCh <- errors.New("rpc server is nil")
- return errCh
- }
- s.rpc = server
-
- if len(s.services) == 0 {
- // todo: why this is an error?
- errCh <- errors.New("no services with RPC")
- return errCh
- }
+ s.rpc = rpc.NewServer()
// Attach all services
for i := 0; i < len(s.services); i++ {
err := s.Register(s.services[i].name, s.services[i].service)
if err != nil {
- errCh <- err
+ errCh <- errors.E(errors.Op("register service"), err)
return errCh
}
}
@@ -134,7 +126,7 @@ func (s *Service) RegisterService(p RPCService) error {
// no suitable methods. It also logs the error using package log.
func (s *Service) Register(name string, svc interface{}) error {
if s.rpc == nil {
- return errors.New("RPC service is not configured")
+ return errors.E("RPC service is not configured")
}
return s.rpc.RegisterName(name, svc)