Skip to content

Commit

Permalink
Add page that shows banners for each post, updates banners, update draft
Browse files Browse the repository at this point in the history
  • Loading branch information
sudorandom committed Apr 21, 2024
1 parent aa993d5 commit 0671915
Show file tree
Hide file tree
Showing 19 changed files with 594 additions and 331 deletions.
2 changes: 1 addition & 1 deletion content/_index.html
Expand Up @@ -5,7 +5,7 @@ <h2>Welcome!</h2>
Hey, I'm <strong>Kevin</strong>. I'm a backend software engineer from Texas, living and working in Copenhagen, Denmark. I currently work for <a href="https://sybogames.com/" target="_blank">SYBO</a>. By the way, none of this content represents SYBO in any way.
</p>
<p>
You can find me as <code>sudorandom</code> on most platforms.
You can find me as <strong><code>sudorandom</code></strong> on most platforms.
</p>
</div>

Expand Down
9 changes: 9 additions & 0 deletions content/banners.md
@@ -0,0 +1,9 @@
+++
description = ""
title = "Banners"
subtitle = "Banners of all my posts"
slug = "/banners"
layout = "banners"
+++

This page has all of the post banners for all of my posts. It's not super useful but I think it looks pretty.
2 changes: 1 addition & 1 deletion content/posts/2023/internet-map-2023/index.md
Expand Up @@ -3,7 +3,7 @@ categories: ["article", "project"]
tags: ["dataviz", "internet", "networking", "fiber-optics", "map", "world", "infrastructure", "peeringdb", "svg", "javascript", "golang"]
date: "2023-08-01"
description: "Journey into the depths of the Internet with this incredible map showcasing undersea cables and internet exchanges."
cover: ""
cover: "geo-mercator-apac.png"
images: ["posts/internet-map-2023/geo-mercator-na.png"]
featured: ""
featuredalt: ""
Expand Down
Binary file removed content/posts/2023/softlayer-python/cover.jpg
Binary file not shown.
Binary file added content/posts/2023/softlayer-python/cover.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions content/posts/2023/softlayer-python/index.md
Expand Up @@ -3,8 +3,8 @@ categories: ["article", "project"]
tags: ["python", "cloud", "softlayer", "api", "cli", "open-source"]
date: "2023-07-31"
description: "I wrote and maintained language bindings for a large cloud company. Join me as I reflect on that experience."
cover: "cover.jpg"
images: ["/posts/softlayer-python/cover.jpg"]
cover: "cover.png"
images: ["/posts/softlayer-python/cover.png"]
featured: ""
featuredalt: ""
featuredpath: "date"
Expand Down
Binary file removed content/posts/2023/softlayer-python/thumbnail.png
Binary file not shown.
Binary file removed content/posts/2023/swftp/cover.jpg
Binary file not shown.
File renamed without changes
4 changes: 2 additions & 2 deletions content/posts/2023/swftp/index.md
Expand Up @@ -3,8 +3,8 @@ categories: ["article", "project"]
tags: ["programming", "python", "sftp", "ftp", "cloud", "swftp", "twisted", "openstack", "swift", "api", "open-source"]
date: "2023-07-30"
description: "Describing an old project of mine from 2014; an SFTP/FTP interface over an object storage API using Python Twisted."
cover: "cover.jpg"
images: ["/posts/swftp/cover.jpg"]
cover: "cover.png"
images: ["/posts/swftp/cover.png"]
featured: ""
featuredalt: ""
featuredpath: "date"
Expand Down
110 changes: 110 additions & 0 deletions content/posts/2024/grpc-from-scratch-part-3/go/encoding.go
@@ -0,0 +1,110 @@
package grpcfromscratchpart3

import (
"bytes"
"errors"
"io"
"math"
"math/bits"
)

const (
MaxVarintLen64 = 10
)

var (
ErrOverflow = errors.New("overflow")
ErrTruncated = errors.New("truncated")
)

func WriteUvarint(buf *bytes.Buffer, x uint64) {
for x >= 0x80 {
buf.WriteByte(byte(x) | 0x80)
x >>= 7
}
buf.WriteByte(byte(x))
}

func WriteFieldTag(buf *bytes.Buffer, field int32, protoType uint8) {
WriteUvarint(buf, uint64(field<<3)|uint64(protoType))
}

