<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by David Delassus on Medium]]></title>
        <description><![CDATA[Stories by David Delassus on Medium]]></description>
        <link>https://medium.com/@david-delassus?source=rss-64e2b344388d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*XlMXM1xJ5ziwnzOfWuQ7yw.png</url>
            <title>Stories by David Delassus on Medium</title>
            <link>https://medium.com/@david-delassus?source=rss-64e2b344388d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 00:07:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@david-delassus/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Distributed Systems without Raft (part 3)]]></title>
            <link>https://david-delassus.medium.com/distributed-systems-without-raft-part-3-a200bf071c64?source=rss-64e2b344388d------2</link>
            <guid isPermaLink="false">https://medium.com/p/a200bf071c64</guid>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[open-source]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[David Delassus]]></dc:creator>
            <pubDate>Mon, 13 Jul 2026 18:40:03 GMT</pubDate>
            <atom:updated>2026-07-13T18:40:03.002Z</atom:updated>
            <content:encoded><![CDATA[<p>Last year, I wrote about my work on FlowG, trying to implement replication without Raft:</p><ul><li><a href="https://david-delassus.medium.com/distributed-systems-without-raft-part-1-a6b0b43db7ee">Distributed Systems without Raft (part 1)</a></li><li><a href="https://david-delassus.medium.com/distributed-systems-without-raft-part-2-81ca31eae4db">Distributed Systems without Raft (part 2)</a></li></ul><p>We are now 1 year later, and the result is… failure.</p><p>The topic of writing a distributed system, especially one as critical as a database, is extremely complex.</p><p>While I managed to get some experiments working, the cost in performance (bandwidth, CPU and IO usage) was just too much and nowhere near production ready.</p><p>The complexity of the algorithms, and the complexity of the test suite itself was just too big.</p><p>The cherry on the cake was that the more nodes the cluster had, the less performant (log/s throughput) it was. The complete opposite of what we wanted!</p><p>In the end, we decided to take the safer road, use a proper distributed database, and our choice landed on <strong>FoundationDB</strong>.</p><p><a href="https://www.foundationdb.org/">FoundationDB | Home</a></p><p>On the occasion of its release, I wrote on FlowG’s blog a “deep dive” article on the data model, and limitations we met:</p><p><a href="https://flowg.cloud/blog/replication-via-foundationdb">Replication via FoundationDB | FlowG</a></p><p>Have a great read!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a200bf071c64" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Distributed Systems without Raft (part 2)]]></title>
            <link>https://david-delassus.medium.com/distributed-systems-without-raft-part-2-81ca31eae4db?source=rss-64e2b344388d------2</link>
            <guid isPermaLink="false">https://medium.com/p/81ca31eae4db</guid>
            <category><![CDATA[consensus-algorithm]]></category>
            <category><![CDATA[log-management-software]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[replication]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[David Delassus]]></dc:creator>
            <pubDate>Wed, 14 May 2025 11:49:58 GMT</pubDate>
            <atom:updated>2025-05-14T11:49:58.659Z</atom:updated>
            <content:encoded><![CDATA[<ul><li><a href="https://medium.com/@david-delassus/distributed-systems-without-raft-part-1-a6b0b43db7ee">Part 1: SWIM Protocol for Node Discovery</a></li><li><strong>Part 2: Incremental updates of every node</strong></li></ul><h3>Introduction and Context</h3><p>Recently, I published the v0.37.0 release of <strong>FlowG</strong>, a Free and OpenSource low-code log processing software:</p><p><a href="https://link-society.github.io/flowg">FlowG | FlowG</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/963/0*TdEtOEjK817cXVep.png" /><figcaption>FlowG’s pipeline editor</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/963/0*LqOQ78vcTQlVmDHU.png" /><figcaption>FlowG’s log viewer</figcaption></figure><p>This version includes the first <strong>experimental </strong>draft of replication, allowing FlowG to run on multiple servers as a single “semantic” instance.</p><h3>Refresher on Node Discovery</h3><p>We chose a <a href="https://en.wikipedia.org/wiki/Gossip_protocol">Gossip</a> protocol, <a href="https://en.wikipedia.org/wiki/SWIM_Protocol">SWIM</a> (<em>Scalable Weakly Consistent Infection-style Process Group Membership):</em></p><p><a href="https://github.com/hashicorp/memberlist">GitHub - hashicorp/memberlist: Golang package for gossip based membership and failure detection</a></p><p>Thanks to this library, we can tell a node to “join” another node, they will start exchanging information about the cluster architecture (aka: known nodes of the cluster).</p><p>A technical choice was also to use HTTP as the transport layer instead of the default TCP/UDP:</p><ul><li>the TCP part is replaced by a hijacked HTTP connection (similar to Websocket);</li><li>the UDP part is replaced by normal HTTP requests.</li></ul><p>“Why?” you may ask. Here are a few reasons:</p><ul><li>it becomes easy to add TLS, authentication, etc;</li><li>Reverse Proxies are easy to setup;</li><li>we might explore using HTTP3 later on if performance becomes an issue.</li></ul><h3>Eventual Consistency</h3><p>As said in the original article, we chose for FlowG to be <em>eventually consistent</em>. We initially planned to use an “operation log” and <a href="https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type"><strong>C</strong>onflict-free <strong>R</strong>eplicated <strong>D</strong>ata <strong>T</strong>ypes</a> (CRDTs) to implement the replication part.</p><p>This idea was temporarily dismissed. I’ll explain why.</p><p>Our storage backend is <strong>BadgerDB</strong>:</p><p><a href="https://docs.hypermode.com/badger/overview">Overview - Badger</a></p><p>It is an embeddable key/value database with full transaction support. Internally, it uses a <a href="https://en.wikipedia.org/wiki/Log-structured_merge-tree">Log Structured Merge Tree</a> (LSM) to organize key/value pairs.</p><p>Every key/value pair is assigned a “version”, and every mutation on a key updates that version. This enables incremental backups quite easily:</p><pre>nextSince, err := db.Backup(writer, since)<br>nextSince, err = db.Backup(writer, nextSince + 1)<br>// ...</pre><p>This also means that we don’t actually <em>need</em> an “operation log” to track the mutations done on a database.</p><p>As for the CRDT, FlowG has 3 kinds of storages:</p><ul><li><strong>auth:</strong> stores users, permissions, tokens, roles, …</li><li><strong>config:</strong> stores configuration (pipelines, transformers, forwarders, …)</li><li><strong>log:</strong> stores actual logs</li></ul><p>The first 2 storages aren’t used that much. FlowG is a “setup&amp;forget” kind of software. The 3rd one is <strong>append-only</strong> (in reality, we also have a purge operation to remove logs, but we never update logs).</p><p>The BadgerDB instance for those storage already acts like a CRDT. It is “last write wins” for the <em>auth</em> and <em>config</em> storages, and “append-only” for the <em>log</em> storage.</p><h3>Actual Replication</h3><p>To implement replication, we will rely on the SWIM protocol, and on BadgerDB’s incremental backup feature.</p><p>The hashicorp/memberlist package provides what they call a “TCP Push/Pull”. Periodically, each node computes their local state, send them to other nodes, which then merge the remote state with their local state.</p><p>We will trigger the replication during this “TCP Push/Pull” process.</p><p>We defined a node state as: the set of last known version of other nodes.</p><p>For example:</p><pre>{<br>  &quot;node_id&quot;: &quot;flowg-0&quot;,<br>  &quot;last_sync&quot;: {<br>    &quot;flowg-1&quot;: {<br>      &quot;auth&quot;: 1,<br>      &quot;config&quot;: 2,<br>      &quot;log&quot;: 324<br>    },<br>    &quot;flowg-2&quot;: {<br>      &quot;auth&quot;: 0,<br>      &quot;config&quot;: 0,<br>      &quot;log&quot;: 0<br>    }<br>  }<br>}</pre><p>When flowg-1 receives this state from flowg-0, it will start an HTTP request to flowg-0’s management interface and run (in parallel):</p><pre>// POST http://&lt;remote&gt;/cluster/sync/auth<br>newAuthSince, err := authDb.Backup(<br>    syncAuthRequestBodyWriter,<br>    remoteState.LastSync[&quot;flowg-1&quot;].Auth,<br>)<br>// send `X-FlowG-Since: &lt;newAuthSince&gt;` as HTTP trailer<br><br>// POST http://&lt;remote&gt;/cluster/sync/config<br>newConfigSince, err := configDb.Backup(<br>    syncConfigRequestBodyWriter,<br>    remoteState.LastSync[&quot;flowg-1&quot;].Config,<br>)<br>// send `X-FlowG-Since: &lt;newConfigSince&gt;` as HTTP trailer<br><br>// POST http://&lt;remote&gt;/cluster/sync/log<br>newLogSince, err := logDb.Backup(<br>    syncLogRequestBodyWriter,<br>    remoteState.LastSync[&quot;flowg-1&quot;].Log,<br>)<br>// send `X-FlowG-Since: &lt;newLogSince&gt;` as HTTP trailer</pre><p>On the node flowg-0, the HTTP handler for /cluster/sync/... will read the request body:</p><pre>nodeId := request.Header.Get(&quot;X-FlowG-NodeID&quot;)<br>err := db.Load(syncLogrequestBodyReader, 1)<br>updateLocalState(&quot;log&quot;, nodeId, request.Trailer.Get(&quot;X-FlowG-Since&quot;))</pre><p>And voilà! We have actual replication.</p><h3>A few considerations</h3><p>This is only a draft, and it is still highly experimental. Without further testing to understand:</p><ul><li>what happens if the syncing fails?</li><li>what happens during a network partition?</li><li>what happens if a node fails?</li><li>…</li></ul><p>I would not recommend going to production. But you’re free to test and open bug reports:</p><p><a href="https://github.com/link-society/flowg/issues">Issues · link-society/flowg</a></p><p>It would greatly help 🙂</p><h3>Technical notes</h3><p>The node’s local state is also stored in a BadgerDB instance, but it is not replicated. Only the 3 other storages are replicated.</p><p>The interval between 2 “TCP Push/Pull” has been arbitrarily set to 1s. Maybe it should be configurable, maybe it should be tuned, further testing is required.</p><p>We use a very niche feature of HTTP/1.1: Trailers</p><p><a href="https://developer.mozilla.org/fr/docs/Web/HTTP/Reference/Headers/Trailer">Trailer - HTTP | MDN</a></p><p>TL;DR: It allows the emitter to send headers <strong>AFTER</strong> the body of the request or response has been written:</p><pre>HTTP/1.1 POST /<br>Transfer-Encoding: chunked<br>Trailer: Foo<br><br>... request body ...<br><br>Foo: bar</pre><p>This feature is so niche that not many HTTP libraries support it:</p><ul><li>Python’s stdlib, or requests or httpx do not support it</li><li>Web Browser’s fetch API do not support it</li><li>…</li></ul><p>Fortunately, Golang’s stdlib net/http package does support it, and FlowG is written in Go.</p><p>We need this feature because of the way BadgerDB incremental backups work. The db.Backup() method accepts an io.Writer and a uint64 version as input. It will write the key/value pairs newer than the input version. At the end, it will return the highest seen version. But at that point, we already wrote data in the HTTP request body, we cannot send HTTP headers anymore. We can send a Trailer though.</p><h3>Conclusion</h3><p>This concludes the second part of this series of articles. The next one may come in a long time, in order to have some real world usage data to present and give a feedback on how well (or how bad) this implementation worked.</p><p>Stay tuned and follow me if you want to read more about FlowG and distributed systems in Go.</p><p>Don’t hesitate to clap for this article to give it more visibility, and star the repository on Github:</p><p><a href="https://github.com/link-society/flowg">GitHub - link-society/flowg: Low Code / Visual Scripting log processing software</a></p><p>Any contribution are welcome, be it for documentation, bug reports, feature requests, or simply discussions.</p><p>Thank you for reading me, and have a great day (or night depending on when you read this article).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=81ca31eae4db" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Distributed Systems without Raft (part 1)]]></title>
            <link>https://david-delassus.medium.com/distributed-systems-without-raft-part-1-a6b0b43db7ee?source=rss-64e2b344388d------2</link>
            <guid isPermaLink="false">https://medium.com/p/a6b0b43db7ee</guid>
            <category><![CDATA[consensus-algorithm]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[log-management-software]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[replication]]></category>
            <dc:creator><![CDATA[David Delassus]]></dc:creator>
            <pubDate>Thu, 06 Mar 2025 17:11:27 GMT</pubDate>
            <atom:updated>2025-03-07T10:53:03.975Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction and context</h3><p>For almost a year now, at I (and a few other contributors) have been working on <strong>FlowG</strong>, a low-code log management software.</p><p><a href="https://github.com/link-society/flowg/">GitHub - link-society/flowg: Low Code log management solution</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*R_0wdtR3THfK_Itb.png" /><figcaption>FlowG’s pipeline editor</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Jw_tLwtDakerlzhq.png" /><figcaption>FlowG’s log viewer</figcaption></figure><p>The project first started out of frustration. At the time, we were using <a href="https://openobserve.ai/">OpenObserve</a>. But the pipeline feature it propose was subpar, and did not allow us to do what we needed to.</p><p>For some context, we have multiple clusters, each with multiple virtual machines (obviously), running multiple Docker containers, and some of those containers are running multiple processes using another tool I made: <a href="https://github.com/linkdd/procfusion">ProcFusion</a> (like NGINX and NGINX Prometheus Exporter, and a software that pulls data from <a href="https://netboxlabs.com/docs/netbox/en/stable/">Netbox</a> and regenerates the NGINX configuration).</p><p>We needed to be able to parse the logs, categorize them, refine them, route them to different storages, and trigger some API calls based on their content.</p><p>This is exactly what <strong>FlowG</strong> proposes, using the <a href="https://vector.dev/docs/reference/vrl/">Vector Remap Language</a> (from Datadog) for scripting and <a href="https://reactflow.dev/">React Flow</a> for the pipeline editor. All in OpenSource!</p><h3>Making it distributed</h3><p>At the beginning, FlowG was a standalone server. A simple docker run in your infrastructure, and voilà.</p><p>However, some users expressed the need for replication. They need high availability, and redundancy. And FlowG introduced a <strong>S</strong>ingle <strong>P</strong>oint <strong>O</strong>f <strong>F</strong>ailure in their infrastructure.</p><p>On top of that, the Helm chart we provide to deploy FlowG in a Kubernetes cluster, alongside <a href="https://www.fluentd.org/">Fluentd</a> to gather the logs of every Pod, had to require a node name to properly configure node affinity (so that FlowG’s pod is always scheduled on the host that has the Persistent Volume, which is by default a path on the node’s host).</p><p>We need to add <strong>replication </strong>to FlowG, and the first step is to make it <strong>distributed</strong>. FlowG instances in a cluster need to know about eachother.</p><p>In that space, I heard about 3 techniques:</p><ul><li><a href="https://en.wikipedia.org/wiki/Paxos_(computer_science)">Paxos</a> protocols</li><li><a href="https://raft.github.io/">Raft</a> protocol</li><li><a href="https://en.wikipedia.org/wiki/Gossip_protocol">Gossip</a> protocols</li></ul><p>But I have to admit, I was biased towards <em>Raft</em>, as I’ve heard quite a lot about it over the years. Emphasis on “was”.</p><h3>Reminder: The CAP theorem</h3><p>When talking about distributed systems, we need to talk about <strong>consensus</strong>. How do nodes agree with each other on what the current state is?</p><p>This is similar to the <a href="https://en.wikipedia.org/wiki/Two_Generals%27_Problem">2 generals problem</a>. How to coordinate actions by communicating over an unreliable link. The link here being the network.</p><p>The network can (will?) fail, or can be slow (high latency). Yet, that’s the medium we use to communicate between nodes in a cluster.</p><p>The <a href="https://en.wikipedia.org/wiki/CAP_theorem">CAP Theorem</a> says that every distributed system can only choose 2 options out of those 3:</p><ul><li>(Strong) <strong>Consistency</strong>: every read receives the most recent write, or an error</li><li><strong>Availability:</strong> every request must succeed</li><li>(Network) <strong>Partitioning Tolerance:</strong> the system continues to work despite potential (hopefully temporary) network failures</li></ul><p>A system can then be describes as:</p><ul><li><strong>CP:</strong> strongly consistent, and resistant to network failures, but can result in unavailability</li><li><strong>AP:</strong> always available, and resistant to network failures, but can result in inconsistent responses</li><li><strong>CA:</strong> strongly consistent, always available, but can break due to network failures</li></ul><p>In reality, one <strong>has</strong> to deal with network failures. We simply can’t escape them. So the choice remain between “C” (strong consistency) and “A” (availability).</p><h3>About Raft and Paxos?</h3><p><a href="https://raft.github.io/">Raft</a> is a distributed consensus protocol. In terms of CAP theorem, it is “CP”, it favors <strong>strong consistency</strong> and <strong>partitioning tolerance</strong> over <strong>availability</strong>.</p><p><em>Raft</em> is a leader/follower protocol, it tries to elect a leader node. All writes must be done on the leader node, which means:</p><ul><li>when leader election fails, the cluster becomes unavailable</li><li>when a write is sent to a node, it must redirect it to the leader node, increasing latency</li><li>since all writes must be directed to a single node, its load increase</li><li>since all writes must have a quorum to be accepted (which implies back-and-forth between the leader and followers), performance can be degraded</li></ul><blockquote>EDIT: One thing I forgot to mention, Raft ensures that all write operation are “durable”. If a leader node accepted a write, there is a guarantee that this write will not disappear after the consensus is achieved.</blockquote><p>High Availability and Performance are key concerns for <strong>FlowG</strong>, which makes <em>Raft</em> not suited for our use case.</p><p>Similarly to <em>Raft</em>, <a href="https://en.wikipedia.org/wiki/Paxos_(computer_science)">Paxos</a> protocols are (in terms of CAP theorem) “CP”. Which means the cluster can also become unavailable.</p><blockquote>EDIT: I should clarify, the cluster can always become unavailable no matter what the software does. What I meant here is that, during a network partition, the cluster is split in 2 (or more) parts. “AP” systems will allow writes on all partitions, while “CP” systems will only allow writes on the partition that has a majority of nodes.</blockquote><h3>SWIM entered the chat</h3><p>Our last candidate is a Gossip protocol. One that caught my eye is <a href="https://en.wikipedia.org/wiki/SWIM_Protocol">SWIM</a>, it stands for <em>Scalable Weakly Consistent Infection-style Process Group Membership.</em></p><p>And there is even a Go library for it:</p><p><a href="https://github.com/hashicorp/memberlist">GitHub - hashicorp/memberlist: Golang package for gossip based membership and failure detection</a></p><p>This protocol is (in terms of the CAP theorem) “AP”. Which is what we want. We will achieve <strong>Eventual Consistency</strong> using <a href="https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type"><strong>C</strong>onflict-free <strong>R</strong>eplicated <strong>D</strong>ata <strong>T</strong>ypes</a>.</p><p>In FlowG, we use the library above over HTTP, for 3 reasons:</p><ul><li>HTTP can easily be put behind Reverse Proxies who will handle TLS for you (though we do support it natively as well)</li><li>Authentication is easy to add, we support a secret key to be passed as a header but a Reverse Proxy could add client TLS certificate authentication, Basic/Digest authentication, or anything else</li><li>Many edge infrastructures only allow HTTP(S) traffic</li></ul><p>For now it’s HTTP/1.1 for simplicity, but migration to HTTP/2 or even HTTP/3 (there are Golang packages that have feature parity with the standard library) should be smooth.</p><p>By default, this library uses a TCP (and UDP) network transport, with no TLS, and expects the exact same configuration on all nodes. We wanted flexibility (maybe some nodes use HTTPS, maybe some are on a non standard port, maybe some are behind a Reverse Proxy, etc…).</p><p>This had the consequence that all the information provided by the library to identify a node was:</p><ul><li>a name</li><li>a host &amp; port</li></ul><p>We need the actual scheme ( http:// or https:// ? ) as well, and the opportunity to add a path at the end if some Reverse Proxy are doing URL rewriting.</p><p>Thankfully, the library provides hooks to the join/leave events. In the node’s metadata (exchanged during the protocol’s ping, alive and other broadcasted events), we put the actual endpoint. This endpoint is then communicated to each node (alongside with the node’s name). And we hook into the join/leave events to maintain a local, in-memory, cluster mesh, a simple map of strings (node name) to URLs (node endpoint).</p><p>The bootstrap of a cluster then looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/962/1*_M-P9EeFDjH6Ku77-tsIug.png" /></figure><h3>The Actual Replication layer</h3><p>…is not done yet!</p><p>But, I can already share some ideas. First things first, FlowG will be <strong>eventually consistent</strong>. Every node in the cluster can receive writes (unlike <em>Raft</em>), and the replication will happen behind the scene. The cluster will converge to the same state. Examples of database that use eventual consistency are:</p><ul><li>Cassandra</li><li>DynamoDB</li><li>CouchDB</li></ul><blockquote>EDIT: DynamoDB is actually strongly consistent, but will allow readers to opt in to eventually consistent reads as a latency optimization.</blockquote><p>To achieve eventual consistency, we will rely on <a href="https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type"><strong>C</strong>onflict-free <strong>R</strong>eplicated <strong>D</strong>ata <strong>T</strong>ypes</a> (CRDTs).</p><h4>What are CRDTs?</h4><p>A simple example is a “counter”. A counter is a datatype with 2 operations:</p><ul><li>increment by X</li><li>decrement by X</li></ul><p>Two nodes receives many of those operations:</p><ul><li>Node A receives: +1, -1, +2, -1 = 1</li><li>Node B receives: -4, +2, +1, +3 = 2</li></ul><p>Then, they share with each other the history of those operations, apply them to their local state and they both <strong>converge</strong> to the same result: <strong>3</strong>.</p><p>In FlowG, are main CRDT will be an “oplog”. It has only one operation:</p><ul><li>append a timestamped write operation</li></ul><p>To converge 2 nodes, we will merge both oplog’s together and sort by timestamp.</p><p>The “oplog” describe a sequence of write operation to be done in order. Those write operations will be done on CRDTs as well, we have 3 kinds:</p><ul><li>CRUD operations in the Authentication database (where users and roles are stored)</li><li>CRUD operations in the Configuration storage (where pipelines, transformers, etc. are stored)</li><li>Append &amp; Purge operations in the Log database</li></ul><p>For the first 2, we will use the “Last Write Wins” strategy.<br>For the third one, they already converge naturally.</p><p>Once all the CRDTs are synchronized, a “diff” with the local storage will be made, and applied.</p><h4>Some considerations:</h4><p>Again, this part is not yet implemented. A lot of testing will have to be done. And some potential problems can already arise from this design, like:</p><ul><li>how big will the “oplog” be?</li><li>how fast will it be to exchange the “oplog” with other nodes?</li><li>will we need to make “snapshots” in order to reduce the “oplog” size?</li><li>…</li></ul><p>Feedback and contributions to discussions on the Github will be appreciated 🙂</p><h3>Conclusion</h3><p>This concludes the first part of this series of articles.</p><p>Stay tuned and follow me if you want to read more about FlowG and distributed systems in Go.</p><p>Don’t hesitate to clap for this article to give it more visibility, and star the repository on Github:</p><p><a href="https://github.com/link-society/flowg">GitHub - link-society/flowg: Low Code / Visual Scripting log processing software</a></p><p>Any contribution are welcome, be it for documentation, bug reports, feature requests, or simply discussions.</p><p>Thank you for reading me, and have a great day (or night depending on when you read this article).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a6b0b43db7ee" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Trying out SDL3 by writing a C++ Game Engine]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://david-delassus.medium.com/trying-out-sdl3-by-writing-a-c-game-engine-c9bb13156b74?source=rss-64e2b344388d------2"><img src="https://cdn-images-1.medium.com/max/1925/0*ngsI602Rija30BYW.png" width="1925"></a></p><p class="medium-feed-snippet">SDL3 is around the corner! SDL3_image has been released, and we&#x2019;re waiting for SDL3_ttf, SDL3_mixer and SDL3_net to be released soon as&#x2026;</p><p class="medium-feed-link"><a href="https://david-delassus.medium.com/trying-out-sdl3-by-writing-a-c-game-engine-c9bb13156b74?source=rss-64e2b344388d------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://david-delassus.medium.com/trying-out-sdl3-by-writing-a-c-game-engine-c9bb13156b74?source=rss-64e2b344388d------2</link>
            <guid isPermaLink="false">https://medium.com/p/c9bb13156b74</guid>
            <category><![CDATA[c-plus-plus-language]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[game-engine]]></category>
            <category><![CDATA[indie-game-development]]></category>
            <category><![CDATA[game-development]]></category>
            <dc:creator><![CDATA[David Delassus]]></dc:creator>
            <pubDate>Thu, 27 Feb 2025 08:20:14 GMT</pubDate>
            <atom:updated>2025-02-27T08:20:14.085Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Unity-like coroutines in plain old C]]></title>
            <link>https://david-delassus.medium.com/unity-like-coroutines-in-plain-old-c-d16bcb90388c?source=rss-64e2b344388d------2</link>
            <guid isPermaLink="false">https://medium.com/p/d16bcb90388c</guid>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[indie-game-development]]></category>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[c-programming-language]]></category>
            <dc:creator><![CDATA[David Delassus]]></dc:creator>
            <pubDate>Sat, 13 Jul 2024 17:08:23 GMT</pubDate>
            <atom:updated>2024-07-14T13:19:37.761Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*7_vsBFS7BQCwfMciuc_COQ.png" /></figure><p>Let’s just begin with this: Coroutines are great!</p><p>For those who don’t know what coroutines are, let me explain:</p><p>Coroutines allow a function to be paused and resumed. This is often used to allow the concurrent execution of multiple functions (and is often what is used to implement async / await in most programming languages), but in Game Development, they are also used to spread a long running function over multiple frames.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WO9rZHpL7gzktX0M1KVBmg.gif" /></figure><p>In the above screencast, clicking on the button “DAMAGE” sets the red bar (representing health) to a specific value, and triggers a coroutine that changes the value of the orange bar progressively over time.</p><p>In this article, we’ll see how to implement this in C.</p><h3>In-depth explanation of coroutines</h3><p><a href="https://en.wikipedia.org/wiki/Coroutine">Coroutine - Wikipedia</a></p><p>At its core, a coroutine can be modeled as a state machine:</p><pre>typedef enum {<br>    CORO_SUSPENDED,<br>    CORO_COMPLETED<br>} coro_state;<br><br>coro_state my_coroutine(void) {<br>    static int state = 0;<br><br>    switch (state) {<br>        case 0:<br>            printf(&quot;Step 1\n&quot;);<br>            state = 1;<br>            return CORO_SUSPENDED;<br><br>        case 1:<br>            printf(&quot;Step 2\n&quot;);<br>            state = 2;<br>            return CORO_SUSPENDED;<br><br>        case 2:<br>        default:<br>            return CORO_COMPLETED;<br>    }<br>}<br><br>int main(void) {<br>    while (my_coroutine() != CORO_COMPLETED) {}<br><br>    return 0;<br>}</pre><p>There are 2 kinds of coroutines:</p><ul><li><strong>stackful:</strong> each coroutine has its own call stack independent from the main thread</li><li><strong>stackless:</strong> each coroutine shares its call stack with the main thread</li></ul><p><strong>NB: </strong>The above example is a <strong>stackless</strong> coroutine.</p><p>The main advantage of stackful coroutines is that they can easily handle more complex control flows like recursion. However, they do use more memory and can have an impact on performance. They can be implemented using setjmp / longjmp. Therefore, “yielding”/pausing a coroutine can be done wherever in the call stack:</p><pre>void f(void) {<br>    coroutine_yield();<br>    printf(&quot;Step 3\n&quot;); // will resume here<br>}<br><br>void my_coroutine() {<br>    printf(&quot;Step 1\n&quot;);<br>    coroutine_yield();<br><br>    printf(&quot;Step 2\n&quot;);<br>    f(); // will yield<br>}</pre><p>A stackless coroutine however, is more lightweight and a great option when performance is required. They are well fit for simple tasks, but cannot handle the complex control flows that stackful coroutines allow. If the above example were to be recursive, the explicit handling of the coroutine’s state can become hard to follow quite quickly.</p><h4>Coroutines in other languages</h4><p>C++20 introduced stackless coroutines:</p><p><a href="https://en.cppreference.com/w/cpp/language/coroutines">Coroutines (C++20)</a></p><p>In Unity, coroutines are also stackless, and even transformed into state machines. They are resumed each frame:</p><p><a href="https://docs.unity3d.com/Manual/Coroutines.html">Coroutines</a></p><p>However, Lua coroutines are stackful:</p><p><a href="https://www.lua.org/pil/9.1.html">Programming in Lua : 9.1</a></p><h4>Asynchronous tasks, generators, and… Unity-like?</h4><p>You may have seen:</p><pre>async Task DoSomething() {<br>    await DoSomethingElse();<br>    await DoSomethingElse();<br>}<br><br>void Example() {<br>    var task = DoSomething(); // the task in run in a separate thread<br>    while (!task.IsCompleted) {<br>        Sleep(1);<br>    }<br>}</pre><p>This is a C# example of an asynchronous task. A special kind of coroutine which is to be executed in a separate thread (in parallel). The await keyword is responsible for pausing the coroutine.</p><p>We also have generators, which can be used to create iterators:</p><pre>IEnumerable&lt;int&gt; Example() {<br>    for (int i = 1; i &lt;= 10; i++) {<br>        yield return i;<br>    }<br>}</pre><p>The yield return statement is responsible for pausing the coroutine, it is also used to send back data to the caller. This is especially useful to implement infinite lists for example:</p><pre>IEnumerable&lt;int&gt; Example() {<br>    while (true) {<br>        yield return RandomInteger();<br>    }<br>}</pre><p>Now, what do I mean by <strong>Unity-like</strong>? What is so special about Unity coroutines? Aren’t they just C# generators/iterators?</p><p>Well, yes they are. But the exposed API allow to yield another coroutine, which will pause the current coroutine until the yielded one completed!</p><pre>IEnumerable&lt;int&gt; Do123() {<br>    yield return 1;<br>    yield return 2;<br>    yield return 3;<br>}<br><br>IEnumerable&lt;int&gt; Do456() {<br>    yield return 4;<br>    yield return 5;<br>    yield return 6;<br>}<br><br>IEnumerable&lt;int&gt; Do123456() {<br>    yield return Do123();<br>    yield return Do456();<br>}</pre><p>This API is incredibly useful in Game Development, this allows you to reuse simple coroutines to build more complex behaviors:</p><pre>IEnumerable Wait(float seconds) {<br>    while (seconds &gt; 0f) {<br>        seconds -= Time.deltaTime;<br>        yield return null;<br>    }<br>}<br><br>IEnumerable MoveTowards(Vector3 target) {<br>    while(DistanceBetween(transform.position, target) &gt; someThreshold) {<br>        yield return null;<br>    }<br>}<br><br>IEnumerable WaitAndMove() {<br>    yield return Wait(5);<br>    yield return MoveTowards(someTarget);<br>}</pre><h4>For the C++ developers</h4><p>I already implemented such API in my C++ game framework using C++20 coroutines:</p><p><a href="https://github.com/trollworks/sdk-core#coroutines">GitHub - trollworks/sdk-core: Core SDK of Trollworks engine</a></p><p>So if you are not interested by the C implementation, you can skip the rest of the article.</p><h3>Coroutines in C, where to start?</h3><p><a href="https://github.com/edubart/minicoro">GitHub - edubart/minicoro: Single header stackful cross-platform coroutine library in pure C.</a></p><p><strong>minicoro</strong> is a single-file library inspired by Lua coroutines, it is mainly used as a backend for the <a href="https://github.com/edubart/nelua-lang">Nelua</a> programming language. As such, they are <strong>stackful</strong>.</p><p>Not only it is easy to integrate in your own project, it also supports custom allocators, which is great!</p><p>This is what we will use to build a Unity-like API on top.</p><h4>A small detour: a custom allocator</h4><p>One thing I consider best practice when writing a C library, is to define an allocator interface, so that the user of your library can plug in his own allocator.</p><p>I take a lot of inspiration from this article, which I highly recommend you read before continuing:</p><p><a href="https://nullprogram.com/blog/2023/12/17/">So you want custom allocator support in your C library</a></p><p>As such, here is the interface that I often use:</p><pre>#include &lt;assert.h&gt;<br>#include &lt;stdint.h&gt;<br>#include &lt;string.h&gt;<br><br>typedef struct allocator allocator;<br>struct allocator {<br>    void *(*alloc)(size_t sz, void *ctx);<br>    void *(*realloc)(void *ptr, size_t oldsz, size_t newsz, void *ctx);<br>    void (*dealloc)(void *ptr, size_t sz, void *ctx);<br>    void *ctx;<br>};<br><br>void *allocator_alloc(allocator *self, size_t sz) {<br>    assert(self != NULL);<br>    assert(self-&gt;alloc != NULL);<br><br>    return self-&gt;alloc(sz, self-&gt;ctx);<br>}<br><br>void *allocator_calloc(allocator *self, size_t nmemb, size_t membsz) {<br>    if (nmemb &gt; 0 &amp;&amp; membsz &gt; SIZE_MAX / nmemb) {<br>        // integer overflow, abort<br>        return NULL;<br>    }<br><br>    void *ptr = allocator_alloc(self, nmemb * membsz);<br>    if (ptr != NULL) {<br>        memset(ptr, 0, nmemb * membsz);<br>    }<br>    return ptr;<br>}<br><br>void *allocator_realloc(allocator *self, void *ptr, size_t oldsz, size_t newsz) {<br>    assert(self != NULL);<br>    assert(self-&gt;realloc != NULL);<br><br>    return self-&gt;realloc(ptr, oldsz, newsz, self-&gt;ctx);<br>}<br><br><br>void allocator_dealloc(allocator *self, void *ptr, size_t sz) {<br>    assert(self != NULL);<br>    assert(self-&gt;dealloc != NULL);<br><br>    return self-&gt;dealloc(ptr, sz, self-&gt;ctx);<br>}</pre><p>If you wish to use the stdlib allocation functions, this can then easily be done with the following code:</p><pre>#include &lt;stdlib.h&gt;<br><br>static void *_malloc(size_t sz, void *ctx) {<br>    (void)ctx;<br><br>    return malloc(sz);<br>}<br><br>static void *_realloc(void *ptr, size_t oldsz, size_t newsz, void *ctx) {<br>    (void)ctx;<br>    (void)oldsz;<br><br>    return realloc(ptr, newsz);<br>}<br><br>static void _free(void *ptr, size_t sz, void *ctx) {<br>    (void)ctx;<br>    (void)sz;<br><br>    free(ptr);<br>}<br><br>void allocator_init_stdlib(allocator *self) {<br>    assert(self != NULL);<br><br>    self-&gt;alloc   = _malloc;<br>    self-&gt;realloc = _realloc;<br>    self-&gt;dealloc = _free;<br>    self-&gt;ctx     = NULL;<br>}</pre><p>Which can then be used:</p><pre>allocator a = {0};<br>allocator_init_stdlib(&amp;a);</pre><p>Parenthesis closed, let’s go back to our coroutines.</p><h3>A coroutine manager</h3><p>What we actually want is the ability to create coroutines and schedule them for execution each frame of our game loop (until they are done).</p><p>To do this, we need a data structure to keep track of the scheduled coroutines. Because our coroutines can be nested (by yielding a new coroutine), this data structure is a tree.</p><p>Let’s define a few types:</p><pre>typedef struct coroutine_manager coroutine_manager;<br>typedef struct coroutine coroutine;<br>typedef struct coroutine_udata coroutine_udata;<br>typedef void (*coroutine_fn)(coroutine *coro);</pre><p>The first one represent our manager that will keep track of our scheduled coroutines.<br>The second one represent a wrapper to the <strong>minicoro</strong> coroutine structure, it will contain:</p><ul><li>the underlying minicoro coroutine</li><li>the user data passed to it</li><li>a reference to the coroutine manager, and the allocator</li><li>links to the siblings and parent coroutines in the tree</li></ul><p>Here is the definition:</p><pre>/// Structure passed as userdata to the underlying coroutine<br>struct coroutine_udata {<br>    coroutine *self;<br>    coroutine_fn fn;<br>};<br><br>struct coroutine {<br>    /// copy of the initial data passed as argument to the coroutine<br>    void *arg;<br>    size_t argsz;<br><br>    allocator *allocator;<br>    coroutine_manager *mgr;<br><br>    coroutine_udata udata;<br>    mco_desc desc; /// The underlying coroutine descriptor<br>    mco_coro *co;  /// The actual underlying coroutine<br><br>    // tree structure<br>    coroutine *parent;<br>    struct {<br>        coroutine *prev;<br>        coroutine *next;<br>    } siblings;<br>};<br><br>struct coroutine_manager {<br>    allocator *allocator;<br><br>    struct {<br>        coroutine *head;<br>        coroutine *tail;<br>    } coroutines;<br>};</pre><p>The API of a coroutine structure is quite simple: a constructor, a destructor, and a wrapper to the <strong>minicoro</strong> yield function.</p><p>The constructor takes as input our coroutine implementation, and the address of initial data to pass as argument to the coroutine function. It must allocate the coroutine structure, and the memory to store the initial data. Then it must initialize the underlying coroutine with our wrapper implementation:</p><pre>// wrapper coroutine implementation<br>static void _coro_task(mco_coro *co) {<br>    coroutine_udata *udata = mco_get_user_data(co);<br>    udata-&gt;fn(udata-&gt;self); // calls the coroutine_fn function<br>}<br><br>coroutine *coroutine_new(<br>    coroutine_manager *mgr,<br>    coroutine_fn fn,<br>    void *arg,<br>    size_t argsz<br>) {<br>    assert(self != NULL);<br>    assert(fn != NULL);<br><br>    coroutine *coro = allocator_alloc(mgr-&gt;allocator, sizeof(coroutine));<br>    if (coro == NULL) {<br>        // allocation failed<br>        return NULL;<br>    }<br><br>    if (arg != NULL) {<br>        assert(argsz &gt; 0);<br><br>        coro-&gt;arg = allocator_alloc(mgr-&gt;allocator, argsz);<br>        if (coro-&gt;arg == NULL) {<br>            // allocation failed<br>            allocator_dealloc(mgr-&gt;allocator, coro, sizeof(coroutine));<br>            return NULL;<br>        }<br><br>        memcpy(coro-&gt;arg, arg, argsz);<br>        coro-&gt;argsz = argsz;<br>    }<br>    else {<br>        coro-&gt;arg   = NULL;<br>        coro-&gt;argsz = 0;<br>    }<br><br>    coro-&gt;allocator = mgr-&gt;allocator;<br>    coro-&gt;mgr       = mgr;<br><br>    coro-&gt;udata.self = coro;<br>    coro-&gt;udata.fn   = fn;<br><br>    coro-&gt;desc           = mco_desc_init(_coro_task, 0);<br>    coro-&gt;desc.user_data = &amp;coro-&gt;udata;<br><br>    coro-&gt;parent        = NULL;<br>    coro-&gt;siblings.next = NULL;<br>    coro-&gt;siblings.prev = NULL;<br><br>    mco_result res = mco_create(&amp;coro-&gt;co, &amp;coro-&gt;desc);<br>    if (res != MCO_SUCCESS) {<br>        // coroutine creation failed<br><br>        if (coro-&gt;arg != NULL) {<br>            allocator_dealloc(mgr-&gt;allocator, coro-&gt;arg, coro-&gt;argsz);<br>        }<br><br>        allocator_dealloc(mgr-&gt;allocator, coro, sizeof(coroutine));<br>        return NULL;<br>    }<br><br>    return coro;<br>}</pre><p><strong>NB:</strong> It’s worth mentioning that minicoro also provides an interface to override the allocation functions. We can plug in our allocator:</p><pre>static void *_mco_alloc(size_t size, void *udata) {<br>    assert(udata != NULL);<br>    return allocator_alloc((allocator *)udata, size);<br>}<br><br>static void *_mco_dealloc(void *ptr, size_t size, void *udata) {<br>    assert(udata != NULL);<br>    allocator_dealloc((allocator *)udata, ptr, size);<br>}<br><br>coroutine *coroutine_new(coroutine_manager *mgr,<br>    coroutine_fn fn,<br>    void *arg,<br>    size_t argsz<br>) {<br>    // ...<br><br>    coro-&gt;desc                = mco_desc_init(_coro_task, 0);<br>    coro-&gt;desc.alloc_cb       = _mco_alloc;<br>    coro-&gt;desc.dealloc_cb     = _mco_dealloc;<br>    coro-&gt;desc.allocator_data = mgr-&gt;allocator;<br>    coro-&gt;desc.user_data      = &amp;coro-&gt;udata;<br><br>    // ...<br>}</pre><p>The desctructor is quite straightforward, simply free the memory:</p><pre>void coroutine_free(coroutine *self) {<br>    assert(self != NULL);<br><br>    mco_destroy(self-&gt;co);<br><br>    if (self-&gt;arg != NULL) {<br>        allocator_dealloc(self-&gt;allocator, self-&gt;arg, self-&gt;argsz);<br>    }<br><br>    allocator_dealloc(self-&gt;allocator, self, sizeof(coroutine));<br>}</pre><p>And finally, the wrapper to yield. The minicoro library provides a LIFO data structure to exchange data between the coroutine and the manager. We can push data on this structure right before we yield, so that the manager can pop it. This is where we will push the address of the nested coroutine to run:</p><pre>void coroutine_yield(coroutine *self, coroutine *nested) {<br>    assert(self != NULL);<br><br>    mco_push(self-&gt;co, &amp;nested, sizeof(coroutine*));<br>    mco_yield(self-&gt;co);<br>}</pre><p>Which will be used like this:</p><pre>void mycoro(coroutine *coro) {<br>    // C#: yield return null<br>    coroutine_yield(coro, NULL);<br><br>    // C#: yield return OtherCoroutine()<br>    coroutine *other = coroutine_new(coro-&gt;mgr, other_coroutine_fn, NULL, 0);<br>    // ...error handling...<br>    coroutine_yield(coro, other);<br>}</pre><p>The coroutine API being done, we are now missing the coroutine manager scheduling API. Which consist of 2 functions:</p><ul><li>schedule : to add a coroutine to the list of scheduled coroutines</li><li>unschedule : to remove a coroutine from that list</li></ul><p>It’s a classic doubly-linked list implementation, so let’s get to it without commentary:</p><pre>void coroutine_manager_schedule(coroutine_manager *self, coroutine *coro) {<br>    assert(self != NULL);<br>    assert(coro != NULL);<br><br>    if (self-&gt;coroutines.head == NULL) {<br>        self-&gt;coroutines.head = coro;<br>        self-&gt;coroutines.tail = coro;<br>    }<br>    else {<br>        self-&gt;croutines.tail-&gt;siblings.next = coro;<br>        coro-&gt;siblings.prev                 = self-&gt;coroutines.tail;<br>        self-&gt;coroutines.tail               = coro;<br>    }<br>}<br><br>void coroutine_manager_unschedule(coroutine_manager *self, coroutine *coro) {<br>    assert(self != NULL);<br>    assert(coro != NULL);<br><br>    if (coro-&gt;siblings.prev != NULL) {<br>        coro-&gt;siblings.prev-&gt;siblings.next = coro-&gt;siblings.next;<br>    }<br>    else {<br>        self-&gt;coroutines.head = coro-&gt;siblings.next;<br>    }<br><br>    if (coro-&gt;siblings.next != NULL) {<br>        coro-&gt;siblings.next-&gt;siblings.prev = coro-&gt;siblings.prev;<br>    }<br>    else {<br>        self-&gt;coroutines.tail = coro-&gt;siblings.prev;<br>    }<br>}</pre><p>The final part is the API to run all the scheduled coroutines until they are done. We do this by iterating through the list in the manager, resuming the coroutine. If the coroutine is still suspended, we pop from its LIFO data structure. If we find a NULL pointer, we do nothing, otherwise, it’s a pointer to a new coroutine that must run until completion before resuming the old one. So we will unschedule the old one and schedule the new one. However, if the coroutine is completed (or “dead”), we simply unschedule it and free it. If it had a parent, we reschedule the parent:</p><pre>void coroutine_manager_run(coroutine_manager *self) {<br>    assert(self != NULL);<br><br>    coroutine *coro = self-&gt;coroutines.head;<br>    while (coro != NULL) {<br>        // we are going to modify the iterator or even free it<br>        // so, let&#39;s save the reference to the next item right away<br>        coroutine *next = coro-&gt;siblings.next;<br><br>        // resume the underlying coroutine and fetch its status<br>        mco_resume(coro-&gt;co);<br>        mco_state state = mco_status(coro-&gt;co);<br><br>        if (state == MCO_SUSPENDED) {<br>            // the coroutine yielded, fetch the value pushed on the LIFO<br>            coroutine *nested = NULL;<br>            mco_pop(coro-&gt;co, &amp;nested, sizeof(coroutine*));<br><br>            // if there is a new coroutine to run, schedule it<br>            if (nested != NULL) {<br>                nested-&gt;parent = coro; // so that we can reschedule it once completed<br>                coroutine_manager_unschedule(self, coro);<br>                coroutine_manager_schedule(self, nested);<br>            }<br>        }<br>        else if (state == MCO_DEAD) {<br>            // the coroutine completed, let&#39;s reschedule its parent<br>            if (coro-&gt;parent != NULL) {<br>                coroutine_manager_schedule(self, coro-&gt;parent);<br>            }<br><br>            // and unschedule this one, and destroy it<br>            coroutine_manager_unschedule(self, coro);<br>            coroutine_free(coro);<br>        }<br><br>        coro = next;<br>    }<br>}</pre><p>Our coroutine manager is now fully implemented 🙂 Thanks <strong>minicoro</strong> for the heavy lifting.</p><h3>A simple example</h3><p>Let’s write a coroutine that yields until some time has passed (using SDL):</p><pre>#include &lt;SDL.h&gt;<br><br>static void _wait_for_fn(coroutine *coro) {<br>    float seconds = *(float *)coro-&gt;arg;<br><br>    Uint32 start = SDL_GetTicks();<br><br>    while (((float)(SDL_GetTicks() - start) / 1000.0f) &lt; seconds) {<br>        coroutine_yield(coro, NULL);<br>    }<br>}<br><br>coroutine *wait_for(coroutine_manager *mgr, float seconds) {<br>    return coroutine_new(mgr, _wait_for_fn, &amp;seconds, sizeof(float));<br>}    </pre><p>Then, a coroutine to decrease the orange bar (represented by the health_dirty value) until it matches the red bar (represented by the health_real value):</p><pre>static void _update_health_dirty_fn(coroutine *coro) {<br>    player *p = *((player **)coro-&gt;arg);<br><br>    while (true) {<br>        p-&gt;health_dirty = lerp(p-&gt;health_dirty, p-&gt;health, 0.1f);<br>        if (fabsf(p-&gt;health - p-&gt;health_dirty) &lt; 0.0001f) {<br>            p-&gt;health_dirty = p-&gt;health;<br>        }<br><br>        if (p-&gt;health != p-&gt;health_dirty) {<br>            coroutine_yield(coro, NULL);<br>        }<br>        else {<br>            break;<br>        }<br>    }<br>}<br><br>coroutine *update_health_dirty(coroutine_manager *mgr, player *p) {<br>    return coroutine_new(mgr, _update_health_dirty_fn, &amp;p, sizeof(player*));<br>}</pre><p>And finally, the coroutine that waits a bit before updating the health bar:</p><pre>static void _update_health_bar_fn(coroutine *coro) {<br>    player *p = *((player **)coro-&gt;arg);<br><br>    coroutine_yield(coro, wait_for(coro-&gt;mgr, 0.5f /* seconds */));<br>    coroutine_yield(coro, update_health_dirty(coro-&gt;mgr, p));<br>}<br><br>coroutine *update_health_bar(coroutine_manager *mgr, player *p) {<br>    return coroutine_new(mgr, _update_health_bar_fn, &amp;p, sizeof(player*));<br>}</pre><p>Now, in our game loop:</p><pre>while (true) {<br>    // ...input...<br><br>    // ...game logic...<br>    if (damage_taken) {<br>        coroutine_manager_schedule(<br>            &amp;coro_mgr,<br>            update_health_bar(&amp;coro_mgr, &amp;player_data)<br>        );<br>    }<br><br>    coroutine_manager_run(&amp;coro_mgr);<br><br>    // ...rendering...<br>}</pre><p>And the final result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WO9rZHpL7gzktX0M1KVBmg.gif" /></figure><h3>Conclusion</h3><p>Coroutines are a great tool in game development, be it for polishing (like in our example), or implementing complex behavior over time.</p><p>And since <strong>minicoro</strong> is inspired by Lua coroutines, that means the code above can be ported to Lua quite easily, and therefore you can benefit from it with game frameworks like <a href="https://love2d.org/">LÖVE</a>.</p><p>Consider the code above free of use (CC0, public domain, WTFPL, whichever you like), and have fun in your game development journey 🙂</p><p>Feel free to clap 👏 for this article to give it more visibility.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d16bcb90388c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Writing Python like it’s Elixir (or Erlang)]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://david-delassus.medium.com/writing-python-like-its-elixir-or-erlang-d8e15e6d1936?source=rss-64e2b344388d------2"><img src="https://cdn-images-1.medium.com/max/875/0*YDbAE7z92rAutopv.png" width="875"></a></p><p class="medium-feed-snippet">The OTP framework, the most wonderful toolbox of Erlang/Elixir, in Python, is it possible?</p><p class="medium-feed-link"><a href="https://david-delassus.medium.com/writing-python-like-its-elixir-or-erlang-d8e15e6d1936?source=rss-64e2b344388d------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://david-delassus.medium.com/writing-python-like-its-elixir-or-erlang-d8e15e6d1936?source=rss-64e2b344388d------2</link>
            <guid isPermaLink="false">https://medium.com/p/d8e15e6d1936</guid>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[erlang]]></category>
            <category><![CDATA[design-patterns]]></category>
            <dc:creator><![CDATA[David Delassus]]></dc:creator>
            <pubDate>Sun, 11 Feb 2024 18:37:57 GMT</pubDate>
            <atom:updated>2024-02-11T18:37:57.297Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Letlang specification finalized!]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://david-delassus.medium.com/letlang-specification-finalized-ea55fce4bbbe?source=rss-64e2b344388d------2"><img src="https://cdn-images-1.medium.com/max/855/1*FvOKbPEQF2dekpLnz9jKlg.png" width="855"></a></p><p class="medium-feed-snippet">After a lot of work, a specification describing Letlang&#x2019;s intended behavior and semantics has finally been completed!</p><p class="medium-feed-link"><a href="https://david-delassus.medium.com/letlang-specification-finalized-ea55fce4bbbe?source=rss-64e2b344388d------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://david-delassus.medium.com/letlang-specification-finalized-ea55fce4bbbe?source=rss-64e2b344388d------2</link>
            <guid isPermaLink="false">https://medium.com/p/ea55fce4bbbe</guid>
            <category><![CDATA[programming-languages]]></category>
            <category><![CDATA[compiler-design]]></category>
            <category><![CDATA[letlang]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[rust-programming-language]]></category>
            <dc:creator><![CDATA[David Delassus]]></dc:creator>
            <pubDate>Sun, 04 Feb 2024 13:12:50 GMT</pubDate>
            <atom:updated>2024-02-04T13:12:50.255Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[That’s it! I’m making my own C++ package manager…]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://david-delassus.medium.com/thats-it-i-m-making-my-own-c-package-manager-555eecbf7d2e?source=rss-64e2b344388d------2"><img src="https://cdn-images-1.medium.com/max/600/0*ulWWudbqTW2WJqyL.png" width="600"></a></p><p class="medium-feed-snippet">A deadly simple package manager for your C/C++ projects, written in Rust.</p><p class="medium-feed-link"><a href="https://david-delassus.medium.com/thats-it-i-m-making-my-own-c-package-manager-555eecbf7d2e?source=rss-64e2b344388d------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://david-delassus.medium.com/thats-it-i-m-making-my-own-c-package-manager-555eecbf7d2e?source=rss-64e2b344388d------2</link>
            <guid isPermaLink="false">https://medium.com/p/555eecbf7d2e</guid>
            <category><![CDATA[open-source]]></category>
            <category><![CDATA[rust-programming-language]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[c-plus-plus-language]]></category>
            <category><![CDATA[build-system]]></category>
            <dc:creator><![CDATA[David Delassus]]></dc:creator>
            <pubDate>Thu, 25 Jan 2024 21:13:27 GMT</pubDate>
            <atom:updated>2024-01-25T21:13:27.694Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[AI Toolkit — Give a brain to your NPCs, a header-only C++ library]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://david-delassus.medium.com/ai-toolkit-give-a-brain-to-your-npcs-a-header-only-c-library-02a50ae9faed?source=rss-64e2b344388d------2"><img src="https://cdn-images-1.medium.com/max/600/0*Ima4HAKwUzAewMV4.png" width="600"></a></p><p class="medium-feed-snippet">Second edition of me battling my procrastination. This time, it is a project I made 1 or 2 years ago that I decided to publish. I was&#x2026;</p><p class="medium-feed-link"><a href="https://david-delassus.medium.com/ai-toolkit-give-a-brain-to-your-npcs-a-header-only-c-library-02a50ae9faed?source=rss-64e2b344388d------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://david-delassus.medium.com/ai-toolkit-give-a-brain-to-your-npcs-a-header-only-c-library-02a50ae9faed?source=rss-64e2b344388d------2</link>
            <guid isPermaLink="false">https://medium.com/p/02a50ae9faed</guid>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[indiedev]]></category>
            <dc:creator><![CDATA[David Delassus]]></dc:creator>
            <pubDate>Tue, 09 Jan 2024 12:55:23 GMT</pubDate>
            <atom:updated>2024-01-09T12:55:23.074Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Yet another static site generator, but for language designers]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://david-delassus.medium.com/yet-another-static-site-generator-but-for-language-designers-a7d8cdf09aa5?source=rss-64e2b344388d------2"><img src="https://cdn-images-1.medium.com/max/1000/0*76lpoISqcyJjTFMI.png" width="1000"></a></p><p class="medium-feed-snippet">You are a language designer, you are working on your own programming language, maybe because you had an innovative idea, or maybe you want&#x2026;</p><p class="medium-feed-link"><a href="https://david-delassus.medium.com/yet-another-static-site-generator-but-for-language-designers-a7d8cdf09aa5?source=rss-64e2b344388d------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://david-delassus.medium.com/yet-another-static-site-generator-but-for-language-designers-a7d8cdf09aa5?source=rss-64e2b344388d------2</link>
            <guid isPermaLink="false">https://medium.com/p/a7d8cdf09aa5</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[letlang]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[programming-languages]]></category>
            <category><![CDATA[static-site-generator]]></category>
            <dc:creator><![CDATA[David Delassus]]></dc:creator>
            <pubDate>Tue, 02 Jan 2024 11:42:24 GMT</pubDate>
            <atom:updated>2024-01-02T22:08:28.283Z</atom:updated>
        </item>
    </channel>
</rss>