Skip to content

Instantly share code, notes, and snippets.

View Petelin's full-sized avatar

lin Petelin

View GitHub Profile
@Petelin
Petelin / debugPrintMiddleware.go
Created July 16, 2020 09:16
rpc 中间打印
func fieldSet(fields ...string) map[string]bool {
set := make(map[string]bool, len(fields))
for _, s := range fields {
set[s] = true
}
return set
}
func IgnoreFields(v interface{}, fields ...string) map[string]interface{} {
@Petelin
Petelin / condition_lock.go
Created February 28, 2020 11:06
条件锁
package versionlock
import (
"errors"
"sync/atomic"
"time"
)
var ErrOldVersion = errors.New("old version")
var ErrUnlockFailed = errors.New("unlock failed")
@Petelin
Petelin / 4.py
Created January 6, 2020 04:18
leetcode 4.Median of Two Sorted Arrays
def median(A, B):
m, n = len(A), len(B)
if m > n:
A, B, m, n = B, A, n, m
if n == 0:
raise ValueError
imin, imax, half_len = 0, m, (m + n + 1) / 2
while imin <= imax:
i = (imin + imax) / 2
@Petelin
Petelin / do_pprof.go
Created November 18, 2019 11:51
上面是一个middleware实现, 最终cpu信息会记录到profMap中. (要多次调用接口才能看到效果).
func Handler(meta endpoint.Meta, next endpoint.Endpoint) endpoint.Endpoint {
profMapLock.Lock()
defer profMapLock.Unlock()
traceBytes := []byte{}
tracePtr := &traceBytes
var lock sync.Mutex
var buf bytes.Buffer
var traceBuf bytes.Buffer
@Petelin
Petelin / decortor_with_recurive.go
Last active November 3, 2019 01:39
递归函数加装饰器
package main
import "fmt"
type FibI interface {
Fib(n int) int
Wrap(fib FibI) FibI
}
type Fib struct {
@Petelin
Petelin / pop_set.go
Last active June 4, 2019 09:12
pop a set
// Copyright 2013 Gary Burd
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// Copyright (c) 2012-2019 Grabtaxi Holdings PTE LTD (GRAB), All Rights Reserved. NOTICE: All information contained herein
// is, and remains the property of GRAB. The intellectual and technical concepts contained herein are confidential, proprietary
// and controlled by GRAB and may be covered by patents, patents in process, and are protected by trade secret or copyright law.
//
// You are strictly forbidden to copy, download, store (in any medium), transmit, disseminate, adapt or change this material
// in any way unless prior written permission is obtained from GRAB. Access to the source code contained herein is hereby
// forbidden to anyone except current GRAB employees or contractors with binding Confidentiality and Non-disclosure agreements
// explicitly covering such access.
//
// The copyright notice above does not evidence any actual or intended publication or disclosure of this source code,
package main
import (
"fmt"
"sync"
"time"
)
func main() {
s := []string{"A", "B", "C"}
@Petelin
Petelin / condition.go
Last active March 21, 2019 10:41
三个线程顺序打印ABC
package main
import (
"fmt"
"sync"
"time"
)
var l = sync.Mutex{}
var cond = sync.NewCond(&l)
@Petelin
Petelin / patchVariable.go
Created January 23, 2019 10:19
go通过反射替换任意变量的函数
func TestH(t *testing.T) {
say := func() {
fmt.Println("say")
}
rec := patchFunction(&say, func() { fmt.Println("replace") })
say()
rec()
say()
}