summaryrefslogtreecommitdiff
path: root/plugins/rpc/rpc.go
blob: 6568eea3df7223d256a00eeb78f659c80acf05b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package rpc

import (
	"errors"

	"github.com/spiral/goridge/v2"
	"github.com/spiral/roadrunner/v2/plugins/config"

	"net/rpc"
)

type PluginRpc interface {
	Name() string
	RpcService() (interface{}, error)
}

// ID contains default service name.
const ID = "rpc"

type services struct {
	service interface{}
	name    string
}

// Service is RPC service.
type Service struct {
	// TODO do we need a pointer here since all receivers are pointers??
	rpc            *rpc.Server
	configProvider config.Provider
	services       []services
	config         Config
	close          chan struct{}
}

// Init rpc service. Must return true if service is enabled.
func (s *Service) Init(cfg config.Provider) error {
	s.configProvider = cfg
	err := s.configProvider.UnmarshalKey(ID, &s.config)
	if err != nil {
		return err
	}

	// TODO Do we need to init defaults
	if s.config.Listen == "" {
		s.config.InitDefaults()
	}

	s.close = make(chan struct{})

	return nil
}

// Serve serves the service.
func (s *Service) Serve() chan error {
	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 {
		errCh <- errors.New("no services with RPC")
		return errCh
	}

	// 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
			return errCh
		}
	}

	ln, err := s.config.Listener()
	if err != nil {
		errCh <- err
		return errCh
	}

	go func() {
		for {
			select {
			case <-s.close:
				// log error
				errCh <- ln.Close()
				return
			default:
				conn, err := ln.Accept()
				if err != nil {
					continue
				}

				go s.rpc.ServeCodec(goridge.NewCodec(conn))
			}
		}
	}()

	return nil
}

// Stop stops the service.
func (s *Service) Stop() error {
	s.close <- struct{}{}
	return nil
}

func (s *Service) Depends() []interface{} {
	return []interface{}{
		s.RpcService,
	}
}

func (s *Service) RpcService(p PluginRpc) error {
	service, err := p.RpcService()
	if err != nil {
		return err
	}

	s.services = append(s.services, services{
		service: service,
		name:    p.Name(),
	})
	return nil
}

// Register publishes in the server the set of methods of the
// receiver value that satisfy the following conditions:
//	- exported method of exported type
//	- two arguments, both of exported type
//	- the second argument is a pointer
//	- one return value, of type error
// It returns an error if the receiver is not an exported type or has
// 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 s.rpc.RegisterName(name, svc)
}

// Client creates new RPC client.
func (s *Service) Client() (*rpc.Client, error) {
	if s.configProvider == nil {
		return nil, errors.New("RPC service is not configured")
	}

	conn, err := s.config.Dialer()
	if err != nil {
		return nil, err
	}

	return rpc.NewClientWithCodec(goridge.NewClientCodec(conn)), nil
}