Skip to content

Instantly share code, notes, and snippets.

View wubin1989's full-sized avatar

武斌 wubin1989

View GitHub Profile
@wubin1989
wubin1989 / gist:673f4f6b4456b9841ec44f5cd9158c46
Created June 30, 2023 15:00 — forked from jedp/gist:3166329
Hash to small integer
// Hash 'string' to a small number (digits default: 6)
function hash(string, digits) {
digits = digits || 6;
var m = Math.pow(10, digits+1) - 1;
var phi = Math.pow(10, digits) / 2 - 1;
var n = 0;
for (var i = 0; i < string.length; i++) {
n = (n + phi * string.charCodeAt(i)) % m;
}
return n.toString();
@wubin1989
wubin1989 / errgroup_withcontext.go
Created June 3, 2023 04:31 — forked from dragonsinth/errgroup_withcontext.go
Using errgroup.WithContext() in Golang server handlers
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"sync/atomic"
"time"
@wubin1989
wubin1989 / html_entities.go
Created May 5, 2023 11:42 — forked from brandonrachal/html_entities.go
A Go Lang Program to Convert Special Symbols to HTML Entities
package main
import (
"fmt"
"bufio"
"os"
"strconv"
)
func ReadLinesFromFile(file_path string) ([]string, error) {

Two approaches to handle error responses from Spring WebClient calls globally:

  • Exceptions with webclients are all wrapped in WebClientResponseException class. So we can handle that using Spring's ExceptionHandler annotation.

  • Using ExchangeFilterFunction while constructing the webclient bean.

@wubin1989
wubin1989 / prometheus.yml
Created January 3, 2023 08:27 — forked from weibeld/prometheus.yml
Example Prometheus configuration (scrape config)
global:
scrape_interval: 10s
scrape_configs:
- job_name: node
static_configs:
- targets:
- localhost:9100
- job_name: python-app
static_configs:
- targets:
@wubin1989
wubin1989 / latency.txt
Created November 28, 2022 02:46 — forked from jboner/latency.txt
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers (~2012)
----------------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD
func (receiver *GoStatsImpl) LargestRemainderRpc(ctx context.Context, request *pb.PercentageReqVo) (*pb.LargestRemainderRpcResponse, error) {
var payload vo.PercentageReqVo
copier.DeepCopy(request, &payload)
data, err := receiver.LargestRemainder(ctx, payload)
if err != nil {
return nil, err
}
var ret pb.LargestRemainderRpcResponse
err = copier.DeepCopy(data, &ret.Data)
if err != nil {
func (receiver *GoStatsImpl) LargestRemainder(ctx context.Context, payload vo.PercentageReqVo) (data []vo.PercentageRespVo, err error) {
if len(payload.Data) == 0 {
return
}
input := make([]numberutils.Percentage, 0)
for _, item := range payload.Data {
input = append(input, numberutils.Percentage{
Value: item.Value,
Data: item.Key,
})
/**
* Generated by go-doudou v2.0.3.
* You can edit it as your need.
*/
package service
import (
"context"
"go-doudou-tutorials/go-stats/config"
pb "go-doudou-tutorials/go-stats/transport/grpc"
/**
* Generated by go-doudou v2.0.3.
* You can edit it as your need.
*/
package service
import (
"context"
"go-doudou-tutorials/go-stats/config"
pb "go-doudou-tutorials/go-stats/transport/grpc"