Skip to content

glide_shared.ffi_helpers

Shared FFI helper utilities for converting Python arguments to C-compatible arrays.

FFIClientTypeEnum

Client type enum matching the Rust ClientType repr.

Source code in doc-gen/valkey-glide/python/glide-shared/glide_shared/ffi_helpers.py
264
265
266
267
268
class FFIClientTypeEnum:
    """Client type enum matching the Rust ClientType repr."""

    Async = 0
    Sync = 1

encode_arg(arg)

Encode a single argument to bytes.

Source code in doc-gen/valkey-glide/python/glide-shared/glide_shared/ffi_helpers.py
138
139
140
141
142
143
144
def encode_arg(arg):
    """Encode a single argument to bytes."""
    if isinstance(arg, str):
        return arg.encode(ENCODING)
    if isinstance(arg, (bytes, bytearray, memoryview)):
        return bytes(arg) if isinstance(arg, (bytearray, memoryview)) else arg
    raise TypeError(f"Unsupported argument type: {type(arg)}")

to_c_strings(ffi, args)

Convert Python arguments to C-compatible (pointers_array, lengths_array, buffers).

The returned buffers list must be kept alive for the duration of the FFI call.

Source code in doc-gen/valkey-glide/python/glide-shared/glide_shared/ffi_helpers.py
147
148
149
150
151
152
153
154
155
156
157
def to_c_strings(ffi, args):
    """Convert Python arguments to C-compatible (pointers_array, lengths_array, buffers).

    The returned `buffers` list must be kept alive for the duration of the FFI call.
    """
    buffers = [encode_arg(a) for a in args]
    c_strings = ffi.new(
        "size_t[]", [ffi.cast("size_t", ffi.from_buffer(b)) for b in buffers]
    )
    c_lengths = ffi.new("unsigned long[]", [len(b) for b in buffers])
    return c_strings, c_lengths, buffers

to_c_route_ptr_and_len(ffi, route)

Convert a Route to C-compatible (route_ptr, route_len, route_bytes).

The returned route_bytes must be kept alive for the duration of the FFI call.

Source code in doc-gen/valkey-glide/python/glide-shared/glide_shared/ffi_helpers.py
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
def to_c_route_ptr_and_len(ffi, route):
    """Convert a Route to C-compatible (route_ptr, route_len, route_bytes).

    The returned `route_bytes` must be kept alive for the duration of the FFI call.
    """
    if route is None:
        return ffi.NULL, 0, None

    from glide_shared.routes import build_protobuf_route

    proto_route = build_protobuf_route(route)
    if proto_route:
        route_bytes = proto_route.SerializeToString()
        route_ptr = ffi.from_buffer(route_bytes)
        route_len = len(route_bytes)
    else:
        route_bytes = None
        route_ptr = ffi.NULL
        route_len = 0
    return route_ptr, route_len, route_bytes

to_c_route_info(ffi, route)

Convert a Route to a C RouteInfo* for batch operations.

Returns (route_info_ptr, refs) where refs must be kept alive for the duration of the FFI call.

