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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
package rubyserver
import (
"fmt"
"io/ioutil"
"net"
"os"
"path"
"path/filepath"
"strconv"
"sync"
"time"
"gitlab.com/gitlab-org/gitaly/internal/command"
"gitlab.com/gitlab-org/gitaly/internal/config"
"gitlab.com/gitlab-org/gitaly/internal/supervisor"
"gitlab.com/gitlab-org/gitaly/streamio"
pb "gitlab.com/gitlab-org/gitaly-proto/go"
"github.com/grpc-ecosystem/go-grpc-prometheus"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
var (
socketDir string
lazyInit sync.Once
// ConnectTimeout is the timeout for establishing a connection to the gitaly-ruby process.
ConnectTimeout = 20 * time.Second
)
func init() {
timeout64, err := strconv.ParseInt(os.Getenv("GITALY_RUBY_CONNECT_TIMEOUT"), 10, 32)
if err == nil && timeout64 > 0 {
ConnectTimeout = time.Duration(timeout64) * time.Second
}
}
func prepareSocketPath() {
// The socket path must be short-ish because listen(2) fails on long
// socket paths. We hope/expect that ioutil.TempDir creates a directory
// that is not too deep. We need a directory, not a tempfile, because we
// will later want to set its permissions to 0700. The permission change
// is done in the Ruby child process.
var err error
socketDir, err = ioutil.TempDir("", "gitaly-ruby")
if err != nil {
log.Fatalf("create ruby server socket directory: %v", err)
}
}
func socketPath() string {
if socketDir == "" {
panic("socketDir is not set")
}
return path.Join(filepath.Clean(socketDir), "socket")
}
// Server represents a gitaly-ruby helper process.
type Server struct {
*supervisor.Process
clientConnMu sync.RWMutex
clientConn *grpc.ClientConn
}
// Stop shuts down the gitaly-ruby helper process and cleans up resources.
func (s *Server) Stop() {
if s != nil {
s.clientConnMu.RLock()
defer s.clientConnMu.RUnlock()
if s.clientConn != nil {
s.clientConn.Close()
}
if s.Process != nil {
s.Process.Stop()
}
}
if socketDir != "" {
os.RemoveAll(socketDir)
}
}
// Start spawns the Ruby server.
func Start() (*Server, error) {
lazyInit.Do(prepareSocketPath)
cfg := config.Config
env := []string{
"GITALY_RUBY_GIT_BIN_PATH=" + command.GitPath(),
fmt.Sprintf("GITALY_RUBY_WRITE_BUFFER_SIZE=%d", streamio.WriteBufferSize),
"GITALY_RUBY_GITLAB_SHELL_PATH=" + cfg.GitlabShell.Dir,
"GITALY_RUBY_GITALY_BIN_DIR=" + cfg.BinDir,
}
args := []string{"bundle", "exec", "bin/gitaly-ruby", fmt.Sprintf("%d", os.Getpid()), socketPath()}
p, err := supervisor.New("gitaly-ruby", append(os.Environ(), env...), args, cfg.Ruby.Dir)
return &Server{Process: p}, err
}
// CommitServiceClient returns a CommitServiceClient instance that is
// configured to connect to the running Ruby server. This assumes Start()
// has been called already.
func (s *Server) CommitServiceClient(ctx context.Context) (pb.CommitServiceClient, error) {
conn, err := s.getConnection(ctx)
return pb.NewCommitServiceClient(conn), err
}
// DiffServiceClient returns a DiffServiceClient instance that is
// configured to connect to the running Ruby server. This assumes Start()
// has been called already.
func (s *Server) DiffServiceClient(ctx context.Context) (pb.DiffServiceClient, error) {
conn, err := s.getConnection(ctx)
return pb.NewDiffServiceClient(conn), err
}
// RefServiceClient returns a RefServiceClient instance that is
// configured to connect to the running Ruby server. This assumes Start()
// has been called already.
func (s *Server) RefServiceClient(ctx context.Context) (pb.RefServiceClient, error) {
conn, err := s.getConnection(ctx)
return pb.NewRefServiceClient(conn), err
}
// OperationServiceClient returns a OperationServiceClient instance that is
// configured to connect to the running Ruby server. This assumes Start()
// has been called already.
func (s *Server) OperationServiceClient(ctx context.Context) (pb.OperationServiceClient, error) {
conn, err := s.getConnection(ctx)
return pb.NewOperationServiceClient(conn), err
}
// RepositoryServiceClient returns a RefServiceClient instance that is
// configured to connect to the running Ruby server. This assumes Start()
// has been called already.
func (s *Server) RepositoryServiceClient(ctx context.Context) (pb.RepositoryServiceClient, error) {
conn, err := s.getConnection(ctx)
return pb.NewRepositoryServiceClient(conn), err
}
// WikiServiceClient returns a WikiServiceClient instance that is
// configured to connect to the running Ruby server. This assumes Start()
// has been called already.
func (s *Server) WikiServiceClient(ctx context.Context) (pb.WikiServiceClient, error) {
conn, err := s.getConnection(ctx)
return pb.NewWikiServiceClient(conn), err
}
func (s *Server) getConnection(ctx context.Context) (*grpc.ClientConn, error) {
s.clientConnMu.RLock()
conn := s.clientConn
s.clientConnMu.RUnlock()
if conn != nil {
return conn, nil
}
return s.createConnection(ctx)
}
func (s *Server) createConnection(ctx context.Context) (*grpc.ClientConn, error) {
s.clientConnMu.Lock()
defer s.clientConnMu.Unlock()
if conn := s.clientConn; conn != nil {
return conn, nil
}
dialCtx, cancel := context.WithTimeout(ctx, ConnectTimeout)
defer cancel()
conn, err := grpc.DialContext(dialCtx, socketPath(), dialOptions()...)
if err != nil {
return nil, err
}
s.clientConn = conn
return s.clientConn, nil
}
func dialOptions() []grpc.DialOption {
return []grpc.DialOption{
grpc.WithBlock(), // With this we get retries. Without, connections fail fast.
grpc.WithInsecure(),
grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
return net.DialTimeout("unix", addr, timeout)
}),
grpc.WithUnaryInterceptor(grpc_prometheus.UnaryClientInterceptor),
grpc.WithStreamInterceptor(grpc_prometheus.StreamClientInterceptor),
}
}
|