Skip to content

Instantly share code, notes, and snippets.

View huantt's full-sized avatar
🎯
Focusing

Jack huantt

🎯
Focusing
View GitHub Profile
package main
import (
"fmt"
"time"
)
func write(ch chan int) {
for i := 0; i < 4; i++ {
@huantt
huantt / go-redis-marshal-binary.go
Created August 15, 2022 03:41
Redis is based on key-value pairs, and key-values are all strings and other string-based data structures. Therefore, if you want to put some data into redis, you should make these data strings. Your struct must implement this interface like code below to make go-redis able to stringify your type.
func (i Info) MarshalBinary() (data []byte, err error) {
bytes, err := json.Marshal(i) \\edited - changed to i
return bytes, err
}
@huantt
huantt / gist:5ae6643d58ab7c5998745b0f26e0400c
Created August 22, 2022 15:19
jwt-header-sample.json
{
"alg": "HS256",
"typ": "JWT"
}
{
"name": "John Doe",
"email":"[email protected]",
"exp": 1671722663
}
@huantt
huantt / IERC20.sol
Last active December 18, 2022 08:30
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
contract DepositFunds {
mapping(address => uint) public balances;
function deposit() public payable {
balances[msg.sender] += msg.value;
}
function withdraw() public {
uint bal = balances[msg.sender];
require(bal > 0);
contract Attack {
DepositFunds public depositFunds;
constructor(address _depositFundsAddress) {
depositFunds = DepositFunds(_depositFundsAddress);
}
// Fallback is called when DepositFunds sends Ether to this contract.
fallback() external payable {
if (address(depositFunds).balance >= 1 ether) {
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
import {IERC20} from "./IERC20.sol";
contract Fund {
mapping(address => mapping(address => uint)) public balances;
mapping(address => mapping(address => bool)) public lock;
@huantt
huantt / rate_limit.go
Last active June 23, 2023 10:21
Rate limit pattern using channel in Golang
package main
import (
"fmt"
"sync"
"time"
)
var limit = 10
@huantt
huantt / golang-limit-requests-per-second.go
Last active May 12, 2023 09:10
Limit requests per second in Golang using time.Tick
package main
import (
"fmt"
"time"
)
func main() {
// Rate limit to 10 requests per second
for i := 0; i < 100; i++ {