Source code in doc-gen/valkey-glide/python/glide-shared/glide_shared/ffi_helpers.py
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
def to_c_route_info(ffi, route):
    """Convert a Route to a C RouteInfo* for batch operations.

    Returns (route_info_ptr, refs) where refs must be kept alive
    for the duration of the FFI call.
    """
    if route is None:
        return ffi.NULL, []

    from glide_shared.routes import (
        AllNodes,
        AllPrimaries,
        ByAddressRoute,
        RandomNode,
        SlotIdRoute,
        SlotKeyRoute,
        SlotType,
    )

    refs = []
    slot_key_ptr = ffi.NULL
    hostname_ptr = ffi.NULL
    route_type = _RouteType.RANDOM
    slot_id = 0
    slot_type = 0  # Primary
    port = 0

    if isinstance(route, AllNodes):
        route_type = _RouteType.ALL_NODES
    elif isinstance(route, AllPrimaries):
        route_type = _RouteType.ALL_PRIMARIES
    elif isinstance(route, RandomNode):
        route_type = _RouteType.RANDOM
    elif isinstance(route, SlotIdRoute):
        route_type = _RouteType.SLOT_ID
        slot_id = route.slot_id
        slot_type = 0 if route.slot_type == SlotType.PRIMARY else 1
    elif isinstance(route, SlotKeyRoute):
        route_type = _RouteType.SLOT_KEY
        slot_key_bytes = route.slot_key.encode(ENCODING) + b"\0"
        refs.append(slot_key_bytes)
        slot_key_ptr = ffi.from_buffer(slot_key_bytes)
        slot_type = 0 if route.slot_type == SlotType.PRIMARY else 1
    elif isinstance(route, ByAddressRoute):
        route_type = _RouteType.BY_ADDRESS
        hostname_bytes = route.host.encode(ENCODING) + b"\0"
        refs.append(hostname_bytes)
        hostname_ptr = ffi.from_buffer(hostname_bytes)
        port = route.port if route.port is not None else 0

    route_info = ffi.new(
        "RouteInfo*",
        {
            "route_type": route_type,
            "slot_id": slot_id,
            "slot_key": slot_key_ptr,
            "slot_type": slot_type,
            "hostname": hostname_ptr,
            "port": port,
        },
    )
    refs.append(route_info)
    return route_info, refs

parse_push_notification(ffi, kind, message_ptr, message_len, channel_ptr, channel_len, pattern_ptr, pattern_len)

Parse raw FFI push notification data into (message_kind, message_bytes, channel_bytes, pattern_bytes).

Returns (kind_str, message, channel, pattern) where pattern may be None.

Source code in doc-gen/valkey-glide/python/glide-shared/glide_shared/ffi_helpers.py
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
def parse_push_notification(
    ffi,
    kind,
    message_ptr,
    message_len,
    channel_ptr,
    channel_len,
    pattern_ptr,
    pattern_len,
):
    """Parse raw FFI push notification data into (message_kind, message_bytes, channel_bytes, pattern_bytes).

    Returns (kind_str, message, channel, pattern) where pattern may be None.
    """
    message = ffi.buffer(message_ptr, message_len)[:]
    channel = ffi.buffer(channel_ptr, channel_len)[:]
    pattern = (
        ffi.buffer(pattern_ptr, pattern_len)[:] if pattern_ptr != ffi.NULL else None
    )
    message_kind = PUSH_KIND_MAP.get(kind)
    return message_kind, message, channel, pattern

convert_commands_to_c_batch_info(ffi, commands, is_atomic)

Convert a list of (request_type, args) tuples to a C BatchInfo*.

Returns (batch_info, refs) where refs must be kept alive during the FFI call.

Source code in doc-gen/valkey-glide/python/glide-shared/glide_shared/ffi_helpers.py
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
def convert_commands_to_c_batch_info(ffi, commands, is_atomic):
    """Convert a list of (request_type, args) tuples to a C BatchInfo*.

    Returns (batch_info, refs) where refs must be kept alive during the FFI call.
    """
    all_refs = []
    cmd_infos = []

    for request_type, args in commands:
        arg_buffers = []
        arg_ptrs = []
        arg_lengths = []

        for arg in args:
            arg_bytes = encode_arg(arg)
            arg_buffers.append(arg_bytes)
            arg_ptrs.append(ffi.from_buffer(arg_bytes))
            arg_lengths.append(len(arg_bytes))

        c_arg_array = ffi.new("const uint8_t*[]", arg_ptrs)
        c_lengths = ffi.new("size_t[]", arg_lengths)

        cmd_info = ffi.new(
            "CmdInfo*",
            {
                "request_type": request_type,
                "args": c_arg_array,
                "arg_count": len(args),
                "args_len": c_lengths,
            },
        )

        cmd_infos.append(cmd_info)
        all_refs.extend(arg_buffers + [c_arg_array, c_lengths])

    cmd_info_array = ffi.new("const CmdInfo*[]", cmd_infos)
    all_refs.extend(cmd_infos + [cmd_info_array])

    batch_info = ffi.new(
        "BatchInfo*",
        {
            "cmd_count": len(commands),
            "cmds": cmd_info_array,
            "is_atomic": is_atomic,
        },
    )

    return batch_info, all_refs + [batch_info]

