Purpose
The Messagepack::Factory class provides thread-safe management of packer and unpacker instances with support for custom type registrations.
References
-
API reference - Factory class documentation
-
Thread-safe usage tutorial - Multi-threading best practices
Concepts
Creating a factory
factory = Messagepack::Factory.newWhere,
-
Factory.newcreates a new factory instance -
Each factory maintains its own type registry
-
Factories can be frozen for thread-safe use
Registering custom types
factory.register_type(0x01, MyClass,
packer: :to_msgpack_ext,
unpacker: :from_msgpack_ext
)Where,
-
0x01is the type identifier (must be -128 to 127) -
MyClassis the Ruby class to register -
packerspecifies how to serialize instances (symbol, method, or proc) -
unpackerspecifies how to deserialize data (symbol, method, or proc)
Using factory pool for thread safety
pool = factory.pool(5) # Create pool with 5 packers/unpackers
data = pool.pack(my_object) # Thread-safe packing
obj = pool.unpack(binary) # Thread-safe unpackingWhere,
-
factory.pool(size)creates a thread-safe pool -
sizeis the number of packer/unpacker instances in the pool -
The pool automatically manages instance reuse
-
Each thread gets its own instance from the pool
Examples
# Create a factory with custom types
factory = Messagepack::Factory.new
factory.register_type(0x01, MyCustomClass,
packer: ->(obj) { obj.serialize },
unpacker: ->(data) { MyCustomClass.deserialize(data) }
)
# Create a thread-safe pool
pool = factory.pool(10)
# Use from multiple threads safely
threads = 10.times.map do |i|
Thread.new do
object = MyCustomClass.new("data-#{i}")
binary = pool.pack(object)
result = pool.unpack(binary)
result.value
end
end
puts threads.map(&:value).inspect# Create factory
factory = Messagepack::Factory.new
# Pool size should match expected concurrent threads
# Too small: threads wait for available instances
# Too much: wasted memory
pool = factory.pool(4) # Match your thread pool size
# Default factory is also available
Messagepack.pack(data) # Uses DefaultFactory internally
Messagepack::DefaultFactory.register_type(0x01, MyClass, ...)