bug fix && nolonger support interface{}

master v1.2.0
兔子 3 years ago
parent 3c9d51a44a
commit 016dea5a2e

@ -1,21 +1,24 @@
package starmap package starmap
import "errors" import (
"errors"
"os"
)
func (stack *StarStack) Count() int { func (stack *StarStackMem) Count() int {
stack.kvPushmu.Lock() stack.kvPushmu.Lock()
defer stack.kvPushmu.Unlock() defer stack.kvPushmu.Unlock()
return len(stack.kvStack) return len(stack.kvStack)
} }
func (stack *StarStack) Push(val interface{}) error { func (stack *StarStackMem) Push(val interface{}) error {
stack.kvPushmu.Lock() stack.kvPushmu.Lock()
defer stack.kvPushmu.Unlock() defer stack.kvPushmu.Unlock()
stack.kvStack = append(stack.kvStack, val) stack.kvStack = append(stack.kvStack, val)
return nil return nil
} }
func (stack *StarStack) Pop() (interface{}, error) { func (stack *StarStackMem) Pop() (interface{}, error) {
stack.kvPushmu.Lock() stack.kvPushmu.Lock()
defer stack.kvPushmu.Unlock() defer stack.kvPushmu.Unlock()
if len(stack.kvStack) == 0 { if len(stack.kvStack) == 0 {
@ -26,59 +29,70 @@ func (stack *StarStack) Pop() (interface{}, error) {
return val, nil return val, nil
} }
func (stack *StarStack) MustPop() interface{} { func (stack *StarStackMem) MustPop() interface{} {
val, _ := stack.Pop() val, _ := stack.Pop()
return val return val
} }
func Get(key string) (interface{}, error) { func Get(key interface{}) (interface{}, error) {
return globalMap.Get(key) return globalMap.Get(key)
} }
func (m *StarMapKV) Get(key string) (interface{}, error) { func (m *StarMapKV) Get(key interface{}) (interface{}, error) {
var err error var err error
m.kvmu.RLock() m.mu.RLock()
defer m.kvmu.RUnlock() defer m.mu.RUnlock()
data, ok := m.kvMap[key] data, ok := m.kvMap[key]
if !ok { if !ok {
err = errors.New("key not exists") err = os.ErrNotExist
} }
return data, err return data, err
} }
func (m *StarMapKV) MustGet(key string) interface{} { func (m *StarMapKV) MustGet(key interface{}) interface{} {
result, _ := m.Get(key) result, _ := m.Get(key)
return result return result
} }
func MustGet(key string) interface{} { func MustGet(key interface{}) interface{} {
return globalMap.MustGet(key) return globalMap.MustGet(key)
} }
func Store(key string, value interface{}) error { func Store(key interface{}, value interface{}) error {
return globalMap.Store(key, value) return globalMap.Store(key, value)
} }
func (m *StarMapKV) Store(key string, value interface{}) error { func (m *StarMapKV) Store(key interface{}, value interface{}) error {
m.kvmu.Lock() m.mu.Lock()
defer m.kvmu.Unlock() defer m.mu.Unlock()
m.kvMap[key] = value m.kvMap[key] = value
return nil return nil
} }
func Delete(key string) error { func Exists(key interface{}) bool {
return globalMap.Exists(key)
}
func (m *StarMapKV) Exists(key interface{}) bool {
m.mu.RLock()
defer m.mu.RUnlock()
_, ok := m.kvMap[key]
return ok
}
func Delete(key interface{}) error {
return globalMap.Delete(key) return globalMap.Delete(key)
} }
func (m *StarMapKV) Delete(key string) error { func (m *StarMapKV) Delete(key interface{}) error {
m.kvmu.Lock() m.mu.Lock()
defer m.kvmu.Unlock() defer m.mu.Unlock()
delete(m.kvMap, key) delete(m.kvMap, key)
return nil return nil
} }
func Range(run func(k string, v interface{}) bool) error { func Range(run func(k interface{}, v interface{}) bool) error {
return globalMap.Range(run) return globalMap.Range(run)
} }
func (m *StarMapKV) Range(run func(k string, v interface{}) bool) error { func (m *StarMapKV) Range(run func(k interface{}, v interface{}) bool) error {
for k, v := range m.kvMap { for k, v := range m.kvMap {
if !run(k, v) { if !run(k, v) {
break break

@ -1,19 +1,11 @@
package starmap package starmap
import ( import (
"errors" "b612.me/notify"
"time"
) )
func (kv *RemoteKv) clientOnline() error { func (kv *RemoteKv) clientOnline() error {
if kv.client == nil || (kv.setKeepAlive && !kv.client.Online) { return kv.reconnect()
return errors.New("Client Not Online,Please Contact A Server First")
}
if (!kv.setKeepAlive) && (!kv.client.Online) {
err := kv.reconnect()
return err
}
return nil
} }
func (kv *RemoteKv) MustGet(key string) interface{} { func (kv *RemoteKv) MustGet(key string) interface{} {
@ -21,77 +13,84 @@ func (kv *RemoteKv) MustGet(key string) interface{} {
return result return result
} }
func (kv *RemoteKv) Get(key string) (interface{}, error) { func (kv *RemoteKv) Get(key interface{}) (interface{}, error) {
kv.kvmu.Lock()
defer kv.kvmu.Unlock()
defer kv.keepalive()
if err := kv.clientOnline(); err != nil { if err := kv.clientOnline(); err != nil {
return nil, err return nil, err
} }
data, err := kv.client.SendValueWait("Get", key, kv.TimeOut) keyCode, err := notify.ToMsgVal(key)
if err != nil { if err != nil {
return nil, err return nil, err
} }
inf, err := decode([]byte(data.Value)) data, err := kv.client.SendWait("get", keyCode, kv.readTimeout)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var repInf *basicReply rp, err := data.Value.ToInterface()
repInf = inf.(*basicReply) if err != nil {
if repInf.Key == "ok" { return nil, err
inf2, err := decode(repInf.Value)
if err != nil {
return nil, err
}
return inf2, nil
} }
return nil, errors.New(string(repInf.Value)) reply := rp.(kvMsg)
return reply.Value, reply.Err
} }
func (kv *RemoteKv) Store(key string, value interface{}) error { func (kv *RemoteKv) Store(key interface{}, value interface{}) error {
kv.kvmu.Lock()
defer kv.kvmu.Unlock()
defer kv.keepalive()
if err := kv.clientOnline(); err != nil { if err := kv.clientOnline(); err != nil {
return err return err
} }
encodeData, err := encode(value) encodeData, err := notify.ToMsgVal(kvMsg{
Key: key,
Value: value,
Err: nil,
})
if err != nil { if err != nil {
return err return err
} }
sendData, _ := encode(basicReply{key, encodeData}) data, err := kv.client.SendWait("store", encodeData, kv.readTimeout)
data, err := kv.client.SendValueWait("Store", string(sendData), kv.TimeOut)
if err != nil { if err != nil {
return err return err
} }
if data.Value == "ok" { rp, err := data.Value.ToInterface()
return nil if err != nil {
return err
} }
return errors.New(data.Value) return rp.(kvMsg).Err
} }
func (kv *RemoteKv) Delete(key string) error { func (kv *RemoteKv) Delete(key interface{}) error {
kv.kvmu.Lock()
defer kv.kvmu.Unlock()
defer kv.keepalive()
if err := kv.clientOnline(); err != nil { if err := kv.clientOnline(); err != nil {
return err return err
} }
data, err := kv.client.SendValueWait("Delete", key, kv.TimeOut) keyCode, err := notify.ToMsgVal(key)
if err != nil { if err != nil {
return err return err
} }
if data.Value == "ok" { data, err := kv.client.SendWait("delete", keyCode, kv.readTimeout)
return nil if err != nil {
return err
} }
return errors.New(data.Value) rp, err := data.Value.ToInterface()
if err != nil {
return err
}
return rp.(kvMsg).Err
} }
func (kv *RemoteKv) keepalive() { func (kv *RemoteKv) Exists(key interface{}) bool {
if !kv.setKeepAlive && kv.client.Online { if err := kv.clientOnline(); err != nil {
kv.client.ClientStop() return false
for kv.client.Online { }
time.Sleep(time.Millisecond) keyCode, err := notify.ToMsgVal(key)
} if err != nil {
return false
}
data, err := kv.client.SendWait("exists", keyCode, kv.readTimeout)
if err != nil {
return false
}
rp, err := data.Value.ToInterface()
if err != nil {
return false
} }
reply := rp.(kvMsg)
return reply.Value.(bool)
} }

@ -1,55 +0,0 @@
package starmap
import (
"errors"
)
func (kv *RemoteKv) GetMap(key string) (StarMap, error) {
kv.kvmu.Lock()
defer kv.kvmu.Unlock()
defer kv.keepalive()
recv, err := kv.client.SendValueWait("GetMap", key, kv.TimeOut)
if err != nil {
return StarMapSample{}, err
}
if recv.Key == "error" {
return StarMapSample{}, errors.New(recv.Value)
}
value, err := decodeMap([]byte(recv.Value))
if err != nil {
return StarMapSample{}, err
}
return value, nil
}
func (kv *RemoteKv) StoreMap(value StarMap) error {
kv.kvmu.Lock()
defer kv.kvmu.Unlock()
defer kv.keepalive()
encodeData, err := encodeMap(value)
if err != nil {
return err
}
recv, err := kv.client.SendValueWait("StoreMap", string(encodeData), kv.TimeOut)
if err != nil {
return err
}
if recv.Key == "error" {
return errors.New(recv.Value)
}
return nil
}
func (kv *RemoteKv) DeleteMap(value StarMap) error {
kv.kvmu.Lock()
defer kv.kvmu.Unlock()
defer kv.keepalive()
recv, err := kv.client.SendValueWait("DeleteMap", value.GetName(), kv.TimeOut)
if err != nil {
return err
}
if recv.Key == "error" {
return errors.New(recv.Value)
}
return nil
}

@ -1,48 +0,0 @@
package starmap
import (
"errors"
)
type StarMap interface {
GetName() string
}
type StarMapSample struct{}
func (sample StarMapSample) GetName() string {
return "starsample"
}
func GetMap(value StarMap) error {
return globalMap.GetMap(value)
}
func (m *StarMapKV) GetMap(value StarMap) error {
m.kvmuPro.RLock()
defer m.kvmuPro.RUnlock()
value, ok := m.kvMapPro[value.GetName()]
if !ok {
return errors.New("key not exists")
}
return nil
}
func StoreMap(value StarMap) error {
return globalMap.StoreMap(value)
}
func (m *StarMapKV) StoreMap(value StarMap) error {
m.kvmuPro.Lock()
defer m.kvmuPro.Unlock()
m.kvMapPro[value.GetName()] = value
return nil
}
func DeleteMap(value StarMap) error {
return globalMap.DeleteMap(value)
}
func (m *StarMapKV) DeleteMap(value StarMap) error {
m.kvmuPro.RLock()
defer m.kvmuPro.RUnlock()
delete(m.kvMapPro, value.GetName())
return nil
}

@ -2,47 +2,73 @@ package starmap
import ( import (
"encoding/gob" "encoding/gob"
"errors" "os"
"sync"
"time" "time"
"b612.me/notify" "b612.me/notify"
) )
func init() {
notify.Register(kvMsg{})
notify.Register(starMapErr{})
}
type starMapErr struct {
Err string
}
func (s starMapErr) Error() string {
return s.Err
}
func newStarMapErr(err error) error {
if err == nil {
return nil
}
return starMapErr{Err: err.Error()}
}
type kvMsg struct {
Key interface{}
Value interface{}
Err error
}
type RemoteKv struct { type RemoteKv struct {
kvmu sync.RWMutex server notify.Server
server *notify.StarNotifyS client notify.Client
client *notify.StarNotifyC kvmap StarMapKV
addr string addr string
setKeepAlive bool network string
TimeOut time.Duration readTimeout time.Duration
ErrMsg string timeout time.Duration
kvmap StarMapKV
} }
func NewServer(addr string) (*RemoteKv, error) { func NewServer(network, addr string) (*RemoteKv, error) {
var kv RemoteKv
var err error var err error
kv.addr = addr kv := RemoteKv{
kv.TimeOut = time.Second * 10 server: notify.NewServer(),
kv.kvmap = NewStarMap() kvmap: NewStarMap(),
kv.server, err = notify.NewNotifyS("tcp", addr) addr: addr,
network: network,
}
err = kv.server.Listen(network, addr)
if err == nil { if err == nil {
kv.bind() kv.bind()
} }
return &kv, err return &kv, err
} }
func NewClient(addr string) (*RemoteKv, error) { func NewClient(network, addr string, dialTimeout time.Duration) (*RemoteKv, error) {
var err error var err error
var kv RemoteKv kv := RemoteKv{
kv.addr = addr client: notify.NewClient(),
kv.TimeOut = time.Second * 10 kvmap: NewStarMap(),
kv.client, err = notify.NewNotifyC("tcp", addr) addr: addr,
if err == nil { network: network,
kv.setKeepAlive = true timeout: dialTimeout,
kv.client.UseChannel = false readTimeout: time.Second * 5,
} }
err = kv.client.ConnectTimeout(network, addr, dialTimeout)
return &kv, err return &kv, err
} }
@ -56,38 +82,21 @@ func (kv *RemoteKv) RegisterAll(data []interface{}) {
} }
} }
func (kv *RemoteKv) SetKeepAlive(alive bool) { func (kv *RemoteKv) bind() {
kv.setKeepAlive = alive //for server
} kv.server.SetDefaultLink(kv.dispatch)
//for client
func (kv *RemoteKv) GetKeepAlive() bool {
return kv.setKeepAlive
} }
func (kv *RemoteKv) reconnect() error { func (kv *RemoteKv) reconnect() error {
var err error if kv.server != nil {
if kv.addr != "" { return nil
kv.client, err = notify.NewNotifyC("tcp", kv.addr)
if err == nil {
kv.client.UseChannel = false
}
return err
} }
return errors.New("Client Not Online!") if kv.client != nil {
} if kv.client.Status().Alive {
return nil
func (kv *RemoteKv) bind() { }
kv.server.SetNotify("Get", kv.get) return kv.client.ConnectTimeout(kv.network, kv.addr, kv.timeout)
kv.server.SetNotify("Store", kv.store)
kv.server.SetNotify("Delete", kv.store)
kv.server.SetNotify("GetMap", kv.getMap)
kv.server.SetNotify("StoreMap", kv.storeMap)
kv.server.SetNotify("DeleteMap", kv.deleteMap)
}
func (kv *RemoteKv) ErrTimeOut(err error) bool {
if err.Error() == "Time Exceed" {
return true
} }
return false return os.ErrInvalid
} }

@ -1,63 +1,39 @@
package starmap package starmap
import "b612.me/notify" import (
"b612.me/notify"
)
type basicReply struct { func (r *RemoteKv) dispatch(msg *notify.Message) {
Key string switch msg.Key {
Value []byte case "get":
} data, err := r.kvmap.Get(msg.Value.MustToInterface())
msg.ReplyObj(kvMsg{
func (kv *RemoteKv) get(data notify.SMsg) string { Key: msg.Value.MustToInterface(),
key := data.Value Value: data,
value, err := kv.kvmap.Get(key) Err: newStarMapErr(err),
if err != nil { })
sendData, err := encode(basicReply{"error", []byte("key not exists")}) case "delete":
if err != nil { err := r.kvmap.Delete(msg.Value.MustToInterface())
kv.ErrMsg += err.Error() msg.ReplyObj(kvMsg{
return "" Key: msg.Value.MustToInterface(),
} Value: nil,
data.Reply(string(sendData)) Err: newStarMapErr(err),
} else { })
prepareData, err := encode(value) case "exists":
if err != nil { ext := r.kvmap.Exists(msg.Value.MustToInterface())
kv.ErrMsg += err.Error() msg.ReplyObj(kvMsg{
sendData, _ := encode(basicReply{"error", []byte(err.Error())}) Key: msg.Value.MustToInterface(),
data.Reply(string(sendData)) Value: ext,
return "" Err: newStarMapErr(nil),
} })
sendData, err := encode(basicReply{"ok", prepareData}) case "store":
if err != nil { ext := msg.Value.MustToInterface().(kvMsg)
kv.ErrMsg += err.Error() err := r.kvmap.Store(ext.Key, ext.Value)
sendData, _ := encode(basicReply{"error", []byte(err.Error())}) msg.ReplyObj(kvMsg{
data.Reply(string(sendData)) Key: msg.Value.MustToInterface(),
return "" Value: nil,
} Err: newStarMapErr(err),
data.Reply(string(sendData)) })
}
return ""
}
func (kv *RemoteKv) store(data notify.SMsg) string {
decodeData, err := decode([]byte(data.Value))
if err != nil {
return err.Error()
}
inf := decodeData.(*basicReply)
customStruct, err := decode(inf.Value)
if err != nil {
return err.Error()
}
err = kv.kvmap.Store(inf.Key, customStruct)
if err != nil {
return err.Error()
}
return "ok"
}
func (kv *RemoteKv) delete(data notify.SMsg) string {
err := kv.kvmap.Delete(data.Value)
if err != nil {
return err.Error()
} }
return "ok"
} }

@ -1,48 +0,0 @@
package starmap
import (
"b612.me/notify"
)
func (kv *RemoteKv) getMap(data notify.SMsg) string {
kv.kvmu.Lock()
defer kv.kvmu.Unlock()
val, ok := kv.kvmap.kvMapPro[data.Value]
if !ok {
data.Key = "error"
data.Reply("key not exists")
return ""
}
encodeData, err := encodeMap(val)
if err != nil {
data.Key = "error"
data.Reply(err.Error())
return ""
}
return string(encodeData)
}
func (kv *RemoteKv) storeMap(data notify.SMsg) string {
kv.kvmu.Lock()
defer kv.kvmu.Unlock()
recvData, err := decodeMap([]byte(data.Value))
if err != nil {
data.Key = "error"
data.Reply(err.Error())
return ""
}
err = kv.kvmap.StoreMap(recvData)
if err != nil {
data.Key = "error"
data.Reply(err.Error())
return ""
}
return "ok"
}
func (kv *RemoteKv) deleteMap(data notify.SMsg) string {
kv.kvmu.Lock()
defer kv.kvmu.Unlock()
delete(kv.kvmap.kvMapPro, data.Value)
return "ok"
}

@ -0,0 +1,199 @@
package starmap
import (
"errors"
"fmt"
"io"
"os"
"runtime"
"sync"
"sync/atomic"
"time"
)
type StarStack struct {
datas []interface{}
pStart uint64
pEnd uint64
cap uint64
isClose atomic.Value
rmu sync.Mutex
wmu sync.Mutex
}
func NewStarStack(cap uint64) *StarStack {
rtnBuffer := new(StarStack)
rtnBuffer.cap = cap
rtnBuffer.datas = make([]interface{}, cap)
rtnBuffer.isClose.Store(false)
return rtnBuffer
}
func (star *StarStack) init() {
star.cap = 1024
star.datas = make([]interface{}, star.cap)
star.isClose.Store(false)
}
func (star *StarStack) Free() uint64 {
return star.cap - star.Len()
}
func (star *StarStack) Cap() uint64 {
return star.cap
}
func (star *StarStack) Len() uint64 {
if star.pEnd >= star.pStart {
return star.pEnd - star.pStart
}
return star.pEnd - star.pStart + star.cap
}
func (star *StarStack) PopNoWait() (interface{}, error) {
if star.isClose.Load() == nil {
star.init()
}
if star.isClose.Load().(bool) {
return 0, io.EOF
}
if star.Len() == 0 {
return 0, os.ErrNotExist
}
nowPtr := star.pStart
nextPtr := star.pStart + 1
if nextPtr >= star.cap {
nextPtr = 0
}
data := star.datas[nowPtr]
ok := atomic.CompareAndSwapUint64(&star.pStart, nowPtr, nextPtr)
if !ok {
return 0, os.ErrInvalid
}
return data, nil
}
func (star *StarStack) MustPop() interface{} {
if star.isClose.Load() == nil {
star.init()
}
data, err := star.Pop()
if err != nil {
return nil
}
return data
}
func (star *StarStack) Pop() (interface{}, error) {
if star.isClose.Load() == nil {
star.init()
}
for {
if star.isClose.Load().(bool) {
return 0, io.EOF
}
if star.Len() == 0 {
return 0, os.ErrNotExist
}
nowPtr := star.pStart
nextPtr := star.pStart + 1
if nextPtr >= star.cap {
nextPtr = 0
}
data := star.datas[nowPtr]
ok := atomic.CompareAndSwapUint64(&star.pStart, nowPtr, nextPtr)
if !ok {
time.Sleep(time.Microsecond)
runtime.Gosched()
continue
}
return data, nil
}
}
func (star *StarStack) Push(data interface{}) error {
if star.isClose.Load() == nil {
star.init()
}
if star.isClose.Load().(bool) {
return io.EOF
}
nowPtr := star.pEnd
kariEnd := nowPtr + 1
if kariEnd == star.cap {
kariEnd = 0
}
if kariEnd == atomic.LoadUint64(&star.pStart) {
for {
time.Sleep(time.Microsecond)
runtime.Gosched()
if kariEnd != atomic.LoadUint64(&star.pStart) {
break
}
}
}
star.datas[nowPtr] = data
if ok := atomic.CompareAndSwapUint64(&star.pEnd, nowPtr, kariEnd); !ok {
return os.ErrInvalid
}
return nil
}
func (star *StarStack) Close() error {
if star.isClose.Load() == nil {
star.init()
}
star.isClose.Store(true)
return nil
}
func (star *StarStack) Read(buf []interface{}) (int, error) {
if star.isClose.Load() == nil {
star.init()
}
if star.isClose.Load().(bool) {
return 0, io.EOF
}
if buf == nil {
return 0, errors.New("buffer is nil")
}
star.rmu.Lock()
defer star.rmu.Unlock()
var sum int = 0
for i := 0; i < len(buf); i++ {
data, err := star.PopNoWait()
if err != nil {
if err == io.EOF {
return sum, err
}
if err == os.ErrNotExist {
i--
continue
}
return sum, nil
}
buf[i] = data
sum++
}
return sum, nil
}
func (star *StarStack) Write(bts []byte) (int, error) {
if star.isClose.Load() == nil {
star.init()
}
if bts == nil || star.isClose.Load().(bool) {
return 0, io.EOF
}
star.wmu.Lock()
defer star.wmu.Unlock()
var sum = 0
for i := 0; i < len(bts); i++ {
err := star.Push(bts[i])
if err != nil {
fmt.Println("Write bts err:", err)
return sum, err
}
sum++
}
return sum, nil
}

@ -0,0 +1,33 @@
package starmap
import (
"fmt"
"sync/atomic"
"testing"
"time"
)
func Test_Circle_Speed(t *testing.T) {
buf := StarStack{}
count := uint64(0)
for i := 1; i <= 10; i++ {
go func() {
for {
buf.Push('a')
}
}()
}
for i := 1; i <= 10; i++ {
go func() {
for {
_, err := buf.Pop()
if err == nil {
atomic.AddUint64(&count, 1)
}
}
}()
}
time.Sleep(time.Second * 10)
fmt.Println(count)
}

@ -5,13 +5,11 @@ import "sync"
var globalMap StarMapKV var globalMap StarMapKV
type StarMapKV struct { type StarMapKV struct {
kvMap map[string]interface{} kvMap map[interface{}]interface{}
kvmu sync.RWMutex mu sync.RWMutex
kvMapPro map[string]StarMap
kvmuPro sync.RWMutex
} }
type StarStack struct { type StarStackMem struct {
kvPushmu sync.RWMutex kvPushmu sync.RWMutex
kvStack []interface{} kvStack []interface{}
} }
@ -22,7 +20,6 @@ func init() {
func NewStarMap() StarMapKV { func NewStarMap() StarMapKV {
var mp StarMapKV var mp StarMapKV
mp.kvMap = make(map[string]interface{}) mp.kvMap = make(map[interface{}]interface{})
mp.kvMapPro = make(map[string]StarMap)
return mp return mp
} }

@ -15,32 +15,22 @@ type Miaomiao struct {
func Test_Remote(t *testing.T) { func Test_Remote(t *testing.T) {
Store("nmb", 22222) Store("nmb", 22222)
server, err := NewServer("127.0.0.1:45678") server, _ := NewServer("tcp", "127.0.0.1:45678")
server.Register(&Miaomiao{}) server.Register(&Miaomiao{})
fmt.Println(err) client, _ := NewClient("tcp", "127.0.0.1:45678", time.Second*2)
client, err := NewClient("127.0.0.1:45678")
client.SetKeepAlive(false)
fmt.Println(err)
_ = server _ = server
fmt.Println(client.Get("nmb")) fmt.Println(client.Get("maio"))
fmt.Println(client.Exists("maio"))
fmt.Println(client.Store("maio", Miaomiao{"sss", 222, true})) fmt.Println(client.Store("maio", Miaomiao{"sss", 222, true}))
fmt.Println(client.Get("maio")) fmt.Println(client.Get("maio"))
fmt.Println(client.Exists("maio"))
fmt.Println(client.Delete("maio"))
fmt.Println(client.Exists("maio"))
} }
func (cat *Miaomiao) GetName() string { func (cat *Miaomiao) GetName() string {
return "miaomiao" return "miaomiao"
} }
func Test_Remote2(t *testing.T) {
server, err := NewServer("127.0.0.1:45678")
server.Register(&Miaomiao{})
fmt.Println(err)
client, err := NewClient("127.0.0.1:45678")
//client.SetKeepAlive(false)
fmt.Println(err)
_ = server
fmt.Println(client.StoreMap(&Miaomiao{"suki", 1127, false}))
fmt.Println(client.GetMap("miaomiao"))
}
func Test_Math(t *testing.T) { func Test_Math(t *testing.T) {
wg := NewWaitGroup(5000) wg := NewWaitGroup(5000)

@ -25,7 +25,7 @@ func (swg *WaitGroup) Add(delta int) {
} }
for { for {
allC := atomic.LoadUint32(&swg.allCount) allC := atomic.LoadUint32(&swg.allCount)
if atomic.LoadUint32(&swg.maxCount) >= allC+uint32(delta) { if atomic.LoadUint32(&swg.maxCount) == 0 || atomic.LoadUint32(&swg.maxCount) >= allC+uint32(delta) {
if delta < 0 { if delta < 0 {
atomic.AddUint32(&swg.allCount, ^uint32(Udelta)) atomic.AddUint32(&swg.allCount, ^uint32(Udelta))
} else { } else {

@ -1,38 +0,0 @@
package starmap
import (
"bytes"
"encoding/gob"
)
func init() {
gob.Register(&basicReply{})
}
func encode(src interface{}) ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(&src)
return buf.Bytes(), err
}
func decode(src []byte) (interface{}, error) {
dec := gob.NewDecoder(bytes.NewReader(src))
var dst interface{}
err := dec.Decode(&dst)
return dst, err
}
func encodeMap(src StarMap) ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(&src)
return buf.Bytes(), err
}
func decodeMap(src []byte) (StarMap, error) {
dec := gob.NewDecoder(bytes.NewReader(src))
var dst StarMap
err := dec.Decode(&dst)
return dst, err
}
Loading…
Cancel
Save