Class: Baykit::BayServer::Docker::SendFile::FileStore

Inherits:
Object
  • Object
show all
Defined in:
lib/baykit/bayserver/docker/send_file/file_store.rb

Defined Under Namespace

Classes: FileContentStatus

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timeout_sec, limit_bytes) ⇒ FileStore



33
34
35
36
37
38
39
# File 'lib/baykit/bayserver/docker/send_file/file_store.rb', line 33

def initialize(timeout_sec, limit_bytes)
  @lifespan_seconds = timeout_sec
  @limit_bytes = limit_bytes
  @total_bytes = 0
  @contents = {}
  @lock = Mutex.new
end

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



27
28
29
# File 'lib/baykit/bayserver/docker/send_file/file_store.rb', line 27

def contents
  @contents
end

#lifespan_secondsObject (readonly)

Returns the value of attribute lifespan_seconds.



30
31
32
# File 'lib/baykit/bayserver/docker/send_file/file_store.rb', line 30

def lifespan_seconds
  @lifespan_seconds
end

#limit_bytesObject (readonly)

Returns the value of attribute limit_bytes.



28
29
30
# File 'lib/baykit/bayserver/docker/send_file/file_store.rb', line 28

def limit_bytes
  @limit_bytes
end

#lockObject (readonly)

Returns the value of attribute lock.



31
32
33
# File 'lib/baykit/bayserver/docker/send_file/file_store.rb', line 31

def lock
  @lock
end

#total_bytesObject (readonly)

Returns the value of attribute total_bytes.



29
30
31
# File 'lib/baykit/bayserver/docker/send_file/file_store.rb', line 29

def total_bytes
  @total_bytes
end

Instance Method Details

#evictObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/baykit/bayserver/docker/send_file/file_store.rb', line 90

def evict()
  evict_list = []
  @contents.each do | path, content |
    if content.is_loaded
      next
    end

    if content.loaded_time + @lifespan_seconds < Time.now.to_i
      # Timed out content
      BayLog.debug("Remove expired content: %s", path)
      @total_bytes -= content.length
      evict_list << path
    else
      break
    end
  end

  evict_list.each do | path |
    @contents.delete(path)
  end

  return !evict_list.empty?
end

#get(path) ⇒ Object



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
# File 'lib/baykit/bayserver/docker/send_file/file_store.rb', line 41

def get(path)
  @lock.synchronize do
    status = 0
    file_content = @contents[path]

    if file_content != nil
      now =  Time.now.to_i

      if file_content.loaded_time + @lifespan_seconds < Time.now.to_i
        @total_bytes -= file_content.content_length
        BayLog.debug("Remove expired content: %s", path)
        @contents.delete(path)
        file_content = nil
      else
        if file_content.is_loaded
          status = FileContentStatus::COMPLETED
        else
          status = FileContentStatus::READING
        end
      end
    end

    if file_content == nil
      length = File.size(path)
      exceeded = false
      if length <= @limit_bytes
        if @total_bytes + length > @limit_bytes
          if !evict()
            exceeded = true
          end
        end
      else
        exceeded = true
      end

      if exceeded
        status = FileContentStatus::EXCEEDED
      else
        file_content = FileContent.new(path, length)
        @contents[path] = file_content
        @total_bytes += length
        status = FileContentStatus::STARTED
      end
    end
    return FileContentStatus.new(file_content, status)

  end
end