create_c_batch_options(ffi, route, retry_server_error=False, retry_connection_error=False, timeout=None)

Create a C BatchOptionsInfo* from Python parameters.

Returns (batch_options, refs) where refs must be kept alive during the FFI call.

Source code in doc-gen/valkey-glide/python/glide-shared/glide_shared/ffi_helpers.py
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
def create_c_batch_options(
    ffi, route, retry_server_error=False, retry_connection_error=False, timeout=None
):
    """Create a C BatchOptionsInfo* from Python parameters.

    Returns (batch_options, refs) where refs must be kept alive during the FFI call.
    """
    route_info, route_refs = to_c_route_info(ffi, route)

    batch_options = ffi.new(
        "BatchOptionsInfo*",
        {
            "retry_server_error": retry_server_error,
            "retry_connection_error": retry_connection_error,
            "has_timeout": timeout is not None,
            "timeout": timeout or 0,
            "route_info": route_info,
        },
    )

    return batch_options, route_refs + [batch_options]

create_address_resolver_callback(ffi, resolver_fn)

Create an AddressResolverCallback for the FFI from a Python resolver function.

Parameters:

Name Type Description Default
ffi

The CFFI instance.

required
resolver_fn

A callable(host: str, port: int) -> (resolved_host: str, resolved_port: int).

required

Returns:

Type Description

The CFFI callback object. Caller must keep a reference to prevent GC.

Source code in doc-gen/valkey-glide/python/glide-shared/glide_shared/ffi_helpers.py
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
def create_address_resolver_callback(ffi, resolver_fn):
    """Create an AddressResolverCallback for the FFI from a Python resolver function.

    Args:
        ffi: The CFFI instance.
        resolver_fn: A callable(host: str, port: int) -> (resolved_host: str, resolved_port: int).

    Returns:
        The CFFI callback object. Caller must keep a reference to prevent GC.
    """
    if resolver_fn is None:
        return ffi.NULL

    def _address_resolver_callback(
        client_id,
        host_ptr,
        host_len,
        port,
        resolved_host_buf,
        resolved_host_buf_len,
        resolved_host_len_ptr,
    ):
        try:
            host = ffi.buffer(host_ptr, host_len)[:].decode(ENCODING)
            resolved_host, resolved_port = resolver_fn(host, port)
            encoded_host = resolved_host.encode(ENCODING)
            write_len = min(len(encoded_host), resolved_host_buf_len)
            ffi.memmove(resolved_host_buf, encoded_host, write_len)
            resolved_host_len_ptr[0] = write_len
            return resolved_port
        except Exception as e:
            # Return 0 (original port) to signal failure to the Rust layer,
            # which will fall back to the original address. We cannot propagate
            # exceptions across the FFI callback boundary.
            from glide_shared.logger import Level, Logger

            Logger.log(Level.WARN, "address_resolver", f"Resolver failed: {e}")
            return 0

    return ffi.callback("AddressResolverCallback", _address_resolver_callback)

create_credential_provider_callback(ffi, credential_provider_fn, *, callback_id=None, event_loop=None, trio_token=None, allow_async=False, provider_owner=None)

Register a credential owner behind an FFI-stable weak trampoline.

The callback follows the native stateless tri-state protocol: 0 means failure, 1 means success, and 2 requests larger buffers. A provider can be called twice during large-value negotiation; no result is cached between those calls.

Returns:

Type Description

A (callback, owner) pair. The callback is typed NULL and the owner

is None when no provider is configured. Otherwise the caller strongly

owns the owner and must close it at the lifecycle transition where new

callbacks are no longer allowed. The module registry holds it weakly.

