Skip to main content

hashMapRetry

This item is only intended to be used by the module's authors. Private

Provides a utility function to wrap Roblox MemoryStoreService HashMap API calls (like GetAsync, SetAsync, UpdateAsync) with automatic retries for specific, commonly transient error conditions identified by error message strings.

Purpose: Similar to dataStoreRetry, MemoryStore operations can fail due to temporary service issues, throttling, or internal errors. This module implements a retry mechanism with exponential backoff to handle these transient failures. It differs from dataStoreRetry by matching against error strings rather than numeric codes, as MemoryStore errors often manifest this way. It also includes a cancellation mechanism.

Usage:

local MemoryStoreService = game:GetService("MemoryStoreService")
local hashMapRetry = require(script.Parent.hashMapRetry)
local myMap = MemoryStoreService:GetHashMap("MyMap")

local handle = hashMapRetry(function()
	-- This function will be retried automatically on specific errors
	return myMap:GetAsync("MyKey")
end)

handle.promise:andThen(function(data)
	print("Successfully retrieved data:", data)
end):catch(function(err)
	warn("Failed to get data after retries or cancelled:", err)
end)

-- To cancel the operation:
-- handle.cancel()

Functions

hashMapRetry

hashMapRetry.hashMapRetry(
func() → any--

The function to execute and potentially retry. This function should perform the desired MemoryStore HashMap operation (e.g., GetAsync, SetAsync, UpdateAsync). It should return the result on success or throw an error on failure.

) → RetryHandle<Promise<any>>--

A handle containing: promise (A Promise that resolves with the return value of func if it succeeds within MAX_RETRIES attempts) and cancel (A function that, when called, attempts to stop further retries and rejects the promise).

Wraps a function that performs a MemoryStore HashMap operation, automatically retrying it if it fails with specific transient error message substrings.

Implements an exponential backoff strategy: waits 1s, 2s, 4s, 8s between retries. Includes a cancellation mechanism via the returned [RetryHandle].

Errors

TypeDescription
stringRejects if `func` fails with a non-retriable error, if it fails with a retriable error `MAX_RETRIES` times, or if `cancel()` is called.
Show raw api
{
    "functions": [
        {
            "name": "hashMapRetry",
            "desc": "Wraps a function that performs a MemoryStore HashMap operation, automatically\nretrying it if it fails with specific transient error message substrings.\n\nImplements an exponential backoff strategy: waits 1s, 2s, 4s, 8s between retries.\nIncludes a cancellation mechanism via the returned [RetryHandle].",
            "params": [
                {
                    "name": "func",
                    "desc": "The function to execute and potentially retry. This function should perform the desired MemoryStore HashMap operation (e.g., `GetAsync`, `SetAsync`, `UpdateAsync`). It should return the result on success or throw an error on failure.",
                    "lua_type": "() -> any"
                }
            ],
            "returns": [
                {
                    "desc": "A handle containing: `promise` (A Promise that resolves with the return value of `func` if it succeeds within `MAX_RETRIES` attempts) and `cancel` (A function that, when called, attempts to stop further retries and rejects the promise).",
                    "lua_type": "RetryHandle<Promise<any>>"
                }
            ],
            "function_type": "static",
            "errors": [
                {
                    "lua_type": "string",
                    "desc": "Rejects if `func` fails with a non-retriable error, if it fails with a retriable error `MAX_RETRIES` times, or if `cancel()` is called."
                }
            ],
            "source": {
                "line": 75,
                "path": "src/hashMapRetry.luau"
            }
        }
    ],
    "properties": [],
    "types": [],
    "name": "hashMapRetry",
    "desc": "Provides a utility function to wrap Roblox MemoryStoreService HashMap API calls\n(like `GetAsync`, `SetAsync`, `UpdateAsync`) with automatic retries for specific,\ncommonly transient error conditions identified by error message strings.\n\n**Purpose:** Similar to [dataStoreRetry], MemoryStore operations can fail due to\ntemporary service issues, throttling, or internal errors. This module implements\na retry mechanism with exponential backoff to handle these transient failures.\nIt differs from [dataStoreRetry] by matching against error *strings* rather than\nnumeric codes, as MemoryStore errors often manifest this way. It also includes\na cancellation mechanism.\n\n**Usage:**\n```lua\nlocal MemoryStoreService = game:GetService(\"MemoryStoreService\")\nlocal hashMapRetry = require(script.Parent.hashMapRetry)\nlocal myMap = MemoryStoreService:GetHashMap(\"MyMap\")\n\nlocal handle = hashMapRetry(function()\n\t-- This function will be retried automatically on specific errors\n\treturn myMap:GetAsync(\"MyKey\")\nend)\n\nhandle.promise:andThen(function(data)\n\tprint(\"Successfully retrieved data:\", data)\nend):catch(function(err)\n\twarn(\"Failed to get data after retries or cancelled:\", err)\nend)\n\n-- To cancel the operation:\n-- handle.cancel()\n```",
    "private": true,
    "source": {
        "line": 37,
        "path": "src/hashMapRetry.luau"
    }
}