func ReadFieldTag(buf *bytes.Buffer) (int32, int8, error) {
field, err := ReadUvarint(buf)
if err != nil {
return 0, 0, err
}
if field>>3 > uint64(math.MaxInt32) {
return 0, 0, ErrOverflow
}
return int32(field >> 3), int8(field & 7), nil
}

func ReadUvarint(buf *bytes.Buffer) (uint64, error) {
var x uint64
var s uint
var i int
for {
b, err := buf.ReadByte()
if err != nil {
return 0, err
}
if i == MaxVarintLen64 {
return 0, ErrOverflow // overflow
}
if b < 0x80 {
if i == MaxVarintLen64-1 && b > 1 {
return 0, ErrOverflow // overflow
}
return x | uint64(b)<<s, nil
}
x |= uint64(b&0x7f) << s
s += 7
i++
}
}

func ReadBytes(buf *bytes.Buffer) ([]byte, error) {
size, err := ReadUvarint(buf)
if err != nil {
return nil, ErrTruncated
}
if uint64(buf.Len()) < size {
return nil, ErrTruncated
}

result := make([]byte, size)
n, err := buf.Read(result)
if err != nil {
return nil, err
}
if uint64(n) != size {
return nil, ErrTruncated
}
return result, nil
}

func ReadString(buf *bytes.Buffer) (string, error) {
b, err := ReadBytes(buf)
if err != nil {
return "", err
}
return string(b), err
}

func ReadRepeatedInt32(buf *bytes.Buffer) ([]int32, error) {
result := []int32{}
for {
res, err := ReadUvarint(buf)
if err == io.EOF {
return result, nil
} else if err != nil {
return nil, err
}
result = append(result, int32(res))
}
}

func SizeVarint(v uint64) int {
return int(9*uint32(bits.Len64(v))+64) / 64
}
144 changes: 61 additions & 83 deletions content/posts/2024/grpc-from-scratch-part-3/go/encoding_test.go
@@ -1,46 +1,23 @@
package go_test
package grpcfromscratchpart3

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/sudorandom/sudorandom.dev/grpc-from-scratch/gen"
"google.golang.org/protobuf/encoding/protowire"
"google.golang.org/protobuf/proto"
)

func TestDecodeString(t *testing.T) {
b, err := proto.Marshal(&gen.TestMessage{
StringValue: "Hello World!",
})
if err != nil {
require.NoError(t, err)
}

tagNumber, protoType, n := protowire.ConsumeTag(b)
require.GreaterOrEqual(t, n, 0)
require.Equal(t, n, 1)
require.Equal(t, protowire.BytesType, protoType)
require.True(t, tagNumber.IsValid())
assert.Equal(t, protowire.Number(7), tagNumber)

b = b[n:]

str, n := protowire.ConsumeString(b)
require.GreaterOrEqual(t, n, 0)
assert.Equal(t, 13, n)
assert.Equal(t, "Hello World!", str)
}

