Skip to content

Instantly share code, notes, and snippets.

View yangyuqian's full-sized avatar

Yuqian Yang yangyuqian

  • FreeWheel
  • Beijing
View GitHub Profile
@yangyuqian
yangyuqian / main.go
Last active May 16, 2023 07:22
如何用70行Go代码实现DDos攻击
// Go HTTP Client中的Transport是一种连接池实现,如果Client每次请求不共享,
// 且销毁前打开了socket(从server端读了数据),GC会把遗留的Transport对象回收掉,
// 这样就可以保证连接数一直增加,进而实现简单的DDos攻击。
// 执行本实例:
// ```
// # 对某个uri发动10ms一次的请求
// go run main.go -target ${attacked_uri} -interval 10
// ```
package main
@yangyuqian
yangyuqian / crazy_ram.c
Created November 30, 2017 05:57
Code snippet to simulate memory load
// This program takes a integer N as input to "eat" N megabytes RAM
// to simulate arbitrary ram load in reality.
//
// To run this program on CentOS:
// ```
// # install gcc
// $ yum install gcc -y
// # compile the program
// $ gcc -o crazy_ram crazy_ram.c
// # run the program to consume 1G RAM
@yangyuqian
yangyuqian / expr.go
Last active September 21, 2017 08:17
lexical parser example to parse and verify a expression `<int64> + <int64>`
/*
A simple lexical parser for mathematical expression,
i.e.
1 + 2 => {1, 2, "+"}
1+2 => {1, 2, "+"}
1 => error
1 + => error
1 + A => error
a A C => error
@yangyuqian
yangyuqian / unlinkns.sh
Created March 23, 2017 06:54
Remove symlinks to docker containers, which created just for testing purpose
#!/bin/bash
container_ids=`docker ps -aq`
for cid in $container_ids
do
pid=`docker inspect -f '{{.State.Pid}}' $cid`
if [ -f "/var/run/netns/$cid" ]; then
rm /var/run/netns/$cid
fi
@yangyuqian
yangyuqian / linkns.sh
Last active July 9, 2018 16:48
Create symlinks for network namespaces inside docker containers
#!/bin/bash
# This simple script creates symlinks for network namespaces of docker containers,
# then you can interact to that network namespace with built-in iproute2 on linux.
# See iproute2 convention in: http://man7.org/linux/man-pages/man8/ip-netns.8.html
container_ids=`docker ps -aq`
for cid in $container_ids
do
@yangyuqian
yangyuqian / graceful_exit.go
Created March 10, 2017 02:08
Graceful Exit in Go Program
/*
* Killing a program immediately sometimes leads to corrupted data or files, which brings unexpected result in your system.
* In Go, you can catch the termination signals and let your program decide the time of exiting.
*/
package main
import (
"os"
"os/signal"
"syscall"
@yangyuqian
yangyuqian / main.go
Last active October 19, 2016 05:05
Go处理输入输出格式不一致的场景
package main
import (
"encoding/json"
"fmt"
)
/*
处理JSON的输入和输出格式不一致的情况下,
用同一个struct定义的场景
@yangyuqian
yangyuqian / gist:7158b4ff25524c3de6dde978a45bd882
Last active March 5, 2018 07:10
Go Vendoring源码阅读笔记与用法分析

为什么引入Vendoring机制?

Go1.5之后有一些比较重要的改动,其中包含Vendoring的支持,本文从使用和源码实现整理了一些备忘录,难免有疏漏,各位看官多指教。

没有引入Vendoring机制时,Go项目组织主要有两种方案:

  • 直接把项目放到GOPATH下面[详见: 附录1]
  • 项目放到GOPATH外,修改GOPATH来使用Go Command[详见: 附录2]

第三方依赖管理的灵活性和便捷性要求Go加入一种新机制,在不打破现有GOPATH假设的情况下,扩展GOPATH的package查找能力,这就是本文中介绍的Vendoring机制.