Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//app/common/live/library/lancer:all-srcs",
"//app/common/live/library/lrucache:all-srcs",
"//app/common/live/library/mengde:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,29 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["lancer.go"],
importpath = "go-common/app/common/live/library/lancer",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = ["//library/log:go_default_library"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,198 @@
package lancer
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"net"
"runtime"
"strconv"
"strings"
"sync"
"time"
"go-common/library/log"
)
const (
kLancerMinimumLength = 6
kLancerLengthPartBegin = 2
kLancerLengthPartEnd = 6
)
type LancerLogStream struct {
conn net.Conn
addr string
pool sync.Pool
dataChannel chan *LancerData
quit chan struct{}
wg sync.WaitGroup
splitter string
replacer string
timeout time.Duration
}
type LancerData struct {
bytes.Buffer
logid string
isAppend bool
lancer *LancerLogStream
}
func NewLancerLogStream(address string, capacity int, timeout time.Duration) *LancerLogStream {
c, err := net.DialTimeout("tcp", address, timeout)
if err != nil {
log.Error("[Lancer]Dial lancer %s error:%+v", address, err)
c = nil
}
lancer := &LancerLogStream{
conn: c,
addr: address,
pool: sync.Pool{
New: func() interface{} {
return new(LancerData)
},
},
dataChannel: make(chan *LancerData, capacity),
quit: make(chan struct{}),
splitter: "\u0001",
replacer: "|",
timeout: timeout,
}
lancer.wg.Add(1)
go lancer.processor()
return lancer
}
func (lancer *LancerLogStream) Close() {
close(lancer.dataChannel)
close(lancer.quit)
lancer.wg.Wait()
}
func (lancer *LancerLogStream) processor() {
defer func() {
if lancer.conn != nil {
lancer.conn.Close()
}
lancer.wg.Done()
}()
var lastFail *LancerData
PROCESSOR:
for {
select {
case <-lancer.quit:
return
default:
}
if lastFail != nil {
if err := lancer.write(lastFail.Bytes()); err != nil {
runtime.Gosched()
continue PROCESSOR
}
lancer.pool.Put(lastFail)
lastFail = nil
}
for b := range lancer.dataChannel {
if err := lancer.write(b.Bytes()); err != nil {
lastFail = b
runtime.Gosched()
continue PROCESSOR
}
lancer.pool.Put(b)
}
return
}
}
func (lancer *LancerLogStream) write(b []byte) error {
if lancer.conn == nil {
c, err := net.DialTimeout("tcp", lancer.addr, lancer.timeout)
if err != nil {
log.Error("[Lancer]Dial %s error:%+v", lancer.addr, err)
return err
}
lancer.conn = c
}
_, err := lancer.conn.Write(b)
if err != nil {
log.Error("[Lancer]Conn write error:%+v", err)
lancer.conn.Close()
lancer.conn = nil
}
return err
}
func (lancer *LancerLogStream) NewLancerData(logid string, token string) *LancerData {
ld := lancer.pool.Get().(*LancerData)
ld.Reset()
ld.lancer = lancer
ld.isAppend = false
ld.logid = logid
ld.Write([]byte{0xAC, 0xBE})
ld.Write([]byte{0, 0, 0, 0})
ld.Write([]byte{0, 1})
header := fmt.Sprintf("logId=%s&timestamp=%d&token=%s&version=1.1", logid,
time.Now().UnixNano()/int64(time.Millisecond), token)
headerLength := uint16(len(header))
ld.Write([]byte{byte(headerLength >> 8), byte(headerLength)})
ld.Write([]byte(header))
return ld
}
func (ld *LancerData) PutString(v string) *LancerData {
ld.splitter()
ld.WriteString(strings.Replace(v, ld.lancer.splitter, ld.lancer.replacer, -1))
return ld
}
func (ld *LancerData) PutTimestamp(v time.Time) *LancerData {
return ld.PutInt(v.Unix())
}
func (ld *LancerData) PutUint(v uint64) *LancerData {
ld.splitter()
ld.WriteString(strconv.FormatUint(v, 10))
return ld
}
func (ld *LancerData) PutInt(v int64) *LancerData {
ld.splitter()
ld.WriteString(strconv.FormatInt(v, 10))
return ld
}
func (ld *LancerData) PutFloat(v float64) *LancerData {
ld.splitter()
ld.WriteString(strconv.FormatFloat(v, 'f', -1, 64))
return ld
}
func (ld *LancerData) PutBool(v bool) *LancerData {
ld.splitter()
ld.WriteString(strconv.FormatBool(v))
return ld
}
func (ld *LancerData) splitter() {
if ld.isAppend {
ld.WriteString(ld.lancer.splitter)
}
ld.isAppend = true
}
func (ld *LancerData) Commit() error {
if ld.Len() < kLancerMinimumLength {
return errors.New("protocol error")
}
l := uint32(ld.Len()) - kLancerMinimumLength
binary.BigEndian.PutUint32(ld.Bytes()[kLancerLengthPartBegin:kLancerLengthPartEnd], l)
select {
case ld.lancer.dataChannel <- ld:
return nil
default:
ld.lancer.pool.Put(ld)
return errors.New("lancer channel is full")
}
}

