Skip to content

Instantly share code, notes, and snippets.

View pandulaDW's full-sized avatar
🎯
Focusing

Pandula Weerasooriya pandulaDW

🎯
Focusing
View GitHub Profile
@pandulaDW
pandulaDW / main.rs
Created July 5, 2023 05:20
A command line program to get temporary AWS credentials using an MFA device.
use aws_sdk_sts as sts;
use std::env;
#[tokio::main]
async fn main() {
let config = aws_config::load_from_env().await;
let client = sts::Client::new(&config);
let token_code = env::args().nth(1);
@pandulaDW
pandulaDW / main.rs
Created July 16, 2022 09:26
waitgroup full impl
use std::{thread, time::Duration};
mod sync_primitives {
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Condvar, Mutex};
struct Wg {
counter: AtomicUsize,
mu: Mutex<bool>,
condvar: Condvar,
@pandulaDW
pandulaDW / main.rs
Last active July 16, 2022 09:12
wait function for wg
fn wait(&self) {
let mut started = self.0.mu.lock().unwrap();
while !*started {
started = self.0.condvar.wait(started).unwrap();
if self.0.counter.load(Ordering::Relaxed) == 0 {
*started = true;
}
}
@pandulaDW
pandulaDW / main.rs
Created July 16, 2022 09:00
impl clone and drop for wg
impl Clone for WaitGroup {
fn clone(&self) -> Self {
self.0.counter.fetch_add(1, Ordering::Relaxed);
Self(self.0.clone())
}
}
impl Drop for WaitGroup {
fn drop(&mut self) {
self.0.counter.fetch_sub(1, Ordering::Relaxed);
@pandulaDW
pandulaDW / main.rs
Last active July 16, 2022 08:42
wg-impl
use std::sync::atomic::AtomicUsize;
use std::sync::{Condvar, Mutex};
struct Wg {
counter: AtomicUsize,
mu: Mutex<bool>,
condvar: Condvar,
}
impl Wg {
@pandulaDW
pandulaDW / main.go
Created August 7, 2021 13:09
the main function
package main
import (
"fmt"
"github.com/pandulaDW/go-json-to-proto/transactions"
"google.golang.org/protobuf/proto"
"log"
"os"
"time"
)
@pandulaDW
pandulaDW / ioOps.go
Created August 7, 2021 13:06
full transaction IO code
package transactions
import (
"encoding/binary"
"google.golang.org/protobuf/proto"
"io"
"os"
)
func WriteProtoToFile(filepath string, allTransactions []*TransactionJsonType) error {
@pandulaDW
pandulaDW / ioOps.go
Last active August 7, 2021 13:05
reading transaction by transaction
buf := make([]byte, 4)
if _, err := file.ReadAt(buf, offset); err != nil {
if err == io.EOF {
break
}
return nil, err
}
itemSize := binary.LittleEndian.Uint32(buf)
offset += 4
@pandulaDW
pandulaDW / ioOps.go
Last active August 7, 2021 13:01
encoding each transaction
// writing the length of the encoded item before writing the data
buf := make([]byte, 4)
binary.LittleEndian.PutUint32(buf, uint32(len(encodedData)))
if _, err := outFile.Write(buf); err != nil {
return err
}
// writing the actual transaction item to the file
if _, err := outFile.Write(encodedData); err != nil {
return err
@pandulaDW
pandulaDW / conversions.go
Created August 7, 2021 12:47
converting json struct objects to proto struct objects
package transactions
func toProtoSingleTransaction(obj *SingleJsonTransaction) *SingleTransaction {
return &SingleTransaction{
Date: obj.Date.Date.UnixNano(),
Amount: obj.Amount,
TransactionCode: obj.TransactionCode,
Price: obj.Price,
Total: obj.Total,
}