summaryrefslogtreecommitdiff
path: root/lib/gitlab/gitaly_client.rb
blob: bcdf1b1faa89c40ab4cbf904ed474a38fc84f1a4 (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
require 'gitaly'

module Gitlab
  module GitalyClient
    SERVER_VERSION_FILE = 'GITALY_SERVER_VERSION'.freeze

    # This function is not thread-safe because it updates Hashes in instance variables.
    def self.configure_channels
      @addresses = {}
      @channels = {}
      Gitlab.config.repositories.storages.each do |name, params|
        address = params['gitaly_address']
        unless address.present?
          raise "storage #{name.inspect} is missing a gitaly_address"
        end

        unless URI(address).scheme.in?(%w(tcp unix))
          raise "Unsupported Gitaly address: #{address.inspect}"
        end

        @addresses[name] = address
        @channels[name] = new_channel(address)
      end
    end

    def self.new_channel(address)
      address = address.sub(%r{^tcp://}, '') if URI(address).scheme == 'tcp'
      # NOTE: When Gitaly runs on a Unix socket, permissions are
      # handled using the file system and no additional authentication is
      # required (therefore the :this_channel_is_insecure flag)
      # TODO: Add authentication support when Gitaly is running on a TCP socket.
      GRPC::Core::Channel.new(address, {}, :this_channel_is_insecure)
    end

    def self.get_channel(storage)
      if !Rails.env.production? && @channels.nil?
        # In development mode the Rails auto-loader may reset the instance
        # variables of this class. What we do here is not thread-safe. In normal
        # circumstances (including production) these instance variables have
        # been initialized from config/initializers.
        configure_channels
      end

      @channels[storage]
    end

    def self.get_address(storage)
      if !Rails.env.production? && @addresses.nil?
        # In development mode the Rails auto-loader may reset the instance
        # variables of this class. What we do here is not thread-safe. In normal
        # circumstances (including development) these instance variables have
        # been initialized from config/initializers.
        configure_channels
      end

      @addresses[storage]
    end

    def self.enabled?
      Gitlab.config.gitaly.enabled
    end

    def self.feature_enabled?(feature)
      enabled? && ENV["GITALY_#{feature.upcase}"] == '1'
    end

    def self.migrate(feature)
      is_enabled  = feature_enabled?(feature)
      metric_name = feature.to_s
      metric_name += "_gitaly" if is_enabled

      Gitlab::Metrics.measure(metric_name) do
        yield is_enabled
      end
    end

    def self.expected_server_version
      path = Rails.root.join(SERVER_VERSION_FILE)
      path.read.chomp
    end
  end
end