Skip to content

Commit

Permalink
upgrade version to v0.0.13
Browse files Browse the repository at this point in the history
  • Loading branch information
dobyte committed Mar 16, 2023
1 parent 8074a4c commit 23386f4
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ func push(conn network.Conn, route int32, buffer []byte) error {
* consul: github.com/dobyte/due/registry/consul
4. 传输组件
* grpc: github.com/dobyte/due/transporter/grpc
* rpcx: github.com/dobyte/due/transporter/rpcx
5. 定位组件
* redis: github.com/dobyte/due/locate/redis
6. 事件总线
Expand Down
2 changes: 1 addition & 1 deletion eventbus/kafka/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.16

require (
github.com/Shopify/sarama v1.38.1
github.com/dobyte/due v0.0.12
github.com/dobyte/due v0.0.13
)

replace github.com/dobyte/due => ./../../
2 changes: 1 addition & 1 deletion eventbus/nats/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/dobyte/due/eventbus/nats
go 1.16

require (
github.com/dobyte/due v0.0.12
github.com/dobyte/due v0.0.13
github.com/nats-io/nats-server/v2 v2.9.14 // indirect
github.com/nats-io/nats.go v1.23.0
)
Expand Down
2 changes: 1 addition & 1 deletion eventbus/redis/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/dobyte/due/eventbus/redis
go 1.16

require (
github.com/dobyte/due v0.0.12
github.com/dobyte/due v0.0.13
github.com/go-redis/redis/v8 v8.11.5
)

Expand Down
2 changes: 1 addition & 1 deletion locate/redis/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/dobyte/due/locate/redis
go 1.16

require (
github.com/dobyte/due v0.0.12
github.com/dobyte/due v0.0.13
github.com/go-redis/redis/v8 v8.11.5
github.com/jonboulle/clockwork v0.3.0 // indirect
golang.org/x/sync v0.1.0
Expand Down
2 changes: 1 addition & 1 deletion network/tcp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module github.com/dobyte/due/network/tcp

go 1.16

require github.com/dobyte/due v0.0.12
require github.com/dobyte/due v0.0.13

replace github.com/dobyte/due => ../../
2 changes: 1 addition & 1 deletion network/ws/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/dobyte/due/network/ws
go 1.16

require (
github.com/dobyte/due v0.0.12
github.com/dobyte/due v0.0.13
github.com/gorilla/websocket v1.5.0
)

Expand Down
25 changes: 22 additions & 3 deletions network/ws/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ import (
"github.com/dobyte/due/network"
)

type UpgradeHandler func(w http.ResponseWriter, r *http.Request) (allowed bool)

type Server interface {
network.Server
// OnUpgrade 监听HTTP请求升级
OnUpgrade(handler UpgradeHandler)
}

type server struct {
opts *serverOptions // 配置
listener net.Listener // 监听器
Expand All @@ -25,11 +33,12 @@ type server struct {
connectHandler network.ConnectHandler // 连接打开hook函数
disconnectHandler network.DisconnectHandler // 连接关闭hook函数
receiveHandler network.ReceiveHandler // 接收消息hook函数
upgradeHandler UpgradeHandler // HTTP协议升级成WS协议hook函数
}

var _ network.Server = &server{}
var _ Server = &server{}

func NewServer(opts ...ServerOption) network.Server {
func NewServer(opts ...ServerOption) Server {
o := defaultServerOptions()
for _, opt := range opts {
opt(o)
Expand Down Expand Up @@ -110,13 +119,18 @@ func (s *server) serve() {
return
}

if s.upgradeHandler != nil && !s.upgradeHandler(w, r) {
http.Error(w, "Forbidden", 403)
return
}

conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Errorf("websocket upgrade error: %v", err)
return
}

if err := s.connMgr.allocate(conn); err != nil {
if err = s.connMgr.allocate(conn); err != nil {
_ = conn.Close()
}
})
Expand All @@ -143,6 +157,11 @@ func (s *server) OnStop(handler network.CloseHandler) {
s.stopHandler = handler
}

// OnUpgrade 监听HTTP请求升级
func (s *server) OnUpgrade(handler UpgradeHandler) {
s.upgradeHandler = handler
}

// OnConnect 监听连接打开
func (s *server) OnConnect(handler network.ConnectHandler) {
s.connectHandler = handler
Expand Down
7 changes: 7 additions & 0 deletions network/ws/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
package ws_test

import (
"net/http"
"testing"
"time"

"github.com/dobyte/due/log"
"github.com/dobyte/due/network/ws"
Expand All @@ -34,8 +36,13 @@ func TestServer(t *testing.T) {
log.Errorf("push message failed: %v", err)
}
})
server.OnUpgrade(func(w http.ResponseWriter, r *http.Request) (allowed bool) {
return true
})

if err := server.Start(); err != nil {
log.Fatalf("start server failed: %v", err)
}

time.Sleep(30 * time.Second)
}
2 changes: 1 addition & 1 deletion registry/consul/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/dobyte/due/registry/consul
go 1.16

require (
github.com/dobyte/due v0.0.12
github.com/dobyte/due v0.0.13
github.com/hashicorp/consul/api v1.13.0
)

Expand Down
2 changes: 1 addition & 1 deletion registry/etcd/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/dobyte/due/registry/etcd
go 1.16

require (
github.com/dobyte/due v0.0.12
github.com/dobyte/due v0.0.13
go.etcd.io/etcd/api/v3 v3.5.4
go.etcd.io/etcd/client/v3 v3.5.4
)
Expand Down
2 changes: 1 addition & 1 deletion transport/grpc/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/dobyte/due/transport/grpc
go 1.16

require (
github.com/dobyte/due v0.0.12
github.com/dobyte/due v0.0.13
github.com/golang/protobuf v1.5.2
google.golang.org/grpc v1.50.1
google.golang.org/protobuf v1.28.1 // indirect
Expand Down
2 changes: 1 addition & 1 deletion transport/rpcx/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/dobyte/due/transport/rpcx
go 1.16

require (
github.com/dobyte/due v0.0.12
github.com/dobyte/due v0.0.13
github.com/smallnest/rpcx v1.7.11
)

Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
package due

// Version 框架版本
const Version = "v0.0.12"
const Version = "v0.0.13"

// Website 框架网址
const Website = "https://github.com/dobyte/due"

0 comments on commit 23386f4

Please sign in to comment.