summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2018-06-07 17:33:35 +0300
committerWolfy-J <[email protected]>2018-06-07 17:33:35 +0300
commitef78372ec1570571a1af6b1dc9847f7cc72fdc37 (patch)
tree5526cd31961128de24589392eaded8e3ff2089d6
parent1871706eee3aa9e6b534372fbf2817e317f25a92 (diff)
safer rpc serving
-rw-r--r--rpc/service.go16
1 files changed, 13 insertions, 3 deletions
diff --git a/rpc/service.go b/rpc/service.go
index 2dfb04b0..ff5b85c8 100644
--- a/rpc/service.go
+++ b/rpc/service.go
@@ -5,6 +5,7 @@ import (
"github.com/spiral/goridge"
"github.com/spiral/roadrunner/service"
"net/rpc"
+ "sync"
)
// Service is RPC service.
@@ -12,6 +13,9 @@ type Service struct {
cfg *config
stop chan interface{}
rpc *rpc.Server
+
+ mu sync.Mutex
+ serving bool
}
// WithConfig must return Service instance configured with the given environment. Must return error in case of
@@ -35,7 +39,10 @@ func (s *Service) Serve() error {
return errors.New("RPC service is not configured")
}
+ s.mu.Lock()
+ s.serving = true
s.stop = make(chan interface{})
+ s.mu.Unlock()
ln, err := s.cfg.listener()
if err != nil {
@@ -54,7 +61,6 @@ func (s *Service) Serve() error {
}
s.rpc.Accept(ln)
-
go s.rpc.ServeCodec(goridge.NewCodec(conn))
}
}
@@ -64,8 +70,12 @@ func (s *Service) Serve() error {
// Close stop Service Service.
func (s *Service) Stop() {
- //todo: is started?
- close(s.stop)
+ s.mu.Lock()
+ defer s.mu.Unlock()
+
+ if s.serving {
+ close(s.stop)
+ }
}
// Register publishes in the server the set of methods of the