go fmt & better examples
This commit is contained in:
parent
e701be9288
commit
87631cc2e5
@ -3,10 +3,10 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
"bufio"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// One client connect to one server.
|
// One client connect to one server.
|
||||||
@ -20,7 +20,7 @@ type Client struct {
|
|||||||
in chan *Response
|
in chan *Response
|
||||||
isConn bool
|
isConn bool
|
||||||
conn net.Conn
|
conn net.Conn
|
||||||
rw *bufio.ReadWriter
|
rw *bufio.ReadWriter
|
||||||
|
|
||||||
ErrorHandler ErrorHandler
|
ErrorHandler ErrorHandler
|
||||||
}
|
}
|
||||||
|
@ -50,11 +50,11 @@ const (
|
|||||||
dtSubmitJobLow = 33
|
dtSubmitJobLow = 33
|
||||||
dtSubmitJobLowBg = 34
|
dtSubmitJobLowBg = 34
|
||||||
|
|
||||||
WorkComplate = dtWorkComplete
|
WorkComplate = dtWorkComplete
|
||||||
WorkDate = dtWorkData
|
WorkDate = dtWorkData
|
||||||
WorkStatus = dtWorkStatus
|
WorkStatus = dtWorkStatus
|
||||||
WorkWarning = dtWorkWarning
|
WorkWarning = dtWorkWarning
|
||||||
WorkFail = dtWorkFail
|
WorkFail = dtWorkFail
|
||||||
WorkException = dtWorkException
|
WorkException = dtWorkException
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -11,9 +11,9 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrNotFound = errors.New("Server Not Found")
|
ErrNotFound = errors.New("Server Not Found")
|
||||||
SelectWithRate = selectWithRate
|
SelectWithRate = selectWithRate
|
||||||
SelectRandom = selectRandom
|
SelectRandom = selectRandom
|
||||||
)
|
)
|
||||||
|
|
||||||
type poolClient struct {
|
type poolClient struct {
|
||||||
|
@ -61,7 +61,7 @@ func decodeResponse(data []byte) (resp *Response, l int, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
dl := int(binary.BigEndian.Uint32(data[8:12]))
|
dl := int(binary.BigEndian.Uint32(data[8:12]))
|
||||||
if a < minPacketLength + dl {
|
if a < minPacketLength+dl {
|
||||||
err = fmt.Errorf("Invalid data: %V", data)
|
err = fmt.Errorf("Invalid data: %V", data)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var wg sync.WaitGroup
|
|
||||||
// Set the autoinc id generator
|
// Set the autoinc id generator
|
||||||
// You can write your own id generator
|
// You can write your own id generator
|
||||||
// by implementing IdGenerator interface.
|
// by implementing IdGenerator interface.
|
||||||
@ -22,27 +21,58 @@ func main() {
|
|||||||
log.Println(e)
|
log.Println(e)
|
||||||
}
|
}
|
||||||
echo := []byte("Hello\x00 world")
|
echo := []byte("Hello\x00 world")
|
||||||
wg.Add(1)
|
|
||||||
echomsg, err := c.Echo(echo)
|
echomsg, err := c.Echo(echo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
log.Println(string(echomsg))
|
log.Println(string(echomsg))
|
||||||
wg.Done()
|
|
||||||
jobHandler := func(resp *client.Response) {
|
jobHandler := func(resp *client.Response) {
|
||||||
log.Printf("%s", resp.Data)
|
switch resp.DataType {
|
||||||
wg.Done()
|
case client.WorkException:
|
||||||
|
fallthrough
|
||||||
|
case client.WorkFail:
|
||||||
|
fallthrough
|
||||||
|
case client.WorkComplate:
|
||||||
|
if data, err := resp.Result(); err == nil {
|
||||||
|
log.Printf("RESULT: %V\n", data)
|
||||||
|
} else {
|
||||||
|
log.Printf("RESULT: %s\n", err)
|
||||||
|
}
|
||||||
|
case client.WorkWarning:
|
||||||
|
fallthrough
|
||||||
|
case client.WorkDate:
|
||||||
|
if data, err := resp.Update(); err == nil {
|
||||||
|
log.Printf("UPDATE: %V\n", data)
|
||||||
|
} else {
|
||||||
|
log.Printf("UPDATE: %V, %s\n", data, err)
|
||||||
|
}
|
||||||
|
case client.WorkStatus:
|
||||||
|
if data, err := resp.Status(); err == nil {
|
||||||
|
log.Printf("STATUS: %V\n", data)
|
||||||
|
} else {
|
||||||
|
log.Printf("STATUS: %s\n", err)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
log.Printf("UNKNOWN: %V", resp.Data)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
handle, err := c.Do("ToUpper", echo, client.JobNormal, jobHandler)
|
handle, err := c.Do("ToUpper", echo, client.JobNormal, jobHandler)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
wg.Add(1)
|
|
||||||
status, err := c.Status(handle)
|
status, err := c.Status(handle)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
log.Printf("%t", status)
|
log.Printf("%t", status)
|
||||||
|
|
||||||
wg.Wait()
|
_, err = c.Do("Foobar", echo, client.JobNormal, jobHandler)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("Press Ctrl-C to exit ...")
|
||||||
|
var mutex sync.Mutex
|
||||||
|
mutex.Lock()
|
||||||
|
mutex.Lock()
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,16 @@ func ToUpperDelay10(job worker.Job) ([]byte, error) {
|
|||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Foobar(job worker.Job) ([]byte, error) {
|
||||||
|
log.Printf("Foobar: Data=[%s]\n", job.Data())
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
job.SendWarning([]byte{byte(i)})
|
||||||
|
job.SendData([]byte{byte(i)})
|
||||||
|
job.UpdateStatus(i+1, 100)
|
||||||
|
}
|
||||||
|
return job.Data(), nil
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.Println("Starting ...")
|
log.Println("Starting ...")
|
||||||
defer log.Println("Shutdown complete!")
|
defer log.Println("Shutdown complete!")
|
||||||
@ -44,11 +54,12 @@ func main() {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
w.AddServer("tcp4", "127.0.0.1:4730")
|
w.AddServer("tcp4", "127.0.0.1:4730")
|
||||||
w.AddFunc("ToUpper", ToUpper, worker.Immediately)
|
w.AddFunc("Foobar", Foobar, worker.Unlimited)
|
||||||
|
w.AddFunc("ToUpper", ToUpper, worker.Unlimited)
|
||||||
w.AddFunc("ToUpperTimeOut5", ToUpperDelay10, 5)
|
w.AddFunc("ToUpperTimeOut5", ToUpperDelay10, 5)
|
||||||
w.AddFunc("ToUpperTimeOut20", ToUpperDelay10, 20)
|
w.AddFunc("ToUpperTimeOut20", ToUpperDelay10, 20)
|
||||||
w.AddFunc("SysInfo", worker.SysInfo, worker.Immediately)
|
w.AddFunc("SysInfo", worker.SysInfo, worker.Unlimited)
|
||||||
w.AddFunc("MemInfo", worker.MemInfo, worker.Immediately)
|
w.AddFunc("MemInfo", worker.MemInfo, worker.Unlimited)
|
||||||
if err := w.Ready(); err != nil {
|
if err := w.Ready(); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
return
|
return
|
||||||
|
@ -1,18 +1,18 @@
|
|||||||
package worker
|
package worker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"bufio"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// The agent of job server.
|
// The agent of job server.
|
||||||
type agent struct {
|
type agent struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
conn net.Conn
|
conn net.Conn
|
||||||
rw *bufio.ReadWriter
|
rw *bufio.ReadWriter
|
||||||
worker *Worker
|
worker *Worker
|
||||||
in chan []byte
|
in chan []byte
|
||||||
net, addr string
|
net, addr string
|
||||||
|
@ -62,12 +62,12 @@ func TestInPack(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkDecode(b *testing.B) {
|
func BenchmarkDecode(b *testing.B) {
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
for _, v := range inpackcases {
|
for _, v := range inpackcases {
|
||||||
_, _, err := decodeInPack([]byte(v["src"]))
|
_, _, err := decodeInPack([]byte(v["src"]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Error(err)
|
b.Error(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -83,7 +83,7 @@ func TestOutPack(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkEncode(b *testing.B) {
|
func BenchmarkEncode(b *testing.B) {
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
for k, v := range outpackcases {
|
for k, v := range outpackcases {
|
||||||
outpack := getOutPack()
|
outpack := getOutPack()
|
||||||
outpack.dataType = k
|
outpack.dataType = k
|
||||||
@ -95,5 +95,5 @@ func BenchmarkEncode(b *testing.B) {
|
|||||||
}
|
}
|
||||||
outpack.Encode()
|
outpack.Encode()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user