View File

@@ -0,0 +1,43 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"lrucache_test.go",
"synccache_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
)
go_library(
name = "go_default_library",
srcs = [
"lrucache.go",
"synccache.go",
],
importpath = "go-common/app/common/live/library/lrucache",
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,139 @@
package lrucache
// Element - node to store cache item
type Element struct {
prev, next *Element
Key interface{}
Value interface{}
}
// Next - fetch older element
func (e *Element) Next() *Element {
return e.next
}
// Prev - fetch newer element
func (e *Element) Prev() *Element {
return e.prev
}
// LRUCache - a data structure that is efficient to insert/fetch/delete cache items [both O(1) time complexity]
type LRUCache struct {
cache map[interface{}]*Element
head *Element
tail *Element
capacity int
}
// New - create a new lru cache object
func New(capacity int) *LRUCache {
return &LRUCache{make(map[interface{}]*Element), nil, nil, capacity}
}
// Put - put a cache item into lru cache
func (lc *LRUCache) Put(key interface{}, value interface{}) {
if e, ok := lc.cache[key]; ok {
e.Value = value
lc.refresh(e)
return
}
if lc.capacity == 0 {
return
} else if len(lc.cache) >= lc.capacity {
// evict the oldest item
delete(lc.cache, lc.tail.Key)
lc.remove(lc.tail)
}
e := &Element{nil, lc.head, key, value}
lc.cache[key] = e
if len(lc.cache) != 1 {
lc.head.prev = e
} else {
lc.tail = e
}
lc.head = e
}
// Get - get value of key from lru cache with result
func (lc *LRUCache) Get(key interface{}) (interface{}, bool) {
if e, ok := lc.cache[key]; ok {
lc.refresh(e)
return e.Value, ok
}
return nil, false
}
// Delete - delete item by key from lru cache
func (lc *LRUCache) Delete(key interface{}) {
if e, ok := lc.cache[key]; ok {
delete(lc.cache, key)
lc.remove(e)
}
}
// Range - calls f sequentially for each key and value present in the lru cache
func (lc *LRUCache) Range(f func(key, value interface{}) bool) {
for i := lc.head; i != nil; i = i.Next() {
if !f(i.Key, i.Value) {
break
}
}
}
// Update - inplace update
func (lc *LRUCache) Update(key interface{}, f func(value *interface{})) {
if e, ok := lc.cache[key]; ok {
f(&e.Value)
lc.refresh(e)
}
}
// Front - get front element of lru cache
func (lc *LRUCache) Front() *Element {
return lc.head
}
// Back - get back element of lru cache
func (lc *LRUCache) Back() *Element {
return lc.tail
}
// Len - length of lru cache
func (lc *LRUCache) Len() int {
return len(lc.cache)
}
// Capacity - capacity of lru cache
func (lc *LRUCache) Capacity() int {
return lc.capacity
}
func (lc *LRUCache) refresh(e *Element) {
if e.prev != nil {
e.prev.next = e.next
if e.next == nil {
lc.tail = e.prev
} else {
e.next.prev = e.prev
}
e.prev = nil
e.next = lc.head
lc.head.prev = e
lc.head = e
}
}
func (lc *LRUCache) remove(e *Element) {
if e.prev == nil {
lc.head = e.next
} else {
e.prev.next = e.next
}
if e.next == nil {
lc.tail = e.prev
} else {
e.next.prev = e.prev
}
}

View File

@@ -0,0 +1,257 @@
package lrucache
import (
"container/list"
"testing"
)
type Elem struct {
key int
value string
}
func Test_New(t *testing.T) {
lc := New(5)
if lc.Len() != 0 {
t.Error("case 1 failed")
}
}
func Test_Put(t *testing.T) {
lc := New(0)
lc.Put(1, "1")
if lc.Len() != 0 {
t.Error("case 1.1 failed")
}
lc = New(5)
lc.Put(1, "1")
lc.Put(2, "2")
lc.Put(1, "3")
if lc.Len() != 2 {
t.Error("case 2.1 failed")
}
l := list.New()
l.PushBack(&Elem{1, "3"})
l.PushBack(&Elem{2, "2"})
e := l.Front()
for c := lc.Front(); c != nil; c = c.Next() {
v := e.Value.(*Elem)
if c.Key.(int) != v.key {
t.Error("case 2.2 failed: ", c.Key.(int), v.key)
}
if c.Value.(string) != v.value {
t.Error("case 2.3 failed: ", c.Value.(string), v.value)
}
e = e.Next()
}
lc.Put(3, "4")
lc.Put(4, "5")
lc.Put(5, "6")
lc.Put(2, "7")
if lc.Len() != 5 {
t.Error("case 3.1 failed")
}
l = list.New()
l.PushBack(&Elem{2, "7"})
l.PushBack(&Elem{5, "6"})
l.PushBack(&Elem{4, "5"})
l.PushBack(&Elem{3, "4"})
l.PushBack(&Elem{1, "3"})
rl := list.New()
rl.PushBack(&Elem{1, "3"})
rl.PushBack(&Elem{3, "4"})
rl.PushBack(&Elem{4, "5"})
rl.PushBack(&Elem{5, "6"})
rl.PushBack(&Elem{2, "7"})
e = l.Front()
for c := lc.Front(); c != nil; c = c.Next() {
v := e.Value.(*Elem)
if c.Key.(int) != v.key {
t.Error("case 3.2 failed: ", c.Key.(int), v.key)
}
if c.Value.(string) != v.value {
t.Error("case 3.3 failed: ", c.Value.(string), v.value)
}
e = e.Next()
}
e = rl.Front()
for c := lc.Back(); c != nil; c = c.Prev() {
v := e.Value.(*Elem)
if c.Key.(int) != v.key {
t.Error("case 3.4 failed: ", c.Key.(int), v.key)
}
if c.Value.(string) != v.value {
t.Error("case 3.5 failed: ", c.Value.(string), v.value)
}
e = e.Next()
}
lc.Put(6, "8")
if lc.Len() != 5 {
t.Error("case 4.1 failed")
}
l = list.New()
l.PushBack(&Elem{6, "8"})
l.PushBack(&Elem{2, "7"})
l.PushBack(&Elem{5, "6"})
l.PushBack(&Elem{4, "5"})
l.PushBack(&Elem{3, "4"})
e = l.Front()
for c := lc.Front(); c != nil; c = c.Next() {
v := e.Value.(*Elem)
if c.Key.(int) != v.key {
t.Error("case 4.2 failed: ", c.Key.(int), v.key)
}
if c.Value.(string) != v.value {
t.Error("case 4.3 failed: ", c.Value.(string), v.value)
}
e = e.Next()
}
}
func Test_Get(t *testing.T) {
lc := New(2)
lc.Put(1, "1")
lc.Put(2, "2")
if v, _ := lc.Get(1); v != "1" {
t.Error("case 1.1 failed")
}
lc.Put(3, "3")
if lc.Len() != 2 {
t.Error("case 1.2 failed")
}
l := list.New()
l.PushBack(&Elem{3, "3"})
l.PushBack(&Elem{1, "1"})
e := l.Front()
for c := lc.Front(); c != nil; c = c.Next() {
v := e.Value.(*Elem)
if c.Key.(int) != v.key {
t.Error("case 1.3 failed: ", c.Key.(int), v.key)
}
if c.Value.(string) != v.value {
t.Error("case 1.4 failed: ", c.Value.(string), v.value)
}
e = e.Next()
}
}
func Test_Delete(t *testing.T) {
lc := New(5)
lc.Put(3, "4")
lc.Put(4, "5")
lc.Put(5, "6")
lc.Put(2, "7")
lc.Put(6, "8")
lc.Delete(5)
l := list.New()
l.PushBack(&Elem{6, "8"})
l.PushBack(&Elem{2, "7"})
l.PushBack(&Elem{4, "5"})
l.PushBack(&Elem{3, "4"})
if lc.Len() != 4 {
t.Error("case 1.1 failed")
}
e := l.Front()
for c := lc.Front(); c != nil; c = c.Next() {
v := e.Value.(*Elem)
if c.Key.(int) != v.key {
t.Error("case 1.2 failed: ", c.Key.(int), v.key)
}
if c.Value.(string) != v.value {
t.Error("case 1.3 failed: ", c.Value.(string), v.value)
}
e = e.Next()
}
lc.Delete(6)
l = list.New()
l.PushBack(&Elem{2, "7"})
l.PushBack(&Elem{4, "5"})
l.PushBack(&Elem{3, "4"})
if lc.Len() != 3 {
t.Error("case 2.1 failed")
}
e = l.Front()
for c := lc.Front(); c != nil; c = c.Next() {
v := e.Value.(*Elem)
if c.Key.(int) != v.key {
t.Error("case 2.2 failed: ", c.Key.(int), v.key)
}
if c.Value.(string) != v.value {
t.Error("case 2.3 failed: ", c.Value.(string), v.value)
}
e = e.Next()
}
lc.Delete(3)
l = list.New()
l.PushBack(&Elem{2, "7"})
l.PushBack(&Elem{4, "5"})
if lc.Len() != 2 {
t.Error("case 3.1 failed")
}
e = l.Front()
for c := lc.Front(); c != nil; c = c.Next() {
v := e.Value.(*Elem)
if c.Key.(int) != v.key {
t.Error("case 3.2 failed: ", c.Key.(int), v.key)
}
if c.Value.(string) != v.value {
t.Error("case 3.3 failed: ", c.Value.(string), v.value)
}
e = e.Next()
}
}
func Test_Range(t *testing.T) {
lc := New(5)
lc.Put(3, "4")
lc.Put(4, "5")
lc.Put(5, "6")
lc.Put(2, "7")
lc.Put(6, "8")
l := list.New()
l.PushBack(&Elem{6, "8"})
l.PushBack(&Elem{2, "7"})
l.PushBack(&Elem{5, "6"})
l.PushBack(&Elem{4, "5"})
l.PushBack(&Elem{3, "4"})
e := l.Front()
lc.Range(
func(key, value interface{}) bool {
v := e.Value.(*Elem)
if key.(int) != v.key {
t.Error("case 1.1 failed: ", key.(int), v.key)
}
if value.(string) != v.value {
t.Error("case 1.2 failed: ", value.(string), v.value)
}
e = e.Next()
return true
})
if e != nil {
t.Error("case 1.3 failed: ", e.Value)
}
}

View File

@@ -0,0 +1,99 @@
package lrucache
import (
"hash/crc32"
"sync"
"time"
)
// hashCode hashes a string to a unique hashcode.
//
// crc32 returns a uint32, but for our use we need
// and non negative integer. Here we cast to an integer
// and invert it if the result is negative.
func hashCode(s string) (hc int) {
hc = int(crc32.ChecksumIEEE([]byte(s)))
if hc >= 0 {
return hc
}
if -hc >= 0 {
return -hc
}
// hc == MinInt
return hc
}
// SyncCache - concurrent cache structure
type SyncCache struct {
locks []sync.Mutex
caches []*LRUCache
mask int
timeout int64
}
type scValue struct {
Value interface{}
ts int64
}
func nextPowOf2(cap int) int {
if cap < 2 {
return 2
}
if cap&(cap-1) == 0 {
return cap
}
cap |= cap >> 1
cap |= cap >> 2
cap |= cap >> 4
cap |= cap >> 8
cap |= cap >> 16
return cap + 1
}
// NewSyncCache - create sync cache
// `capacity` is lru cache length of each bucket
// store `capacity * bucket` count of element in SyncCache at most
// `timeout` is in seconds
func NewSyncCache(capacity int, bucket int, timeout int64) *SyncCache {
size := nextPowOf2(bucket)
sc := SyncCache{make([]sync.Mutex, size), make([]*LRUCache, size), size - 1, timeout}
for i := range sc.caches {
sc.caches[i] = New(capacity)
}
return &sc
}
// Put - put a cache item into sync cache
func (sc *SyncCache) Put(key string, value interface{}) {
idx := hashCode(key) & sc.mask
sc.locks[idx].Lock()
sc.caches[idx].Put(key, &scValue{value, time.Now().Unix()})
sc.locks[idx].Unlock()
}
// Get - get value of key from sync cache with result
func (sc *SyncCache) Get(key string) (interface{}, bool) {
idx := hashCode(key) & sc.mask
sc.locks[idx].Lock()
v, b := sc.caches[idx].Get(key)
if !b {
sc.locks[idx].Unlock()
return nil, false
}
if time.Now().Unix()-v.(*scValue).ts >= sc.timeout {
sc.caches[idx].Delete(key)
sc.locks[idx].Unlock()
return nil, false
}
sc.locks[idx].Unlock()
return v.(*scValue).Value, b
}
// Delete - delete item by key from sync cache
func (sc *SyncCache) Delete(key string) {
idx := hashCode(key) & sc.mask
sc.locks[idx].Lock()
sc.caches[idx].Delete(key)
sc.locks[idx].Unlock()
}

View File

@@ -0,0 +1,85 @@
package lrucache
import (
"sync"
"testing"
"time"
)
func Test_hashCode(t *testing.T) {
/*if hashCode(-1) != 1 {
t.Error("case 1 failed")
}
if hashCode(0) != 0 {
t.Error("case 2 failed")
}
if hashCode(0x7FFFFFFF) != 0x7FFFFFFF {
t.Error("case 3 failed")
}*/
if hashCode("12345") != 3421846044 {
t.Error("case 4 failed")
}
if hashCode("abcdefghijklmnopqrstuvwxyz") != 1277644989 {
t.Error("case 5 failed")
}
/*if hashCode(123.45) != 123 {
t.Error("case 6 failed")
}
if hashCode(-15268.45) != 15268 {
t.Error("case 7 failed")
}*/
}
func Test_nextPowOf2(t *testing.T) {
if nextPowOf2(0) != 2 {
t.Error("case 1 failed")
}
if nextPowOf2(1) != 2 {
t.Error("case 2 failed")
}
if nextPowOf2(2) != 2 {
t.Error("case 3 failed")
}
if nextPowOf2(3) != 4 {
t.Error("case 4 failed")
}
if nextPowOf2(123) != 128 {
t.Error("case 5 failed")
}
if nextPowOf2(0x7FFFFFFF) != 0x80000000 {
t.Error("case 6 failed")
}
}
func Test_timeout(t *testing.T) {
sc := NewSyncCache(1, 2, 2)
sc.Put("1", "2")
if v, ok := sc.Get("1"); !ok || v != "2" {
t.Error("case 1 failed")
}
time.Sleep(2 * time.Second)
if _, ok := sc.Get("1"); ok {
t.Error("case 2 failed")
}
}
func Test_concurrent(t *testing.T) {
sc := NewSyncCache(1, 4, 2)
var wg sync.WaitGroup
for index := 0; index < 100000; index++ {
wg.Add(3)
go func() {
sc.Put("1", "2")
wg.Done()
}()
go func() {
sc.Get("1")
wg.Done()
}()
go func() {
sc.Delete("1")
wg.Done()
}()
}
wg.Wait()
}

View File

@@ -0,0 +1,29 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["mengde.go"],
importpath = "go-common/app/common/live/library/mengde",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = ["//library/log:go_default_library"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,309 @@
package mengde
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"go-common/library/log"
)
const (
kSvenCheckTimeout = time.Minute
kSvenGetTimeout = 30 * time.Second
kSvenHost = "http://config.bilibili.co"
kSvenCheckAPI = kSvenHost + "/config/v2/check"
kSvenGetAPI = kSvenHost + "/config/v2/get"
kDefaultHost = "invalid-host-name"
kDefaultIP = "127.0.0.1"
kCodeNotModified = -304
)
type MengdeClient struct {
treeID string
zone string
env string
build string
token string
host string
ip string
config atomic.Value
notify chan *MengdeConfig
changeSignal chan struct{}
httpClient *http.Client
ctx context.Context
cancel context.CancelFunc
closeSignal chan struct{}
configLoadWg sync.WaitGroup
configNotifyWg sync.WaitGroup
}
type MengdeConfig struct {
Version int
Config map[string]string
}
func NewMengdeClient(treeID string, zone string, env string, build string, token string) (*MengdeClient, error) {
host, err := os.Hostname()
if err != nil {
host = kDefaultHost
}
ip := kDefaultIP
addr, err := net.InterfaceAddrs()
if err == nil {
for _, a := range addr {
if n, ok := a.(*net.IPNet); ok && !n.IP.IsLoopback() && n.IP.To4() != nil {
ip = n.IP.String()
}
}
}
c := &MengdeClient{
treeID: treeID,
zone: zone,
env: env,
build: build,
token: token,
host: host,
ip: ip,
notify: make(chan *MengdeConfig),
changeSignal: make(chan struct{}, 1),
httpClient: new(http.Client),
closeSignal: make(chan struct{}),
}
c.ctx, c.cancel = context.WithCancel(context.Background())
version, config, err := c.svenConfigSync()
if err != nil {
return nil, err
}
c.config.Store(&MengdeConfig{
Version: version,
Config: config,
})
c.changeSignal <- struct{}{}
c.configLoadWg.Add(1)
go func() {
defer c.configLoadWg.Done()
c.configLoadProcess()
}()
c.configNotifyWg.Add(1)
go func() {
defer c.configNotifyWg.Done()
c.configNotifyProcess()
}()
return c, nil
}
func (c *MengdeClient) Close() {
close(c.closeSignal)
c.cancel()
c.configLoadWg.Wait()
close(c.changeSignal)
c.configNotifyWg.Wait()
}
func (c *MengdeClient) Config() *MengdeConfig {
result := &MengdeConfig{
Version: -1,
Config: make(map[string]string),
}
if config, ok := c.config.Load().(*MengdeConfig); ok {
result.Version = config.Version
for k, v := range config.Config {
result.Config[k] = v
}
}
return result
}
func (c *MengdeClient) ConfigNotify() <-chan *MengdeConfig {
return c.notify
}
func (c *MengdeClient) configLoadProcess() {
for {
select {
case <-c.closeSignal:
return
default:
}
current, ok := c.config.Load().(*MengdeConfig)
if !ok {
current = &MengdeConfig{
Version: -1,
}
}
version, err := c.svenCheckVersion(current.Version)
//config not modified
if version == current.Version {
log.Info("[sven]check version config not modified")
continue
}
if err != nil {
log.Error("[sven]check version error:%s", err.Error())
continue
}
if current.Version == version {
continue
}
config, err := c.svenGetConfig(version)
if err != nil {
log.Error(err.Error())
continue
}
c.config.Store(&MengdeConfig{
Version: version,
Config: config,
})
select {
case c.changeSignal <- struct{}{}:
default:
}
}
}
func (c *MengdeClient) configNotifyProcess() {
for range c.changeSignal {
select {
case <-c.closeSignal:
return
case c.notify <- c.Config():
}
}
}
func (c *MengdeClient) svenConfigSync() (int, map[string]string, error) {
version, err := c.svenCheckVersion(-1)
if err != nil {
return -1, nil, err
}
config, err := c.svenGetConfig(version)
if err != nil {
return -1, nil, err
}
return version, config, nil
}
func (c *MengdeClient) svenCheckVersion(version int) (int, error) {
var err error
req, err := http.NewRequest("GET", kSvenCheckAPI, nil)
if err != nil {
return -1, err
}
q := req.URL.Query()
q.Add("build", c.build)
q.Add("hostname", c.host)
q.Add("ip", c.ip)
q.Add("service", strings.Join([]string{c.treeID, c.env, c.zone}, "_"))
q.Add("token", c.token)
q.Add("version", strconv.Itoa(version))
req.URL.RawQuery = q.Encode()
ctx, cancel := context.WithTimeout(c.ctx, kSvenCheckTimeout)
defer cancel()
req = req.WithContext(ctx)
resp, err := c.httpClient.Do(req)
if err != nil {
return -1, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return -1, errors.New(fmt.Sprintf("http request URL:%s result:%s", req.URL.RawQuery, resp.Status))
}
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
return -1, err
}
var svenCheckRespBody struct {
Code int `json:"code"`
Message string `json:"message"`
Data struct {
Version int `json:"version"`
} `json:"data"`
}
if err = json.Unmarshal(result, &svenCheckRespBody); err != nil {
return -1, err
}
if svenCheckRespBody.Code != 0 {
if svenCheckRespBody.Code == kCodeNotModified {
return version, nil
}
return -1, errors.New(svenCheckRespBody.Message)
}
return svenCheckRespBody.Data.Version, nil
}
func (c *MengdeClient) svenGetConfig(version int) (map[string]string, error) {
var err error
req, err := http.NewRequest("GET", kSvenGetAPI, nil)
if err != nil {
return nil, err
}
q := req.URL.Query()
q.Add("build", c.build)
q.Add("hostname", c.host)
q.Add("ip", c.ip)
q.Add("service", strings.Join([]string{c.treeID, c.env, c.zone}, "_"))
q.Add("token", c.token)
q.Add("version", strconv.Itoa(version))
req.URL.RawQuery = q.Encode()
ctx, cancel := context.WithTimeout(c.ctx, kSvenGetTimeout)
defer cancel()
req = req.WithContext(ctx)
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errors.New(fmt.Sprintf("http request URL:%s result:%s", req.URL.RawQuery, resp.Status))
}
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var svenGetRespBody struct {
Code int `json:"code"`
Message string `json:"message"`
TTL int `json:"ttl"`
Data struct {
Version int `json:"version"`
MD5 string `json:"md5"`
Content string `json:"content"`
} `json:"data"`
}
if err = json.Unmarshal(result, &svenGetRespBody); err != nil {
return nil, err
}
if svenGetRespBody.Code != 0 {
return nil, errors.New(svenGetRespBody.Message)
}
var configContent []struct {
Name string `json:"name"`
Config string `json:"config"`
}
if err = json.Unmarshal([]byte(svenGetRespBody.Data.Content), &configContent); err != nil {
return nil, err
}
config := make(map[string]string)
for _, c := range configContent {
config[c.Name] = strings.TrimSpace(c.Config)
}
return config, nil
}