EsHiredisSciSocketContext
Description
This class is the primary, low-level connection handler for the EsRedisClient. It provides a pure Smalltalk implementation that bridges the gap between the high-level client and the network, using SciSocket for communication and the hiredis C library for parsing. It serves as a direct replacement for the original, purely FFI-based OSHiredisContext.
This class orchestrates the communication between a low-level <SciSocket> and the hiredis C parsing library, which is wrapped by <OSHiredisReader>.
This context is responsible for managing the entire lifecycle of a connection to the Redis server. It handles connecting, disconnecting, and reconnecting. It formats Smalltalk commands into the RESP protocol, sends them over the socket, and then uses the hiredis reader to parse the incoming byte stream back into structured replies.
To ensure thread safety in environments where a single <EsRedisClient> might be shared across multiple Smalltalk processes, all socket I/O and state-modifying operations are protected by a mutex.
Pipelining
For performance, this class supports command pipelining. When in a pipeline, commands are not sent immediately but are buffered internally. The entire buffer of commands is then flushed to the socket in a single network write when the pipeline is executed, reducing network latency.
Instance Variables
-
socket - <SciSocket> The underlying socket connection to the Redis server.
-
reader - <OSHiredisReader> The hiredis parser object that consumes bytes from the socket and produces reply objects.
-
dispatcher - <EsHiredisLibraryDispatcher> Used to make calls into the hiredis C library for parsing and freeing reply objects.
-
connectionOptions - <EsRedisConnectionOptions> Caches the options used to establish the connection, primarily for the reconnect functionality.
-
errstr - <String> Stores the description of the last error that occurred.
-
client - <EsRedisClient> A back-pointer to the high-level client, allowing the context to access client-level settings like the default string reply container.
-
pipelineBuffer - <WriteStream> An in-memory buffer used to queue commands during a pipeline operation before they are sent in a single batch.
-
readBuffer - <ByteArray> A reusable buffer to receive data from the socket, optimizing memory usage by avoiding repeated allocations.
-
mutex - <Semaphore> Ensures that access to the context's state and socket operations are thread-safe.
-
ownsSocket - <Boolean> A flag indicating whether this context created and owns the socket. If true, the context is responsible for closing the socket upon disconnection. If false, the socket is managed externally and will not be closed.
Class Methods
onSocket:takeOwnership:
Create a new context wrapping an existing, connected SciSocket.
Arguments:
anSciSocket - <SciSocket> A connected socket instance.
takeSocketOwnership - <Boolean> Should take over socket lifecycle
Answers:
<EsHiredisSciSocketContext> A new context instance.
Instance Methods
command:as:
Execute a command and wait for the reply. This operation is thread-safe.
Arguments:
aStringOrSequenceableCollection - <String | SequenceableCollection> The command to execute.
aContainerClass - <Class> The desired container for the reply.
Answers:
<Object> The processed reply from Redis.
<SciError> If a socket error occurs during the send.
Raises:
<EsRedisConnectionException> If the context is not connected.
disconnect
Disconnect from the Redis server and release associated resources. This operation is thread-safe.
getReplies:withConverters:
Collect the specified number of replies from the server. This entire operation is thread-safe.
The first call to getReply: will flush any existing pipeline buffer.
Arguments:
anInteger - <Integer> The number of replies to read.
aCollectionOfConverters - <Collection> A collection of classes to use for converting each reply.
Answers:
<Array> An array of the processed replies.
getReply:
Read and process a single pending reply from the server. This is the thread-safe
public entry point for getting a reply.
Arguments:
aContainerClass - <Class> The desired container for the reply.
Answers:
<Object> The processed reply from Redis.
<SciError> If a socket error occurs.
Raises:
<EsRedisConnectionException> If the connection is closed by the server.
handlePushData:
Handle incoming PUSH data by passing it to the client for dispatch.
Arguments:
pushData - <Array> The parsed PUSH reply data.
isConnected
Answer true if the socket is connected.
Answers:
<Boolean>
isSecure
The tunnel is secure if a secure handle connection
is being used
Answers:
<Boolean>
pipelineAppend:
Format a command and append it to the internal pipeline buffer without sending it.
This operation is thread-safe.
Arguments:
aStringOrSequenceableCollection - <String | SequenceableCollection> The command to buffer.
Answers:
<Integer> REDIS_OK to indicate success.
processReply:as:
Process the reply from the hiredis reader, converting it to a Smalltalk object.
Arguments:
aReply - <OSHiredisReply> The raw reply object from the C library.
aContainerClass - <Class> The desired container for the reply.
Answers:
<Object> The converted Smalltalk object.
reconnect
Reconnect to the server using the stored connection options. This operation is thread-safe.
sendCommandForTransactionAsync:
Append a command to the output buffer and answer a future for its reply.
This method is for internal async use by EsRedisPipeline and EsRedisTransaction.
It sends the command immediately and forks a process to wait for the reply.
Arguments:
aStringOrSequenceableCollection - <String | SequenceableCollection> The command to append and send.
Answers:
<EsFuture> A future that will complete with the command's reply (typically 'QUEUED' inside a transaction).
tryGetReply:
Non-blocking variant of #getReply:.
- Answers nil if no complete reply is currently available.
- Still thread-safe via the context mutex.
- May answer a SciError on a hard socket error.
- May signal EsRedisConnectionException if the peer closed.
Last modified date: 01/22/2026