summaryrefslogtreecommitdiff
path: root/internal/rpc/client.go
blob: f371a51cee3133a5b34b7d2daa918864f4752527 (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
// Package prc contains wrapper around RPC client ONLY for internal usage.
package rpc

import (
	"net/rpc"

	"github.com/spiral/errors"
	goridgeRpc "github.com/spiral/goridge/v3/pkg/rpc"
	"github.com/spiral/roadrunner-plugins/v2/config"
	rpcPlugin "github.com/spiral/roadrunner-plugins/v2/rpc"
)

// NewClient creates client ONLY for internal usage (communication between our application with RR side).
// Client will be connected to the RPC.
func NewClient(cfgPlugin *config.Plugin) (*rpc.Client, error) {
	if !cfgPlugin.Has(rpcPlugin.PluginName) {
		return nil, errors.E("rpc service disabled")
	}

	rpcConfig := &rpcPlugin.Config{}
	if err := cfgPlugin.UnmarshalKey(rpcPlugin.PluginName, rpcConfig); err != nil {
		return nil, err
	}

	rpcConfig.InitDefaults()

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

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