func TestEncodeString(t *testing.T) {
var buf []byte
buf = protowire.AppendTag(buf, protowire.Number(7), protowire.BytesType)
buf = protowire.AppendString(buf, "Hello World!")
func TestEncodeInt32(t *testing.T) {
buf := &bytes.Buffer{}
WriteFieldTag(buf, 1, 0)
WriteUvarint(buf, 1234)

res := gen.TestMessage{}
require.NoError(t, proto.Unmarshal(buf, &res))
assert.Equal(t, "Hello World!", res.StringValue)
require.NoError(t, proto.Unmarshal(buf.Bytes(), &res))
assert.Equal(t, int32(1234), res.IntValue)
}

func TestDecodeInt32(t *testing.T) {
Expand All @@ -50,82 +27,83 @@ func TestDecodeInt32(t *testing.T) {
if err != nil {
require.NoError(t, err)
}

tagNumber, protoType, n := protowire.ConsumeTag(b)
require.GreaterOrEqual(t, n, 0)
require.Equal(t, n, 1)
require.Equal(t, protowire.VarintType, protoType)
require.True(t, tagNumber.IsValid())
assert.Equal(t, protowire.Number(1), tagNumber)

b = b[n:]
i, n := protowire.ConsumeVarint(b)
require.GreaterOrEqual(t, n, 0)
assert.Equal(t, 2, n)
buf := bytes.NewBuffer(b)
tagNumber, protoType, err := ReadFieldTag(buf)
require.NoError(t, err)
assert.Equal(t, int8(0), protoType)
assert.Equal(t, int32(1), tagNumber)

i, err := ReadUvarint(buf)
assert.NoError(t, err)
assert.Equal(t, int32(1234), int32(i))
}

func TestEncodeInt32(t *testing.T) {
var buf []byte
buf = protowire.AppendTag(buf, protowire.Number(1), protowire.VarintType)
buf = protowire.AppendVarint(buf, 1234)
func TestEncodeString(t *testing.T) {
buf := &bytes.Buffer{}
s := "hello world"
WriteFieldTag(buf, 4, 2)
WriteUvarint(buf, uint64(len(s)))
buf.WriteString(s)

res := gen.TestMessage{}
require.NoError(t, proto.Unmarshal(buf, &res))
assert.Equal(t, int32(1234), res.IntValue)
require.NoError(t, proto.Unmarshal(buf.Bytes(), &res))
assert.Equal(t, "hello world", res.StringValue)
}

func TestDecodeInt32Array(t *testing.T) {
func TestDecodeString(t *testing.T) {
b, err := proto.Marshal(&gen.TestMessage{
RepeatedIntValue: []int32{100002130, 2, 3, 4, 5},
StringValue: "hello world",
})
if err != nil {
require.NoError(t, err)
}

tagNumber, protoType, n := protowire.ConsumeTag(b)
require.GreaterOrEqual(t, n, 0)
require.Equal(t, n, 1)
require.Equal(t, protowire.BytesType, protoType)
require.True(t, tagNumber.IsValid())
assert.Equal(t, protowire.Number(9), tagNumber)

b = b[n:]
int32buf, n := protowire.ConsumeBytes(b)
require.GreaterOrEqual(t, n, 0)
assert.Equal(t, 9, n)
res := []int32{}
for len(int32buf) > 0 {
v, n := protowire.ConsumeVarint(int32buf)
require.GreaterOrEqual(t, n, 0)
res = append(res, int32(v))
int32buf = int32buf[n:]
}
assert.Equal(t, []int32{100002130, 2, 3, 4, 5}, res)
buf := bytes.NewBuffer(b)
tagNumber, protoType, err := ReadFieldTag(buf)
require.NoError(t, err)
assert.Equal(t, int8(2), protoType)
assert.Equal(t, int32(4), tagNumber)

s, err := ReadString(buf)
assert.NoError(t, err)
assert.Equal(t, "hello world", string(s))
}

func TestEncodeInt32Array(t *testing.T) {
arr := []int32{100002130, 2, 3, 4, 5}
var buf []byte
buf = protowire.AppendVarint(buf, protowire.EncodeTag(protowire.Number(9), protowire.BytesType))
buf := &bytes.Buffer{}
WriteFieldTag(buf, 10, 2)
size := 0
for i := 0; i < len(arr); i++ {
size += protowire.SizeVarint(uint64(arr[i]))
size += SizeVarint(uint64(arr[i]))
}
buf = protowire.AppendVarint(buf, uint64(size))
WriteUvarint(buf, uint64(size))
for i := 0; i < len(arr); i++ {
buf = protowire.AppendVarint(buf, uint64(arr[i]))
WriteUvarint(buf, uint64(arr[i]))
}

t.Log(buf)

res := gen.TestMessage{}
require.NoError(t, proto.Unmarshal(buf, &res))
require.NoError(t, proto.Unmarshal(buf.Bytes(), &res))
assert.Equal(t, []int32{100002130, 2, 3, 4, 5}, res.RepeatedIntValue)
}

func TestAppendVarint(t *testing.T) {
buf := []byte{}
buf = protowire.AppendVarint(buf, 123456)
assert.Equal(t, []byte{0xc0, 0xc4, 0x7}, buf)
func TestDecodeInt32Array(t *testing.T) {
b, err := proto.Marshal(&gen.TestMessage{
RepeatedIntValue: []int32{1, 2, 3, 400},
})
if err != nil {
require.NoError(t, err)
}

buf := bytes.NewBuffer(b)
tagNumber, protoType, err := ReadFieldTag(buf)
require.NoError(t, err)
assert.Equal(t, int8(2), protoType)
assert.Equal(t, int32(10), tagNumber)

packedBytes, err := ReadBytes(buf)
assert.NoError(t, err)

result, err := ReadRepeatedInt32(bytes.NewBuffer(packedBytes))
assert.NoError(t, err)
assert.Equal(t, []int32{1, 2, 3, 400}, result)
}

0 comments on commit 0671915

Please sign in to comment.