Class: Hoodoo::Services::Discovery::ByDRb::DRbServer

Inherits:
Object
  • Object
show all
Defined in:
lib/hoodoo/services/discovery/discoverers/by_drb/drb_server.rb

Overview

A registry of service endpoints, implenented as a DRB server class. An internal implementation detail of Hoodoo::Services::Middleware, in most respects.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDRbServer

Create an instance ready for use as a DRb “front object”.



105
106
107
# File 'lib/hoodoo/services/discovery/discoverers/by_drb/drb_server.rb', line 105

def initialize
  @repository = {}
end

Class Method Details

.start(port = nil) ⇒ Object

Start the DRb server. Does not return (joins the DRb thread). If the server is already running, expect an “address in use” connection exception from DRb.

$SAFE will be set to 1 (unless it is already higher) in this thread.

port

Passed to ::uri method.



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
# File 'lib/hoodoo/services/discovery/discoverers/by_drb/drb_server.rb', line 65

def self.start( port = nil )

  uri = self.uri( port )

  # For security, "disable eval() and friends":
  #
  # http://www.ruby-doc.org/stdlib-2.2.3/libdoc/drb/rdoc/DRb.html
  # https://ruby-hacking-guide.github.io/security.html
  # http://blog.recurity-labs.com/archives/2011/05/12/druby_for_penetration_testers/

  $SAFE = 1

  # Have to allow a tained port string from "outside" just to be able
  # to start the service on a given port; so untaint that deliberately.
  #
  # http://ruby-doc.com/docs/ProgrammingRuby/html/taint.html

  uri.untaint()
  $stop_queue = ::Queue.new

  ::DRb.start_service( uri,
                       FRONT_OBJECT,
                       :tcp_acl => LOCAL_ACL )

  # DRB.thread.exit() does not reliably work; sometimes, it just hangs
  # up. I don't know why. On OS X and under Travis, sporadic failures
  # to return from the "stop()" method would result. Instead, we use a
  # relatively elaborate queue; sit here waiting for a message to be
  # pushed onto it, then just let this method exit naturally, ignoring
  # the value that appeared on the queue.
  #
  # The sleep makes it more reliable too, indicating some kind of nasty
  # race condition on start-vs-wait-to-shutdown.

  sleep( 1 )
  $stop_queue.pop()
end

.uri(port = nil) ⇒ Object

URI for DRb server used during local machine development as a registry of service endpoints. Whichever service starts first runs the server which others connect to if subsequently started.

port

Optional integer port number for DRb service. If specified, this is used; else the HOODOO_DISCOVERY_BY_DRB_PORT_OVERRIDE environment variable is used; else a default of 8787 is chosen. Passing nil explicitly also leads to the use of the environment variable or default value.



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/hoodoo/services/discovery/discoverers/by_drb/drb_server.rb', line 43

def self.uri( port = nil )

  port ||= ENV[ 'HOODOO_DISCOVERY_BY_DRB_PORT_OVERRIDE' ] || 8787

  # Use IP address, rather than 'localhost' here, to ensure that "address
  # in use" errors are raised immediately if a second server startup
  # attempt is made:
  #
  #   https://bugs.ruby-lang.org/issues/3052
  #
  "druby://127.0.0.1:#{ port }"

end

Instance Method Details

#add(resource, version, uri) ⇒ Object

Add an endpoint to the list. If the endpoint was already added, it will be overwritten with the new data.

resource

Resource as a String or Symbol, e.g. “Product”

version

Endpoint’s implemented API version as an Integer, e.g. 1

uri

URI at which this service may be accessed, including the endpoint path (e.g. “localhost:3002/v1/products”), as a String.



124
125
126
# File 'lib/hoodoo/services/discovery/discoverers/by_drb/drb_server.rb', line 124

def add( resource, version, uri )
  @repository[ "#{ resource }/#{ version }" ] = uri
end

#find(resource, version) ⇒ Object

Find an endpoint in the list. Returns URI at which the service may be accessed as a String, or ‘nil’ if not found.

resource

Resource as a String or Symbol, e.g. “Product”

version

Endpoint’s implemented API version as an Integer, e.g. 1



134
135
136
# File 'lib/hoodoo/services/discovery/discoverers/by_drb/drb_server.rb', line 134

def find( resource, version )
  @repository[ "#{ resource }/#{ version }" ]
end

#flushObject

Flush out the repository, clearing all stored service records. This is usually for test purposes only.



141
142
143
# File 'lib/hoodoo/services/discovery/discoverers/by_drb/drb_server.rb', line 141

def flush
  @repository = {}
end

#pingObject

Check to see if this DRb service is awake. Returns true.



111
112
113
# File 'lib/hoodoo/services/discovery/discoverers/by_drb/drb_server.rb', line 111

def ping
  return true
end

#stopObject

Shut down this DRb service.



147
148
149
# File 'lib/hoodoo/services/discovery/discoverers/by_drb/drb_server.rb', line 147

def stop
  $stop_queue.push( true )
end