Compare commits
11 Commits
65d3081870
...
kd-live
| Author | SHA1 | Date | |
|---|---|---|---|
| c31f838ba8 | |||
| f9a146321c | |||
| ea8ae4d02c | |||
| 8173216e95 | |||
| f4510f8990 | |||
| ead6543d95 | |||
| 38683eb616 | |||
| dd05ebf6ce | |||
| b759f13eee | |||
| 3024a17b54 | |||
| a9de99b04a |
59
coupon/helper.go
Normal file
59
coupon/helper.go
Normal file
@ -0,0 +1,59 @@
|
||||
package coupon
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"math/rand"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func DisolveCouponCode(code string) (round string, key string) {
|
||||
var final []byte
|
||||
for _, n := range strings.Split(code, "-") {
|
||||
nb, err := hex.DecodeString(n)
|
||||
if err != nil {
|
||||
// 형식 오류
|
||||
return "", ""
|
||||
}
|
||||
final = append(final, nb...)
|
||||
}
|
||||
|
||||
if len(final) != 8 {
|
||||
// 형식 오류
|
||||
return "", ""
|
||||
}
|
||||
|
||||
uid := final[4:]
|
||||
left := binary.BigEndian.Uint16(uid[0:2])
|
||||
right := binary.BigEndian.Uint16(uid[2:4])
|
||||
|
||||
final = final[0:4]
|
||||
xor := binary.LittleEndian.Uint32(final)
|
||||
roundhashnum := xor ^ (uint32(left) * uint32(right))
|
||||
|
||||
roundhash := make([]byte, 4)
|
||||
binary.BigEndian.PutUint32(roundhash, roundhashnum)
|
||||
|
||||
round = hex.EncodeToString(roundhash)
|
||||
key = hex.EncodeToString(uid)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func MakeCouponRoundHash(name string) (hash string, roundNumber uint32) {
|
||||
m5 := md5.New()
|
||||
m5.Write([]byte(name))
|
||||
hashbt := m5.Sum(nil)
|
||||
|
||||
roundbt := make([]byte, 8)
|
||||
copy(roundbt, hashbt[:8])
|
||||
|
||||
roundseed := int64(binary.BigEndian.Uint64(roundbt))
|
||||
roundhash := make([]byte, 4)
|
||||
rand.New(rand.NewSource(roundseed)).Read(roundhash)
|
||||
roundNumber = binary.BigEndian.Uint32(roundhash)
|
||||
hash = hex.EncodeToString(roundhash)
|
||||
|
||||
return
|
||||
}
|
||||
19
server.go
19
server.go
@ -73,6 +73,7 @@ func healthCheckHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// 한번이라도 들어오면 lb에 붙어있다는 뜻
|
||||
if t := atomic.AddInt64(&healthcheckcounter, 1); t < 0 {
|
||||
logger.Println("healthCheckHandler return StatusServiceUnavailable :", t)
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
}
|
||||
}
|
||||
@ -127,11 +128,25 @@ func (server *Server) shutdown() {
|
||||
|
||||
signal.Stop(server.interrupt)
|
||||
|
||||
if atomic.LoadInt64(&healthcheckcounter) > 0 {
|
||||
if t := atomic.LoadInt64(&healthcheckcounter); t > 0 {
|
||||
logger.Println("http server shutdown. healthcheckcounter :", t)
|
||||
atomic.StoreInt64(&healthcheckcounter, math.MinInt64)
|
||||
for atomic.LoadInt64(&healthcheckcounter)-math.MinInt64 == 0 {
|
||||
|
||||
timer := 600 // 0.1 * 600 = 1분
|
||||
for cnt := 0; cnt < 100 && timer > 0; {
|
||||
next := atomic.LoadInt64(&healthcheckcounter)
|
||||
if next == t {
|
||||
cnt++
|
||||
} else {
|
||||
t = next
|
||||
cnt = 0
|
||||
}
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
timer--
|
||||
}
|
||||
logger.Println("http server shutdown. healthcheck completed")
|
||||
} else {
|
||||
logger.Println("http server shutdown. no lb")
|
||||
}
|
||||
|
||||
if server.httpserver != nil {
|
||||
|
||||
Reference in New Issue
Block a user