Source code in doc-gen/valkey-glide/python/glide-shared/glide_shared/ffi_helpers.py
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
def create_credential_provider_callback(
    ffi,
    credential_provider_fn,
    *,
    callback_id=None,
    event_loop=None,
    trio_token=None,
    allow_async=False,
    provider_owner=None,
):
    """Register a credential owner behind an FFI-stable weak trampoline.

    The callback follows the native stateless tri-state protocol: 0 means
    failure, 1 means success, and 2 requests larger buffers. A provider can be
    called twice during large-value negotiation; no result is cached between
    those calls.

    Returns:
        A ``(callback, owner)`` pair. The callback is typed NULL and the owner
        is None when no provider is configured. Otherwise the caller strongly
        owns the owner and must close it at the lifecycle transition where new
        callbacks are no longer allowed. The module registry holds it weakly.
    """
    if credential_provider_fn is None:
        return ffi.cast("CredentialProviderCallback", ffi.NULL), None

    if callback_id is None:
        callback_id = _allocate_credential_callback_id(ffi)
    callback_owner = _CredentialProviderCallbackOwner(
        callback_id,
        credential_provider_fn,
        event_loop=event_loop,
        trio_token=trio_token,
        allow_async=allow_async,
        provider_owner=provider_owner,
    )
    return _get_credential_provider_trampoline(ffi), callback_owner

handle_command_result(ffi, lib, command_result, response_handler)

Handle a synchronous CommandResult* from FFI.

Parameters:

Name Type Description Default
ffi

The CFFI instance.

required
lib

The FFI library.

required
command_result

The CommandResult* pointer from FFI.

required
response_handler

A callable that takes a response pointer and returns the parsed result.

required

Returns:

Type Description

The parsed response value.

Raises:

Type Description
ClosingError

If result is NULL.

RequestError subclass

If the result contains an error.

Source code in doc-gen/valkey-glide/python/glide-shared/glide_shared/ffi_helpers.py
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
def handle_command_result(ffi, lib, command_result, response_handler):
    """Handle a synchronous CommandResult* from FFI.

    Args:
        ffi: The CFFI instance.
        lib: The FFI library.
        command_result: The CommandResult* pointer from FFI.
        response_handler: A callable that takes a response pointer and returns the parsed result.

    Returns:
        The parsed response value.

    Raises:
        ClosingError: If result is NULL.
        RequestError subclass: If the result contains an error.
    """
    from glide_shared.exceptions import ClosingError, get_request_error_class

    try:
        if command_result == ffi.NULL:
            raise ClosingError("Internal error: Received NULL as a command result")
        if command_result.command_error != ffi.NULL:
            error = ffi.cast("CommandError*", command_result.command_error)
            error_message = ffi.string(error.command_error_message).decode(ENCODING)
            error_class = get_request_error_class(error.command_error_type)
            raise error_class(error_message)
        else:
            return response_handler(command_result.response)
    finally:
        lib.free_command_result(command_result)

parse_inline_pubsub(payload)

Parse inline pubsub payload from pipe.

Format: kind(4) msg_len(4) msg(...) ch_len(4) ch(...) pat_len(4) pat(...)

Source code in doc-gen/valkey-glide/python/glide-shared/glide_shared/ffi_helpers.py
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
def parse_inline_pubsub(payload: bytes):
    """Parse inline pubsub payload from pipe.

    Format: kind(4) msg_len(4) msg(...) ch_len(4) ch(...) pat_len(4) pat(...)
    """
    import sys

    off = 0
    kind = int.from_bytes(payload[off : off + 4], sys.byteorder, signed=True)
    off += 4
    msg_len = int.from_bytes(payload[off : off + 4], sys.byteorder, signed=False)
    off += 4
    message = payload[off : off + msg_len]
    off += msg_len
    ch_len = int.from_bytes(payload[off : off + 4], sys.byteorder, signed=False)
    off += 4
    channel = payload[off : off + ch_len]
    off += ch_len
    pat_len = int.from_bytes(payload[off : off + 4], sys.byteorder, signed=False)
    off += 4
    pattern = payload[off : off + pat_len] if pat_len > 0 else None
    kind_str = _PUBSUB_KIND_MAP.get(kind)
    return kind_str, message, channel, pattern