Welcome to mirror list, hosted at ThFree Co, Russian Federation.

starter.go « starter « bootstrap « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7c43781af05cec5a588a492094f695d8e50d0abf (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
package starter

import (
	"errors"
	"fmt"
	"net"
	"strings"

	"github.com/sirupsen/logrus"
	"gitlab.com/gitlab-org/gitaly/internal/bootstrap"
	"gitlab.com/gitlab-org/gitaly/internal/connectioncounter"
)

const (
	// TCP is the prefix for tcp
	TCP string = "tcp"
	// TLS is the prefix for tls
	TLS string = "tls"
	// Unix is the prefix for unix
	Unix string = "unix"

	separator = "://"
)

var (
	errEmptySchema  = errors.New("empty schema can't be used")
	errEmptyAddress = errors.New("empty address can't be used")
)

// ParseEndpoint returns Config based on the passed in address string.
// Returns error only if provided endpoint has no schema or address defined.
func ParseEndpoint(endpoint string) (Config, error) {
	if endpoint == "" {
		return Config{}, errEmptyAddress
	}

	parts := strings.Split(endpoint, separator)
	if len(parts) != 2 {
		return Config{}, fmt.Errorf("unsupported format: %q", endpoint)
	}

	if err := verifySchema(parts[0]); err != nil {
		return Config{}, err
	}

	if parts[1] == "" {
		return Config{}, errEmptyAddress
	}
	return Config{Name: parts[0], Addr: parts[1]}, nil
}

// ComposeEndpoint returns address string composed from provided schema and schema-less address.
func ComposeEndpoint(schema, address string) (string, error) {
	if address == "" {
		return "", errEmptyAddress
	}

	if err := verifySchema(schema); err != nil {
		return "", err
	}

	return schema + separator + address, nil
}

func verifySchema(schema string) error {
	switch schema {
	case "":
		return errEmptySchema
	case TCP, TLS, Unix:
		return nil
	default:
		return fmt.Errorf("unsupported schema: %q", schema)
	}
}

// Config represents a network type, and address
type Config struct {
	Name, Addr string
}

// Endpoint returns fully qualified address.
func (c *Config) Endpoint() (string, error) {
	return ComposeEndpoint(c.Name, c.Addr)
}

func (c *Config) isSecure() bool {
	return c.Name == TLS
}

func (c *Config) family() string {
	if c.isSecure() {
		return TCP
	}

	return c.Name
}

// New creates a new bootstrap.Starter from a config and a GracefulStoppableServer
func New(cfg Config, servers GracefulStoppableServer) bootstrap.Starter {
	return func(listen bootstrap.ListenFunc, errCh chan<- error) error {
		l, err := listen(cfg.family(), cfg.Addr)
		if err != nil {
			return err
		}

		logrus.WithField("address", cfg.Addr).Infof("listening at %s address", cfg.Name)
		l = connectioncounter.New(cfg.Name, l)

		go func() {
			errCh <- servers.Serve(l, cfg.isSecure())
		}()

		return nil
	}
}

// GracefulStoppableServer allows to serve contents on a net.Listener, Stop serving and performing a GracefulStop
type GracefulStoppableServer interface {
	GracefulStop()
	Stop()
	Serve(l net.Listener, secure bool) error
}