Class: Beachcomber::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/beachcomber/client.rb

Overview

Session holds a persistent connection to the daemon and sends multiple requests over the same socket.

Obtain a Session via Client#session:

client.session do |s|
  s.set_context('/repo')
  r = s.get('git.branch')
end

Not thread-safe; use one session per thread.

Instance Method Summary collapse

Constructor Details

#initialize(socket, timeout) ⇒ Session

Returns a new instance of Session.



23
24
25
26
# File 'lib/beachcomber/client.rb', line 23

def initialize(socket, timeout)
  @socket  = socket
  @timeout = timeout
end

Instance Method Details

#closeObject

Closes the underlying socket connection.



118
119
120
# File 'lib/beachcomber/client.rb', line 118

def close
  @socket.close unless @socket.closed?
end

#get(key, path: nil) ⇒ Result

Reads a cached value.

Parameters:

  • key (String)

    e.g. “git.branch” or “git”

  • path (String, nil) (defaults to: nil)

    optional working-directory override

Returns:



41
42
43
44
45
# File 'lib/beachcomber/client.rb', line 41

def get(key, path: nil)
  req = { op: 'get', key: key }
  req[:path] = path if path
  roundtrip(req)
end

#get_with_flags(key, path: nil, force: false, wait: false) ⇒ Result

Reads a cached value with protocol flags.

Parameters:

  • key (String)
  • path (String, nil) (defaults to: nil)
  • force (Boolean) (defaults to: false)

    bypass cache and recompute

  • wait (Boolean) (defaults to: false)

    block until a fresh value is available

Returns:



54
55
56
57
58
59
60
# File 'lib/beachcomber/client.rb', line 54

def get_with_flags(key, path: nil, force: false, wait: false)
  req = { op: 'get', key: key }
  req[:path]  = path if path
  req[:force] = true if force
  req[:wait]  = true if wait
  roundtrip(req)
end

#helloHelloInfo

Sends a hello handshake and returns server info.

Returns:



84
85
86
87
# File 'lib/beachcomber/client.rb', line 84

def hello
  resp = roundtrip_raw({ op: 'hello' })
  parse_hello(resp)
end

#introspect(subject, duration_secs: nil) ⇒ IntrospectResponse

Introspects a daemon subsystem.

Parameters:

  • subject (String)

    one of the IntrospectSubject constants

  • duration_secs (Numeric, nil) (defaults to: nil)

Returns:



110
111
112
113
114
115
# File 'lib/beachcomber/client.rb', line 110

def introspect(subject, duration_secs: nil)
  req = { op: 'introspect', subject: subject.to_s }
  req[:duration_secs] = duration_secs if duration_secs
  resp = roundtrip_raw(req)
  parse_introspect(subject.to_s, resp)
end

#put(key, data = nil, ttl: nil, path: nil) ⇒ nil

Writes a value into the daemon cache.

Parameters:

  • key (String)
  • data (Object, nil) (defaults to: nil)
  • ttl (Numeric, nil) (defaults to: nil)

    time-to-live in seconds

  • path (String, nil) (defaults to: nil)

Returns:

  • (nil)


96
97
98
99
100
101
102
103
# File 'lib/beachcomber/client.rb', line 96

def put(key, data = nil, ttl: nil, path: nil)
  req = { op: 'put', key: key }
  req[:data] = data unless data.nil?
  req[:ttl]  = ttl  if ttl
  req[:path] = path if path
  roundtrip(req)
  nil
end

#refresh(key, path: nil) ⇒ Object

Forces the daemon to recompute a provider/key.

Parameters:

  • key (String)
  • path (String, nil) (defaults to: nil)


66
67
68
69
70
71
# File 'lib/beachcomber/client.rb', line 66

def refresh(key, path: nil)
  req = { op: 'refresh', key: key }
  req[:path] = path if path
  roundtrip(req)
  nil
end

#set_context(path) ⇒ Object

Sets the default path for subsequent queries on this connection.

Parameters:

  • path (String)


31
32
33
34
# File 'lib/beachcomber/client.rb', line 31

def set_context(path)
  roundtrip({ op: 'context', path: path })
  nil
end

#statusArray<CacheRow>

Returns cache rows from the daemon.

Returns:



76
77
78
79
# File 'lib/beachcomber/client.rb', line 76

def status
  resp_obj = roundtrip_raw({ op: 'status' })
  parse_cache_rows(resp_obj)
end