-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
46 lines (37 loc) · 1.01 KB
/
config.go
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
package rpc
import (
"errors"
"net"
"strings"
"github.com/roadrunner-server/tcplisten"
)
// Config defines RPC service config.
type Config struct {
// Listen - address string
Listen string `mapstructure:"listen"`
}
// InitDefaults allows init blank config with a pre-defined set of default values.
func (c *Config) InitDefaults() {
if c.Listen == "" {
c.Listen = "tcp://127.0.0.1:6001"
}
}
// Valid returns nil if config is valid.
func (c *Config) Valid() error {
if dsn := strings.Split(c.Listen, "://"); len(dsn) != 2 {
return errors.New("invalid socket DSN (tcp://:6001, unix://file.sock)")
}
return nil
}
// Listener creates new rpc socket Listener.
func (c *Config) Listener() (net.Listener, error) {
return tcplisten.CreateListener(c.Listen)
}
// Dialer creates rpc socket Dialer.
func (c *Config) Dialer() (net.Conn, error) {
dsn := strings.Split(c.Listen, "://")
if len(dsn) != 2 {
return nil, errors.New("invalid socket DSN (tcp://:6001, unix://file.sock)")
}
return net.Dial(dsn[0], dsn[1])
}