Skip to content

Instantly share code, notes, and snippets.

struct NullType
{
};
template <typename... Ts>
struct TypeList;
template <typename Head, typename... Tail>
struct TypeList<Head, Tail...>
{
// *************** behavior_scatter_hierarchy.h ***************
template <class T, class U>
struct TypeList
{
using Head = T;
using Tail = U;
};
struct NullType
➜ python python3 nxnrecurse.py
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
➜ python vi nxnrecurse.py
➜ python cat nxnrecurse.py
@amencke
amencke / threadsafe_connpool.py
Last active October 23, 2021 21:47
threadsafe connection pool implementation
import asyncio
import concurrent.futures
import threading
import uuid
from timeit import default_timer as timer
class NoConnectionAvailableError(Exception):
def __init__(self, msg):
for i in {1..5}; do
VAL1=$((i % 2));
VAL2=$(( ($VAL1 + 1) % 2));
curl -H "Content-Type: application/json" \
-X POST \
-d '{"userID":'"${VAL1}"',"message":"Hello user '"${VAL1}"' from user '"${VAL2}"'!"}' \
http://localhost:8080/v1/conversation/123;
echo;
done
case PostMessage(conversationID, sender, message) =>
context.log.info(
s"Received a command: PostMessage(conversationID=${conversationID}, sender=${sender}, message=${message})"
)
context.log.info(
s"Publishing domain event: MessagePosted(conversationID=${conversationID}, sender=${sender}, message=${message})"
)
Effect.persist(MessagePosted(conversationID, sender, message))
def apply(conversationID: ConversationID): Behavior[SessionCommand] =
Behaviors.setup[SessionCommand] { context =>
EventSourcedBehavior[SessionCommand, SessionEvent, ConversationInMemoryState](
persistenceId = PersistenceId.ofUniqueId(conversationID.toString()),
emptyState = ConversationInMemoryState(
mutable.Map[ConversationID, List[(UserID, String)]]()
),
commandHandler = { (state, command) => commandHandler(state, command, context) },
eventHandler = { (state, event) => eventHandler(state, event, context) }
)
override def onMessage(msg: SessionCommand): Behavior[SessionCommand] =
msg match {
case PostMessage(conversationID, sender, message) =>
context.log.info(s"${sender} posted to conversation ${conversationID}: ${message}")
persistentEventSourcedActor ! PostMessage(conversationID, sender, message)
idle()
case GetHistory(conversationID, requester, replyTo) =>
context.log.info(s"Retrieving history of conversation ${conversationID} for ${requester}")
val maybeHistory: Future[ConversationHistory] =
persistentEventSourcedActor.ask(GetHistory(conversationID, requester, _))
case SessionTimeout(conversationID) =>
sessions -= conversationID // remove session from the cache
context.log.info(s"Session timed out for conversation ${conversationID}")
this
case SessionTimeout(conversationID) =>
sessions -= conversationID // remove session from the cache
context.log.info(s"Session timed out for conversation ${conversationID}")
this