Serialization
Messages inside one process are values: nothing is serialized. Messages that cross the network (a hosted actor’s payload, a distributed topic’s events, the cluster’s own messages) are bytes, written and read by codecs.
import Payload.Codec
data Codec a -- how a value of type a is written and read back
encodePayload :: Codec a -> a -> Either Fault Bytes
decodePayload :: Codec a -> Bytes -> Either Fault a
There is no reflection: a codec is built from the codecs of its parts, so what is written is exactly what the type says, and nothing a peer sends can make a reader build a type it did not ask for.
Codecs for simple values
| Codec | Type |
|---|---|
intCodec |
Int |
boolCodec |
Bool |
floatCodec |
Float |
stringCodec |
String |
bytesCodec |
Bytes |
maybeCodec c |
Maybe a |
listCodec c |
[a] |
mapCodec k v |
[(k, v)] |
Records
A record is written as an object of its fields in order. field get codec default describes one field;
recordCodecN (1 to 10 fields) puts them together with the constructor:
data Order = Order { orderId :: String, quantity :: Int, express :: Bool }
orderCodec :: Codec Order
orderCodec = recordCodec3 Order
(field (\o -> o.orderId) stringCodec "")
(field (\o -> o.quantity) intCodec 0)
(field (\o -> o.express) boolCodec False)
encodePayload orderCodec (Order "o-17" 3 True) -- Right bytes
decodePayload orderCodec bytes -- Right (Order "o-17" 3 True)
Sums
A sum is written as an object whose first field is its constructor’s index. Say how each value is written
(caseN index codec value …) and how each constructor is read back (altN constructor codec default …, in index
order):
data Shape = Dot | Circle Int | Rect Int Int
shapeCase :: Shape -> Case
shapeCase s = case s of
Dot -> case0 0
Circle r -> case1 1 intCodec r
Rect w h -> case2 2 intCodec w intCodec h
shapeCodec :: Codec Shape
shapeCodec = unionCodec shapeCase [alt0 Dot, alt1 Circle intCodec 0, alt2 Rect intCodec 0 intCodec 0]
encodePayload (listCodec shapeCodec) [Dot, Circle 2, Rect 3 4]
Recursive types
A type that contains itself refers to its own codec through lazyCodec, which builds it when first used:
data Chain = End | Link Chain
chainCodec :: Codec Chain
chainCodec = unionCodec chainCase [alt0 End, alt1 Link (lazyCodec (\u -> chainCodec)) End]
Evolving a message
Readers and writers of different versions understand each other:
- A reader takes the fields it knows and skips any a newer writer added.
- A reader gives the default of any field an older writer did not write.
So a field is added at the end, with a default:
data OrderV2 = OrderV2 { v2Id :: String, v2Quantity :: Int, v2Express :: Bool, v2Note :: String }
orderV2Codec :: Codec OrderV2
orderV2Codec = recordCodec4 OrderV2
(field (\o -> o.v2Id) stringCodec "")
(field (\o -> o.v2Quantity) intCodec 0)
(field (\o -> o.v2Express) boolCodec False)
(field (\o -> o.v2Note) stringCodec "(no note)")
-- an Order written by the old codec, read by the new one: v2Note is "(no note)"
Never reorder or remove fields, and never reuse a constructor index for something else.
Hosted messages
A hosted actor receives HostedMsg typeName payload reply. The type name is what the sender said the payload is;
use it to choose the codec when a route accepts more than one message:
receive ctx g msg = case msg of
HostedMsg "String" payload reply -> ...
HostedMsg other payload reply -> Sat.answer reply (Left (Fault "UnknownType" other))
Limits
A document is checked as it is read: a truncated or malformed one is Left (Fault ...), never a crash. A string’s
length, a list’s count and a blob’s size are read from the document, so a reader only allocates what is actually
there.
Troubleshooting
| Symptom | Cause |
|---|---|
| A field comes back as its default | The writer is older and did not write it, or the fields are in a different order |
Left on decode |
The bytes were written by another codec, or cut short |
| The wrong constructor | The caseN indexes and the altN list disagree; the list must be in index order |