mperham/dalli

View on GitHub
lib/dalli/server.rb

Summary

Maintainability
F
4 days
Test Coverage

File server.rb has 600 lines of code (exceeds 250 allowed). Consider refactoring.
Open

require 'socket'
require 'timeout'

module Dalli
  class Server
Severity: Major
Found in lib/dalli/server.rb - About 1 day to fix

    Class Server has 59 methods (exceeds 20 allowed). Consider refactoring.
    Open

      class Server
        attr_accessor :hostname
        attr_accessor :port
        attr_accessor :weight
        attr_accessor :options
    Severity: Major
    Found in lib/dalli/server.rb - About 1 day to fix

      Method multi_response_nonblock has a Cognitive Complexity of 20 (exceeds 5 allowed). Consider refactoring.
      Open

          def multi_response_nonblock
            raise 'multi_response has completed' if @multi_buffer.nil?
      
            @multi_buffer << @sock.read_available
            buf = @multi_buffer
      Severity: Minor
      Found in lib/dalli/server.rb - About 2 hrs to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Method generic_response has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
      Open

          def generic_response(unpack=false, cache_nils=false)
            (extras, _, status, count) = read_header.unpack(NORMAL_HEADER)
            data = read(count) if count > 0
            if status == 1
              cache_nils ? NOT_FOUND : nil
      Severity: Minor
      Found in lib/dalli/server.rb - About 1 hr to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Method serialize has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
      Open

          def serialize(key, value, options=nil)
            marshalled = false
            value = unless options && options[:raw]
              marshalled = true
              begin
      Severity: Minor
      Found in lib/dalli/server.rb - About 1 hr to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Method deserialize has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
      Open

          def deserialize(value, flags)
            value = self.compressor.decompress(value) if (flags & FLAG_COMPRESSED) != 0
            value = self.serializer.load(value) if (flags & FLAG_SERIALIZED) != 0
            value
          rescue TypeError
      Severity: Minor
      Found in lib/dalli/server.rb - About 1 hr to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Method multi_response_nonblock has 30 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

          def multi_response_nonblock
            raise 'multi_response has completed' if @multi_buffer.nil?
      
            @multi_buffer << @sock.read_available
            buf = @multi_buffer
      Severity: Minor
      Found in lib/dalli/server.rb - About 1 hr to fix

        Method sasl_authentication has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
        Open

            def sasl_authentication
              Dalli.logger.info { "Dalli/SASL authenticating as #{username}" }
        
              # negotiate
              req = [REQUEST, OPCODES[:auth_negotiation], 0, 0, 0, 0, 0, 0, 0].pack(FORMAT[:noop])
        Severity: Minor
        Found in lib/dalli/server.rb - About 1 hr to fix

        Cognitive Complexity

        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

        A method's cognitive complexity is based on a few simple rules:

        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
        • Code is considered more complex for each "break in the linear flow of the code"
        • Code is considered more complex when "flow breaking structures are nested"

        Further reading

        Method serialize has 26 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

            def serialize(key, value, options=nil)
              marshalled = false
              value = unless options && options[:raw]
                marshalled = true
                begin
        Severity: Minor
        Found in lib/dalli/server.rb - About 1 hr to fix

          Method decr_incr has 5 arguments (exceeds 4 allowed). Consider refactoring.
          Open

              def decr_incr(opcode, key, count, ttl, default)
          Severity: Minor
          Found in lib/dalli/server.rb - About 35 mins to fix

            Method set has 5 arguments (exceeds 4 allowed). Consider refactoring.
            Open

                def set(key, value, ttl, cas, options)
            Severity: Minor
            Found in lib/dalli/server.rb - About 35 mins to fix

              Method replace has 5 arguments (exceeds 4 allowed). Consider refactoring.
              Open

                  def replace(key, value, ttl, cas, options)
              Severity: Minor
              Found in lib/dalli/server.rb - About 35 mins to fix

                Method parse_hostname has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                Open

                    def parse_hostname(str)
                      res = str.match(/\A(\[([\h:]+)\]|[^:]+)(?::(\d+))?(?::(\d+))?\z/)
                      raise Dalli::DalliError, "Could not parse hostname #{str}" if res.nil? || res[1] == '[]'
                      hostnam = res[2] || res[1]
                      if hostnam =~ /\A\//
                Severity: Minor
                Found in lib/dalli/server.rb - About 35 mins to fix

                Cognitive Complexity

                Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                A method's cognitive complexity is based on a few simple rules:

                • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                • Code is considered more complex for each "break in the linear flow of the code"
                • Code is considered more complex when "flow breaking structures are nested"

                Further reading

                Method cas_response has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                Open

                    def cas_response
                      (_, _, status, count, _, cas) = read_header.unpack(CAS_HEADER)
                      read(count) if count > 0  # this is potential data that we don't care about
                      if status == 1
                        nil
                Severity: Minor
                Found in lib/dalli/server.rb - About 25 mins to fix

                Cognitive Complexity

                Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                A method's cognitive complexity is based on a few simple rules:

                • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                • Code is considered more complex for each "break in the linear flow of the code"
                • Code is considered more complex when "flow breaking structures are nested"

                Further reading

                Method set has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                Open

                    def set(key, value, ttl, cas, options)
                      (value, flags) = serialize(key, value, options)
                      ttl = sanitize_ttl(ttl)
                
                      guard_max_value(key, value) do
                Severity: Minor
                Found in lib/dalli/server.rb - About 25 mins to fix

                Cognitive Complexity

                Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                A method's cognitive complexity is based on a few simple rules:

                • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                • Code is considered more complex for each "break in the linear flow of the code"
                • Code is considered more complex when "flow breaking structures are nested"

                Further reading

                Method replace has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                Open

                    def replace(key, value, ttl, cas, options)
                      (value, flags) = serialize(key, value, options)
                      ttl = sanitize_ttl(ttl)
                
                      guard_max_value(key, value) do
                Severity: Minor
                Found in lib/dalli/server.rb - About 25 mins to fix

                Cognitive Complexity

                Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                A method's cognitive complexity is based on a few simple rules:

                • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                • Code is considered more complex for each "break in the linear flow of the code"
                • Code is considered more complex when "flow breaking structures are nested"

                Further reading

                Method add has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                Open

                    def add(key, value, ttl, options)
                      (value, flags) = serialize(key, value, options)
                      ttl = sanitize_ttl(ttl)
                
                      guard_max_value(key, value) do
                Severity: Minor
                Found in lib/dalli/server.rb - About 25 mins to fix

                Cognitive Complexity

                Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                A method's cognitive complexity is based on a few simple rules:

                • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                • Code is considered more complex for each "break in the linear flow of the code"
                • Code is considered more complex when "flow breaking structures are nested"

                Further reading

                Method alive? has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                Open

                    def alive?
                      return true if @sock
                
                      if @last_down_at && @last_down_at + options[:down_retry_delay] >= Time.now
                        time = @last_down_at + options[:down_retry_delay] - Time.now
                Severity: Minor
                Found in lib/dalli/server.rb - About 25 mins to fix

                Cognitive Complexity

                Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                A method's cognitive complexity is based on a few simple rules:

                • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                • Code is considered more complex for each "break in the linear flow of the code"
                • Code is considered more complex when "flow breaking structures are nested"

                Further reading

                Similar blocks of code found in 2 locations. Consider refactoring.
                Open

                    def replace(key, value, ttl, cas, options)
                      (value, flags) = serialize(key, value, options)
                      ttl = sanitize_ttl(ttl)
                
                      guard_max_value(key, value) do
                Severity: Major
                Found in lib/dalli/server.rb and 1 other location - About 1 hr to fix
                lib/dalli/server.rb on lines 284..292

                Duplicated Code

                Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                Tuning

                This issue has a mass of 55.

                We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                Refactorings

                Further Reading

                Similar blocks of code found in 2 locations. Consider refactoring.
                Open

                    def set(key, value, ttl, cas, options)
                      (value, flags) = serialize(key, value, options)
                      ttl = sanitize_ttl(ttl)
                
                      guard_max_value(key, value) do
                Severity: Major
                Found in lib/dalli/server.rb and 1 other location - About 1 hr to fix
                lib/dalli/server.rb on lines 306..314

                Duplicated Code

                Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                Tuning

                This issue has a mass of 55.

                We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                Refactorings

                Further Reading

                Similar blocks of code found in 2 locations. Consider refactoring.
                Open

                    RESPONSE_CODES = {
                      0 => 'No error',
                      1 => 'Key not found',
                      2 => 'Key exists',
                      3 => 'Value too large',
                Severity: Minor
                Found in lib/dalli/server.rb and 1 other location - About 35 mins to fix
                lib/dalli/server.rb on lines 663..680

                Duplicated Code

                Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                Tuning

                This issue has a mass of 36.

                We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                Refactorings

                Further Reading

                Similar blocks of code found in 2 locations. Consider refactoring.
                Open

                    OP_FORMAT = {
                      :get => 'a*',
                      :set => 'NNa*a*',
                      :add => 'NNa*a*',
                      :replace => 'NNa*a*',
                Severity: Minor
                Found in lib/dalli/server.rb and 1 other location - About 35 mins to fix
                lib/dalli/server.rb on lines 615..632

                Duplicated Code

                Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                Tuning

                This issue has a mass of 36.

                We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                Refactorings

                Further Reading

                There are no issues that match your filters.

                Category
                Status