Commit de0b1b51 authored by wongoo's avatar wongoo

add getty demo

parent cd6ab9c8
# Run Hello Demo
```bash
git clone https://github.com/dubbogo/getty.git
cd getty/demo/hello
# run server
go run tcp/server/server.go
# run client
go run tcp/client/client.go
```
\ No newline at end of file
/******************************************************
# MAINTAINER : wongoo
# LICENCE : Apache License 2.0
# EMAIL : gelnyang@163.com
# MOD : 2019-06-11
******************************************************/
package hello
import (
"github.com/dubbogo/getty"
"time"
)
const (
CronPeriod = 20 * time.Second
WritePkgTimeout = 1e8
)
var (
log = getty.GetLogger()
)
/******************************************************
# MAINTAINER : wongoo
# LICENCE : Apache License 2.0
# EMAIL : gelnyang@163.com
# MOD : 2019-06-11
******************************************************/
package hello
import (
"github.com/dubbogo/getty"
)
var (
Sessions []getty.Session
)
func ClientRequest() {
for _, session := range Sessions {
go func() {
echoTimes := 10
for i := 0; i < echoTimes; i++ {
err := session.WritePkg("hello", WritePkgTimeout)
if err != nil {
log.Infof("session.WritePkg(session{%s}, error{%v}", session.Stat(), err)
session.Close()
}
}
log.Infof("after loop %d times", echoTimes)
}()
}
}
/******************************************************
# MAINTAINER : wongoo
# LICENCE : Apache License 2.0
# EMAIL : gelnyang@163.com
# MOD : 2019-06-11
******************************************************/
package hello
import (
"github.com/dubbogo/getty"
"time"
)
type MessageHandler struct {
SessionOnOpen func(session getty.Session)
}
func (h *MessageHandler) OnOpen(session getty.Session) error {
log.Infof("OnOpen session{%s} open", session.Stat())
if h.SessionOnOpen != nil {
h.SessionOnOpen(session)
}
return nil
}
func (h *MessageHandler) OnError(session getty.Session, err error) {
log.Infof("OnError session{%s} got error{%v}, will be closed.", session.Stat(), err)
}
func (h *MessageHandler) OnClose(session getty.Session) {
log.Infof("OnClose session{%s} is closing......", session.Stat())
}
func (h *MessageHandler) OnMessage(session getty.Session, pkg interface{}) {
s, ok := pkg.(string)
if !ok {
log.Infof("illegal package{%#v}", pkg)
return
}
log.Infof("OnMessage: %s", s)
}
func (h *MessageHandler) OnCron(session getty.Session) {
active := session.GetActive()
if CronPeriod.Nanoseconds() < time.Since(active).Nanoseconds() {
log.Infof("OnCorn session{%s} timeout{%s}", session.Stat(), time.Since(active).String())
session.Close()
}
}
/******************************************************
# MAINTAINER : wongoo
# LICENCE : Apache License 2.0
# EMAIL : gelnyang@163.com
# MOD : 2019-06-11
******************************************************/
package hello
import (
"errors"
"github.com/dubbogo/getty"
)
type PackageHandler struct{}
func (h *PackageHandler) Read(ss getty.Session, data []byte) (interface{}, int, error) {
s := string(data)
return s, len(s), nil
}
func (h *PackageHandler) Write(ss getty.Session, pkg interface{}) error {
s, ok := pkg.(string)
if !ok {
log.Infof("illegal pkg:%+v", pkg)
return errors.New("invalid package")
}
return ss.WriteBytes([]byte(s))
}
/******************************************************
# MAINTAINER : wongoo
# LICENCE : Apache License 2.0
# EMAIL : gelnyang@163.com
# MOD : 2019-06-11
******************************************************/
package main
import (
"flag"
"github.com/dubbogo/getty"
"github.com/dubbogo/getty/demo/hello"
"github.com/dubbogo/getty/demo/hello/tcp"
"github.com/dubbogo/getty/demo/util"
)
var (
ip = flag.String("ip", "127.0.0.1", "server IP")
connections = flag.Int("conn", 1, "number of tcp connections")
)
func main() {
flag.Parse()
util.SetLimit()
client := getty.NewTCPClient(
getty.WithServerAddress(*ip+":8090"),
getty.WithConnectionNumber(*connections),
)
client.RunEventLoop(tcp.NewHelloClientSession)
go hello.ClientRequest()
util.WaitCloseSignals(client)
}
/******************************************************
# MAINTAINER : wongoo
# LICENCE : Apache License 2.0
# EMAIL : gelnyang@163.com
# MOD : 2019-06-11
******************************************************/
package tcp
import (
"fmt"
"github.com/dubbogo/getty"
"github.com/dubbogo/getty/demo/hello"
"net"
"time"
)
var (
pkgHandler = &hello.PackageHandler{}
eventListener = &hello.MessageHandler{}
)
func NewHelloClientSession(session getty.Session) (err error) {
eventListener.SessionOnOpen = func(session getty.Session) {
hello.Sessions = append(hello.Sessions, session)
}
return InitialSession(session)
}
func InitialSession(session getty.Session) (err error) {
session.SetCompressType(getty.CompressZip)
tcpConn, ok := session.Conn().(*net.TCPConn)
if !ok {
panic(fmt.Sprintf("newSession: %s, session.conn{%#v} is not tcp connection", session.Stat(), session.Conn()))
}
if err = tcpConn.SetNoDelay(true); err != nil {
return err
}
if err = tcpConn.SetKeepAlive(true); err != nil {
return err
}
if err = tcpConn.SetKeepAlivePeriod(10 * time.Second); err != nil {
return err
}
if err = tcpConn.SetReadBuffer(262144); err != nil {
return err
}
if err = tcpConn.SetWriteBuffer(524288); err != nil {
return err
}
session.SetName("hello")
session.SetMaxMsgLen(128)
session.SetRQLen(1024)
session.SetWQLen(512)
session.SetReadTimeout(time.Second)
session.SetWriteTimeout(5 * time.Second)
session.SetCronPeriod(int(hello.CronPeriod.Nanoseconds() / 1e6))
session.SetWaitTime(time.Second)
session.SetPkgHandler(pkgHandler)
session.SetEventListener(eventListener)
return nil
}
/******************************************************
# MAINTAINER : wongoo
# LICENCE : Apache License 2.0
# EMAIL : gelnyang@163.com
# MOD : 2019-06-11
******************************************************/
package main
import (
"flag"
"github.com/dubbogo/getty"
"github.com/dubbogo/getty/demo/hello/tcp"
"github.com/dubbogo/getty/demo/util"
)
var (
taskPollMode = flag.Bool("taskPool", false, "task pool mode")
taskPollQueueLength = flag.Int("task_queue_length", 100, "task queue length")
taskPollQueueNumber = flag.Int("task_queue_number", 4, "task queue number")
taskPollSize = flag.Int("task_pool_size", 2000, "task poll size")
)
var (
taskPoll *getty.TaskPool
)
func main() {
flag.Parse()
util.SetLimit()
options := []getty.ServerOption{getty.WithLocalAddress(":8090")}
if *taskPollMode {
taskPoll = getty.NewTaskPool(
getty.WithTaskPoolTaskQueueLength(*taskPollQueueLength),
getty.WithTaskPoolTaskQueueNumber(*taskPollQueueNumber),
getty.WithTaskPoolTaskPoolSize(*taskPollSize),
)
}
server := getty.NewTCPServer(options...)
go server.RunEventLoop(NewHelloServerSession)
util.WaitCloseSignals(server)
}
func NewHelloServerSession(session getty.Session) (err error) {
err = tcp.InitialSession(session)
if err != nil {
return
}
session.SetTaskPool(taskPoll)
return
}
// +build linux
/******************************************************
# MAINTAINER : wongoo
# LICENCE : Apache License 2.0
# EMAIL : gelnyang@163.com
# MOD : 2019-06-11
******************************************************/
package util
import "syscall"
func SetLimit() {
var rLimit syscall.Rlimit
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
panic(err)
}
rLimit.Cur = rLimit.Max
if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
panic(err)
}
}
// +build !linux
/******************************************************
# MAINTAINER : wongoo
# LICENCE : Apache License 2.0
# EMAIL : gelnyang@163.com
# MOD : 2019-06-11
******************************************************/
package util
func SetLimit() {
}
/******************************************************
# MAINTAINER : wongoo
# LICENCE : Apache License 2.0
# EMAIL : gelnyang@163.com
# MOD : 2019-06-11
******************************************************/
package util
import (
"os"
"os/signal"
"syscall"
)
type Closer interface {
Close()
}
func WaitCloseSignals(closer Closer) {
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, os.Kill, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
<-signals
closer.Close()
}
......@@ -85,6 +85,11 @@ func SetLogger(logger Logger) {
log = logger
}
// GetLogger get getty logger
func GetLogger() Logger {
return log
}
// SetLoggerLevel
func SetLoggerLevel(level LoggerLevel) error {
var err error
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment