Skip to content

Instantly share code, notes, and snippets.

@toksdotdev
toksdotdev / vesper-dark.json
Created February 12, 2024 23:44 — forked from bdsqqq/vesper-dark.json
Vesper theme for zed.dev
{
"$schema": "https://zed.dev/schema/themes/v0.1.0.json",
"name": "Vesper",
"author": "Rauno Freiberg",
"themes": [
{
"name": "Vesper",
"appearance": "dark",
"style": {
"border": "#101010",
use super::RoundRobinStrategy;
use crate::balancer::StrategyProducer;
use crate::Target;
use futures::future::abortable;
use futures::stream::AbortHandle;
use futures::stream::Abortable;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::sync::Arc;
@toksdotdev
toksdotdev / why-size-of-option-t-is-doubled.md
Last active May 9, 2022 16:21
An explanation of why the size of Option<T> is always often double the size of T

Someone asked a question some weeks back about why size_of::<Option<T>> is always double. Answer is because of alignment.

Explanation

How are Rust enum represented in C?

C doesn't have the ability to directly represent complex Rust enum, hence, the need for a workaround. To understand that, let's take a look at how Option<i32> is represnted in C.

E.g. Given:

@toksdotdev
toksdotdev / assert_nom_locate.rs
Last active March 28, 2022 13:35
Simple macro to assert on nom parser and nom_locate result
/// Perform assertion on a location span type.
macro_rules! assert_span {
($actual: expr, line = $value: expr) => {
assert_eq!($actual.location_line(), $value);
};
($actual: expr, fragment = $value: expr) => {
assert_eq!(*$actual.fragment(), $value);
};
($actual: expr, column = $value: expr) => {
assert_eq!($actual.get_column(), $value);
@toksdotdev
toksdotdev / actix-run-async-as-sync.rs
Last active December 9, 2021 14:06
Run an async block of code synchronously in the current thread
use std::future::Future;
use actix_rt::Runtime;
/// Runs the provided future, blocking the current thread until the future completes.
///
/// A good use case for this would be initialising
pub fn run_sync<F>(future: F) -> F::Output
where
F: Future,
@toksdotdev
toksdotdev / k8-utils.sh
Last active July 30, 2021 10:18
Useful Kubernetes utils
# Scale deployments
DEPLOYMENT=traefik
NAMESPACE=kube-system
kubectl scale deploy $DEPLOYMENT --replicas=0 -n $NAMESPACE
# Fetch k8s secret for any namespace (you must be authorized to fetch the secet)
SERVICE_NAME=whoami
NAMESPACE=kube-system
  • What do Etcd, Consul, and Zookeeper do?
    • Service Registration:
      • Host, port number, and sometimes authentication credentials, protocols, versions numbers, and/or environment details.
    • Service Discovery:
      • Ability for client application to query the central registry to learn of service location.
    • Consistent and durable general-purpose K/V store across distributed system.
      • Some solutions support this better than others.
      • Based on Paxos or some derivative (i.e. Raft) algorithm to quickly converge to a consistent state.
  • Centralized locking can be based on this K/V store.
@toksdotdev
toksdotdev / port-forward-k8-pod.sh
Created December 10, 2020 17:59
Port forward k8 pod to local machine
NAMESPACE=production
POD_LABEL=app=core-service
HOST_PORT=8080
APP_PORT=3333
kubectl port-forward --namespace $NAMESPACE \
$(
kubectl get pod \
--namespace $NAMESPACE \
--selector="$POD_LABEL" \
@toksdotdev
toksdotdev / gcloud-k8s-account-switch.sh
Created December 10, 2020 17:52
Setup GCloud & Kubernetes environment easily
PROJECT_ID=boringcompany
GCLOUD_K8_ZONE=us-central1-c
GCLOUD_K8_CLUSTER=production
gcloud auth login
gcloud auth configure-docker
gcloud config set project $PROJECT_ID
gcloud container clusters get-credentials $GCLOUD_K8_CLUSTER --zone $GCLOUD_K8_ZONE --project $PROJECT_ID
@toksdotdev
toksdotdev / JobHandler.js
Last active March 1, 2020 18:12
Job queue handler abstraction
const Bull = use("Rocketseat/Bull");
const moment = require("moment");
const Redis = use("Redis");
/**
* Prepare queue configuration. Options provided overrides
* the field value of the default configuration.
*
* @param {object} [options]
*/