Difference between revisions of "Nsproxy"

From AOLserver Wiki
Jump to navigation Jump to search
 
(13 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
'''NAME'''
 
'''NAME'''
  
: ns_proxy - Set connection to streaming state for streaming content via ns_write
+
: ns_proxy - Execute Tcl scripts in an external process (aolserver module)
  
 
'''SYNOPSIS'''
 
'''SYNOPSIS'''
  
: '''ns_proxy''' ''option ?arg arg ...?''
+
: '''ns_proxy active''' ''pool''
 +
: '''ns_proxy cleanup'''
 +
: '''ns_proxy config''' ''pool ?-opt val -opt val ...''
 +
: '''ns_proxy eval''' ''handle script ?timeout?''
 +
: '''ns_proxy get''' ''pool ?-handle n -timeout ms?''
 +
: '''ns_proxy ping''' ''handle''
 +
: '''ns_proxy release''' ''handle''
 +
: '''ns_proxy recv''' ''handle''
 +
: '''ns_proxy send''' ''handle script''
 +
: '''ns_proxy wait''' ''handle ?timeout?''
  
 
'''DESCRIPTION'''
 
'''DESCRIPTION'''
Line 13: Line 22:
 
: The legal ''option''s are:
 
: The legal ''option''s are:
  
* '''ns_proxy get''' ''poolname'' ''?'' '''-handles''' ''value?'' ''?'' '''-timeout''' ''value?''  
+
* '''ns_proxy active''' ''pool''
 +
 
 +
: Returns a list of all currently evaluating scripts in proxies for the given pool. The output is a list which includes two elements, the string name of the proxy handle and the string for the script being executed. It is also possible to view the currently evaluating scripts with the Unix ps command as the proxy slave process re-writes it's command argument space with the request script before evaluation and clears it after sending the result.
 +
 
 +
* '''ns_proxy cleanup'''
  
: Returns one or more handles to proxies from the specified pool. The pool will be created with default options if it does not already exist. The option ''-handle'' can be used to specify the number of handles allocated (default: 1). The option ''timeout'' specifies the maximum amount of time in milliseconds to wait for the handles to become available before raising an error. Requesting more than one handle in a single call (if more than one handle is required) is necessary as it is an error to request handles from a pool from which handles are already owned by the thread. This restriction is implemented to avoid possible deadlock conditions. The handle returned by this command can be used as a scalar value for other ''ns_proxy'' commands, or it can be used as Tcl command itself (see ns_proxy eval for more information).
+
: Releases any handles from any pools currently owned by a thread. This command is intended to be used as part of a garbage collection step at the end of a transaction. Calling this command within AOLserver is not necessary as the module registers a trace to release all handles via the Ns_TclRegisterTrace facility when interpreters are deallocated after a transaction, for example, at the end of a connection.  
  
* '''ns_proxy release''' ''handle''
+
* '''ns_proxy config''' ''pool'' ''?'' '''-init''' ''script? ?'' '''-reinit''' ''script? ?'' '''-min''' ''value? ?'' '''-max''' ''value? ?'' '''-exec''' ''program? ?'' '''-gettimeout''' ''value? ?'' '''-evaltimeout''' ''value? ?'' '''-sendtimeout''' ''value? ?'' '''-recvtimeout''' ''value? ?'' '''-waittimeout''' ''value?
  
: Release a single handle. All handles owned by a thread must be returned before any handles can be allocated again.
+
: Configure the specified pool or obtain current configuration values. When '''ns_proxy config''' is called without options, a list of the current options in the form ''-opt val ...'' is returned. '''init''' can be used to specify a script to be evaluated, when the proxy (slave) is started. This can be used to load additional libraries and/or source script files. The default is no script. '''reinit''' Specifies a script to evaluate after being allocated and before being returned to the caller. This can be used to re-initalizes shared state. The default is no script. '''min''' and '''max''' are the minimum and maximum number of slaves ('''min''' defaults to 0, '''max''' 0 means to disable this pool).  '''exec''' names the program (filename) to be executed. This defaults to '''nsproxy''' in the '''bin''' subdirectory of the AOLserver process. It is possible to create a custom program and enter the proxy event loop with the Ns_ProxyMain application startup routine; see the source code for details. '''gettimeout''' specifies the maximum time in ms to wait to allocate handles from the pool (default 500). '''evaltimeout''' specifies the maximum time in ms to wait for a script to be evaluated in a proxy (default 100). This parameter can be overridden on a per-call basis with the optional ''timeout'' parameter to '''ns_proxy eval'''. The default is 0 milliseconds i.e. infinite. '''sendtimeout''' and '''recvtimeout''' specify the maximum time to wait to send a script and receive a result from a proxy (default 100 milliseconds). '''waittimeout''' specifies  the maximum time to wait for a proxy to exit. The wait is performed in a dedicated reaper thread. The reaper will close the connection pipe and wait the given timeout. If the timeout is exceeded, the reaper will send a SIGTERM signal and finally a SIGKILL signal to ensure the process eventually exits. The default is 100 milliseconds which should be ample time for a graceful exit unless the process is hung executing a very long, misbehaving script, resulting in a more disruptive SIGTERM or SIGKILL.  
  
 
* '''ns_proxy eval''' ''handle script ?timeout?''
 
* '''ns_proxy eval''' ''handle script ?timeout?''
  
: Evalutes script in the proxy specified by handle. The optional ''timeout'' argument specifies a maximum number of milliseconds to wait for the command to complete before raising an error.
+
: Evalutes script in the proxy specified by handle. The optional ''timeout'' argument specifies a maximum number of milliseconds to wait for the command to complete before raising an error (see ERROR HANDLING below for details on handling errors).
  
* '''ns_proxy cleanup'''
+
* '''ns_proxy get''' ''pool'' ''?'' '''-handles''' ''value?'' ''?'' '''-timeout''' ''value?''  
  
: Releases any handles from any pools currently owned by a thread. This command is intended to be used as part of a garbage collection step.
+
: Returns one or more handles to proxies from the specified pool. The pool will be created with default options if it does not already exist. The option ''-handle'' can be used to specify the number of handles allocated (default: 1). The option ''timeout'' specifies the maximum amount of time in milliseconds to wait for the handles to become available before raising an error. Requesting more than one handle in a single call (if more than one handle is required) is necessary as it is an error to request handles from a pool from which handles are already owned by the thread. This restriction is implemented to avoid possible deadlock conditions. The handle returned by this command can be used as a scalar value for other '''ns_proxy''' commands, or it can be used as Tcl command itself (see '''ns_proxy eval''' for more information).
  
* '''ns_proxy config''' ''poolname'' ''?'' '''-init''' ''script? ?'' '''-reinit''' ''script? ?'' '''-min''' ''value? ?'' '''-max''' ''value? ?'' '''-exec''' ''program? ?'' '''-gettimeout''' ''value? ?'' '''-evaltimeout''' ''value? ?'' '''-sendtimeout''' ''value? ?'' '''-recvtimeout''' ''value? ?'' '''-waittimeout''' ''value?
+
* '''ns_proxy ping''' ''handle''
  
: Configure the specified pool. ''init'' can be used to specify a script to be evaluated, when the proxy (slave) is started. ''reinit'' specifies the script to be evaluated when the slave is re-initialized. ''min'' and ''max'' are the minimum and maximum number of slaves.  ''exec'' names the program to be executed (defaults to ''nsproxy''). ''gettimeout'' specifies the maximum time in ms to wait to allocate handles from the pool (default 5000). ''evaltimeout'' specifies the maximum time in ms to wait for a script to be evaluated in a proxy. This parameter can be overridden on a per-call basis with the optional ''timeout'' parameter to '''ns_proxy eval'''. The default is 0 milliseconds i.e. infinite. ''sendtimeout'' and ''recvtimeout' specify the maximum time to wait to send a script and receive a result from a proxy. ''waittimeout'' specifies the maximum time to wait for a proxy to exit.
+
: Check if ''handle'' is alive by sending an empty request. This command is not normally required as the '''ns_proxy eval''' command will also verify and restart proxies as needed.  
  
* '''ns_proxy ping''' ''handle''
+
* '''ns_proxy recv'''
  
: Check if ''handle'' is alive by sending an empty request.
+
: Receives a response from a script that was sent via '''ns_proxy send''' and waited on via '''ns_proxy wait'''.  
  
* '''ns_proxy active''' ''pool''
+
* '''ns_proxy release''' ''handle''
  
: Returns a list of all currently evaluating scripts in proxies for the given pool. The output is a list which includes two elements, the string name of the proxy handle and the string for the script being executed. It is also possible to view the currently evaluating scripts with the Unix ps command as the proxy slave process re-writes it's command argument space with the request script before evaluation and clears it after sending the result.  
+
: Release a single handle. All handles owned by a thread to the corresponding pool must be returned before any handles can be allocated again. Within AOLserver, a call to this routine is recommended for clarity but not stricting necessary. As described above, AOLserver installs a trace to release all handles at the end of every connection during interpreter deallocation.  
  
 
* '''ns_proxy send''' ''handle script''
 
* '''ns_proxy send''' ''handle script''
  
: Sends ''script'' in the proxy specified by handle. Unlike with '''ns_proxy eval''', this option will return immediately while the script continues to execute in the proxy process. A later ns_proxy wait followed by an '''ns_proxy recv''' is expected.  
+
: Sends ''script'' in the proxy specified by handle. Unlike with '''ns_proxy eval''', this option will return immediately while the script continues to execute in the proxy process. A later '''ns_proxy wait''' followed by an '''ns_proxy recv''' is expected.  
  
 
* '''ns_proxy wait''' ''handle ?timeout?''
 
* '''ns_proxy wait''' ''handle ?timeout?''
Line 49: Line 62:
 
: Waits for a script sent via '''ns_proxy send''' in the proxy specified by the ''handle'' argument to complete. The optional ''timeout'' parameter specifies the number of milliseconds to wait for the script to complete, the default is an indefinite wait.  
 
: Waits for a script sent via '''ns_proxy send''' in the proxy specified by the ''handle'' argument to complete. The optional ''timeout'' parameter specifies the number of milliseconds to wait for the script to complete, the default is an indefinite wait.  
  
* '''ns_proxy recv'''
+
 +
'''ERROR HANDLING'''
 +
 
 +
: Errors generated by a script evaluated in a proxy interpreter are completely returned to the calling interpreter, including mapping the errorInfo and errorInfo global variables from the proxy to the parent if present and raising a Tcl exception. This enables proxy code to look very similar to that which may use the internal eval command.
 +
 
 +
: Errors raised by a failure to communicate with the proxy process due to a timeout or unexpected process exit are also communicated back to the parent interpreter as Tcl exceptions. To distinguish between these cases, communication related errors set the errorCode global variable with the first element NSPROXY. The second element is one of the following:
 +
 
 +
:'''EDeadlock'''
 +
:: The interpreter attempted to allocate handles from a pool from which it already owns one or more handles.
 +
 
 +
:'''EExec'''
 +
:: The slave program specified by the '''-exec''' ''program'' option could not be started.
 +
 
 +
:'''ERange'''
 +
:: Insufficient handles available in pool.
 +
 
 +
:'''ERecv'''
 +
:: There was an error receiving the result from the slave process.
 +
 
 +
:'''ESend'''
 +
:: There was an error sending the script to the slave process.
  
: Receives a response from a script that was sent via '''ns_proxy send''' and waited on via '''ns_proxy wait'''.  
+
:'''ETimeout'''
 +
:: The timeout specified for the pool by the '''-evaltimeout''' option or as the optional argument to the current call to ns_proxy eval was exceeded.  
  
 
'''EXAMPLES'''
 
'''EXAMPLES'''
  
: ns_proxy get default
+
The following demonstrates sending a script to a remote proxy:
 +
 
 +
        set handle [ns_proxy get myproxy]
 +
        ns_proxy eval $handle {info patchlevel}
 +
        ns_proxy release $handle
 +
 
 +
The following demonstrates an asyncronous request:
 +
 
 +
        set handle [ns_proxy get myproxy]
 +
        ns_proxy send $handle {long running script}
 +
        ... continue other work ...
 +
        ns_proxy wait $handle
 +
        set result [ns_proxy recv $handle]
 +
        ns_proxy release $handle
 +
 
 +
The following demonstrates using multiple proxies:
 +
 
 +
        ns_proxy config myproxy -max 10
 +
        set handles [ns_proxy get myproxy -handle 10]
 +
        foreach h $handles {
 +
                ns_proxy eval $h {puts "alive: [pid]"}
 +
        }
 +
        ns_proxy cleanup
  
 
'''SEE ALSO'''
 
'''SEE ALSO'''
  
: [[ns_limits]]
+
: [[ns_eval]]
 +
 
 +
[[Category:Documentation]] - [[Category:Core Tcl API]]

Latest revision as of 19:03, 7 February 2009

NAME

ns_proxy - Execute Tcl scripts in an external process (aolserver module)

SYNOPSIS

ns_proxy active pool
ns_proxy cleanup
ns_proxy config pool ?-opt val -opt val ...
ns_proxy eval handle script ?timeout?
ns_proxy get pool ?-handle n -timeout ms?
ns_proxy ping handle
ns_proxy release handle
ns_proxy recv handle
ns_proxy send handle script
ns_proxy wait handle ?timeout?

DESCRIPTION

This command provides a simple, robust proxy mechanism to evaluate Tcl scripts in a separate, pipe-connected process. This approach can be useful both to isolate potentially thread-unsafe code outside the address space of a aolserver or to enable separation and timeout of potentially misbehaving, long running scripts. The proxy includes core Tcl commands as well as AOLserver commands from the libnsd library. Evaluating scripts in a proxy process can be used to isolate and/or timeout thread-unsafe or otherwise unsafe 3rd party code.
The legal options are:
  • ns_proxy active pool
Returns a list of all currently evaluating scripts in proxies for the given pool. The output is a list which includes two elements, the string name of the proxy handle and the string for the script being executed. It is also possible to view the currently evaluating scripts with the Unix ps command as the proxy slave process re-writes it's command argument space with the request script before evaluation and clears it after sending the result.
  • ns_proxy cleanup
Releases any handles from any pools currently owned by a thread. This command is intended to be used as part of a garbage collection step at the end of a transaction. Calling this command within AOLserver is not necessary as the module registers a trace to release all handles via the Ns_TclRegisterTrace facility when interpreters are deallocated after a transaction, for example, at the end of a connection.
  • ns_proxy config pool ? -init script? ? -reinit script? ? -min value? ? -max value? ? -exec program? ? -gettimeout value? ? -evaltimeout value? ? -sendtimeout value? ? -recvtimeout value? ? -waittimeout value?
Configure the specified pool or obtain current configuration values. When ns_proxy config is called without options, a list of the current options in the form -opt val ... is returned. init can be used to specify a script to be evaluated, when the proxy (slave) is started. This can be used to load additional libraries and/or source script files. The default is no script. reinit Specifies a script to evaluate after being allocated and before being returned to the caller. This can be used to re-initalizes shared state. The default is no script. min and max are the minimum and maximum number of slaves (min defaults to 0, max 0 means to disable this pool). exec names the program (filename) to be executed. This defaults to nsproxy in the bin subdirectory of the AOLserver process. It is possible to create a custom program and enter the proxy event loop with the Ns_ProxyMain application startup routine; see the source code for details. gettimeout specifies the maximum time in ms to wait to allocate handles from the pool (default 500). evaltimeout specifies the maximum time in ms to wait for a script to be evaluated in a proxy (default 100). This parameter can be overridden on a per-call basis with the optional timeout parameter to ns_proxy eval. The default is 0 milliseconds i.e. infinite. sendtimeout and recvtimeout specify the maximum time to wait to send a script and receive a result from a proxy (default 100 milliseconds). waittimeout specifies the maximum time to wait for a proxy to exit. The wait is performed in a dedicated reaper thread. The reaper will close the connection pipe and wait the given timeout. If the timeout is exceeded, the reaper will send a SIGTERM signal and finally a SIGKILL signal to ensure the process eventually exits. The default is 100 milliseconds which should be ample time for a graceful exit unless the process is hung executing a very long, misbehaving script, resulting in a more disruptive SIGTERM or SIGKILL.
  • ns_proxy eval handle script ?timeout?
Evalutes script in the proxy specified by handle. The optional timeout argument specifies a maximum number of milliseconds to wait for the command to complete before raising an error (see ERROR HANDLING below for details on handling errors).
  • ns_proxy get pool ? -handles value? ? -timeout value?
Returns one or more handles to proxies from the specified pool. The pool will be created with default options if it does not already exist. The option -handle can be used to specify the number of handles allocated (default: 1). The option timeout specifies the maximum amount of time in milliseconds to wait for the handles to become available before raising an error. Requesting more than one handle in a single call (if more than one handle is required) is necessary as it is an error to request handles from a pool from which handles are already owned by the thread. This restriction is implemented to avoid possible deadlock conditions. The handle returned by this command can be used as a scalar value for other ns_proxy commands, or it can be used as Tcl command itself (see ns_proxy eval for more information).
  • ns_proxy ping handle
Check if handle is alive by sending an empty request. This command is not normally required as the ns_proxy eval command will also verify and restart proxies as needed.
  • ns_proxy recv
Receives a response from a script that was sent via ns_proxy send and waited on via ns_proxy wait.
  • ns_proxy release handle
Release a single handle. All handles owned by a thread to the corresponding pool must be returned before any handles can be allocated again. Within AOLserver, a call to this routine is recommended for clarity but not stricting necessary. As described above, AOLserver installs a trace to release all handles at the end of every connection during interpreter deallocation.
  • ns_proxy send handle script
Sends script in the proxy specified by handle. Unlike with ns_proxy eval, this option will return immediately while the script continues to execute in the proxy process. A later ns_proxy wait followed by an ns_proxy recv is expected.
  • ns_proxy wait handle ?timeout?
Waits for a script sent via ns_proxy send in the proxy specified by the handle argument to complete. The optional timeout parameter specifies the number of milliseconds to wait for the script to complete, the default is an indefinite wait.


ERROR HANDLING

Errors generated by a script evaluated in a proxy interpreter are completely returned to the calling interpreter, including mapping the errorInfo and errorInfo global variables from the proxy to the parent if present and raising a Tcl exception. This enables proxy code to look very similar to that which may use the internal eval command.
Errors raised by a failure to communicate with the proxy process due to a timeout or unexpected process exit are also communicated back to the parent interpreter as Tcl exceptions. To distinguish between these cases, communication related errors set the errorCode global variable with the first element NSPROXY. The second element is one of the following:
EDeadlock
The interpreter attempted to allocate handles from a pool from which it already owns one or more handles.
EExec
The slave program specified by the -exec program option could not be started.
ERange
Insufficient handles available in pool.
ERecv
There was an error receiving the result from the slave process.
ESend
There was an error sending the script to the slave process.
ETimeout
The timeout specified for the pool by the -evaltimeout option or as the optional argument to the current call to ns_proxy eval was exceeded.

EXAMPLES

The following demonstrates sending a script to a remote proxy:

       set handle [ns_proxy get myproxy]
       ns_proxy eval $handle {info patchlevel}
       ns_proxy release $handle

The following demonstrates an asyncronous request:

       set handle [ns_proxy get myproxy]
       ns_proxy send $handle {long running script}
       ... continue other work ...
       ns_proxy wait $handle
       set result [ns_proxy recv $handle]
       ns_proxy release $handle

The following demonstrates using multiple proxies:

       ns_proxy config myproxy -max 10
       set handles [ns_proxy get myproxy -handle 10]
       foreach h $handles {
               ns_proxy eval $h {puts "alive: [pid]"}
       }
       ns_proxy cleanup

SEE ALSO

ns_eval -