add timeouts to all websocket emits
This commit is contained in:
1 parent
5a91184d6c
commit
e765cda3f9
9 files changed
+98
-97
No files matched your search
@@ -9,7 +9,7 @@ require (
|
||||
github.com/hashicorp/raft v1.2.0
|
||||
github.com/hashicorp/raft-boltdb/v2 v2.0.0-20210409134258-03c10cc3d4ea
|
||||
github.com/integration-system/bellows v1.0.1
|
||||
github.com/integration-system/isp-etp-go/v2 v2.1.2
|
||||
github.com/integration-system/isp-etp-go/v2 v2.1.3-0.20210430094727-13f5801ec901
|
||||
github.com/integration-system/isp-lib-test v1.6.3
|
||||
github.com/integration-system/isp-lib/v2 v2.8.4
|
||||
github.com/integration-system/isp-log v1.1.6
|
||||
|
||||
@@ -259,8 +259,9 @@ github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iP
|
||||
github.com/hashicorp/go-msgpack v0.5.5 h1:i9R9JSrqIz0QVLz3sz+i3YJdT7TTSLcfLLzJi9aZTuI=
|
||||
github.com/hashicorp/go-msgpack v0.5.5/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM=
|
||||
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
|
||||
github.com/hashicorp/go-multierror v1.1.0 h1:B9UzwGQJehnUY1yNrnwREHc3fGbC2xefo8g4TbElacI=
|
||||
github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA=
|
||||
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
|
||||
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
|
||||
github.com/hashicorp/go-retryablehttp v0.5.3/go.mod h1:9B5zBasrRhHXnJnui7y6sL7es7NDiJgTc6Er0maI1Xs=
|
||||
github.com/hashicorp/go-rootcerts v1.0.0/go.mod h1:K6zTfqpRlCUIjkwsN4Z+hiSfzSTQa6eBIzfwKfwNnHU=
|
||||
github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerXegt+ozgdvDeDU=
|
||||
@@ -301,8 +302,9 @@ github.com/integration-system/go-cmp v0.0.0-20190131081942-ac5582987a2f h1:52UOJ
|
||||
github.com/integration-system/go-cmp v0.0.0-20190131081942-ac5582987a2f/go.mod h1:w+dmaWojuIwNujSYg6qt1wKHhSdorLOivCEl/XuLzbM=
|
||||
github.com/integration-system/gowsdl v1.0.0/go.mod h1:ZwTCE+Av5aQLnPj2/3PIop6mzCs1FY7jXrcWDvSjrBA=
|
||||
github.com/integration-system/isp-etp-go/v2 v2.1.1/go.mod h1:Ndq5DMM3sDJjZn4cyKKvZrVED0FMeoXMRWHZTq+2Z1o=
|
||||
github.com/integration-system/isp-etp-go/v2 v2.1.2 h1:K3mZUm71VuaaBAriMxOp2G/Wql+pgaryI1MZzr81H1U=
|
||||
github.com/integration-system/isp-etp-go/v2 v2.1.2/go.mod h1:fSkS09/engepaDDqaDAEeXUpzmtcMpSLjilZ35BfNcc=
|
||||
github.com/integration-system/isp-etp-go/v2 v2.1.3-0.20210430094727-13f5801ec901 h1:NMGklTh9ninOUdykg20nJXa/bJ5DtYP41TNCVUwpJyE=
|
||||
github.com/integration-system/isp-etp-go/v2 v2.1.3-0.20210430094727-13f5801ec901/go.mod h1:ZpB+FIH3hy7md+f8ylVSQx5zwZVjczhkc45f1B2z9nw=
|
||||
github.com/integration-system/isp-event-lib v1.6.0/go.mod h1:lCzAci6oS+vc51W8EfHVCh9p3p057r0DpMuY4T0lSj0=
|
||||
github.com/integration-system/isp-event-lib v1.6.1 h1:8frVxZKGEjBH1M5ghVrMijQzLfM9F45uiN403IgJJao=
|
||||
github.com/integration-system/isp-event-lib v1.6.1/go.mod h1:5ZIXsMG3gC7sfQ+eICjkDpy2RSvSAxV3Cq1BVN8LFIk=
|
||||
@@ -876,6 +878,7 @@ mellium.im/sasl v0.2.1 h1:nspKSRg7/SyO0cRGY71OkfHab8tf9kCts6a6oTDut0w=
|
||||
mellium.im/sasl v0.2.1/go.mod h1:ROaEDLQNuf9vjKqE1SrAfnsobm2YKXT1gnN1uDp1PjQ=
|
||||
nhooyr.io/websocket v1.7.4/go.mod h1:PxYxCwFdFYQ0yRvtQz3s/dC+VEm7CSuC/4b9t8MQQxw=
|
||||
nhooyr.io/websocket v1.8.4/go.mod h1:LiqdCg1Cu7TPWxEvPjPa0TGYxCsy4pHNTN9gGluwBpQ=
|
||||
nhooyr.io/websocket v1.8.6 h1:s+C3xAMLwGmlI31Nyn/eAehUlZPwfYZu2JXM621Q5/k=
|
||||
nhooyr.io/websocket v1.8.6/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=
|
||||
nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g=
|
||||
nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=
|
||||
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
|
||||
@@ -231,13 +231,13 @@ func gracefulShutdown() {
|
||||
|
||||
func onShutdown(ctx context.Context) {
|
||||
backend.StopGrpcServer()
|
||||
holder.EtpServer.Close()
|
||||
|
||||
if err := holder.HTTPServer.Shutdown(ctx); err != nil {
|
||||
log.Warnf(codes.ShutdownHttpServerError, "http server shutdown err: %v", err)
|
||||
} else {
|
||||
log.Info(codes.ShutdownHttpServerInfo, "http server shutdown success")
|
||||
}
|
||||
holder.EtpServer.Close()
|
||||
|
||||
if err := holder.ClusterClient.Shutdown(); err != nil {
|
||||
log.Warnf(codes.RaftShutdownError, "raft shutdown err: %v", err)
|
||||
|
||||
+8
-5
@@ -187,14 +187,17 @@ func (cs configService) BroadcastActiveConfigs(state state.ReadonlyState, config
|
||||
go cs.broadcast(room, utils.ConfigError, []byte(err.Error()))
|
||||
continue
|
||||
}
|
||||
go cs.broadcast(room, utils.ConfigSendConfigWhenConnected, data)
|
||||
go cs.broadcast(room, utils.ConfigSendConfigChanged, data)
|
||||
}
|
||||
}
|
||||
|
||||
func (cs configService) broadcast(room, event string, data []byte) {
|
||||
err := holder.EtpServer.BroadcastToRoom(room, utils.ConfigSendConfigWhenConnected, data)
|
||||
if err != nil {
|
||||
log.Errorf(codes.ConfigServiceBroadcastConfigError, "broadcast %s err: %v", event, err)
|
||||
func (cs configService) broadcast(room, event string, body []byte) {
|
||||
conns := holder.EtpServer.Rooms().ToBroadcast(room)
|
||||
for _, conn := range conns {
|
||||
err := EmitConnWithTimeout(conn, event, body)
|
||||
if err != nil {
|
||||
log.Errorf(codes.ConfigServiceBroadcastConfigError, "broadcast '%s' to %s: %v", event, conn.RemoteAddr(), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+51
-61
@@ -1,13 +1,9 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/pkg/errors"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/cenkalti/backoff/v4"
|
||||
etp "github.com/integration-system/isp-etp-go/v2"
|
||||
"github.com/integration-system/isp-lib/v2/structure"
|
||||
"github.com/integration-system/isp-lib/v2/utils"
|
||||
@@ -20,13 +16,12 @@ import (
|
||||
|
||||
const (
|
||||
AllModules = "*"
|
||||
|
||||
messagesBackoffInterval = 100 * time.Millisecond
|
||||
messagesBackoffMaxRetries = 3
|
||||
)
|
||||
|
||||
var (
|
||||
Discovery = newDiscoveryService()
|
||||
Discovery = &discoveryService{
|
||||
subs: make(map[string][]string),
|
||||
}
|
||||
)
|
||||
|
||||
type discoveryService struct {
|
||||
@@ -64,9 +59,13 @@ func (ds *discoveryService) Subscribe(conn etp.Conn, modules []string, mesh stat
|
||||
for i := range events {
|
||||
event := events[i]
|
||||
addressList := eventsAddresses[i]
|
||||
err := ds.sendAddrList(conn, event, addressList)
|
||||
body, err := json.Marshal(addressList)
|
||||
if err != nil {
|
||||
log.Errorf(codes.DiscoveryServiceSendModulesError, "send module connected %v", err)
|
||||
panic(err)
|
||||
}
|
||||
err = EmitConnWithTimeout(conn, event, body)
|
||||
if err != nil {
|
||||
log.Errorf(codes.DiscoveryServiceSendModulesError, "send module connected to %s: %v", conn.RemoteAddr(), err)
|
||||
}
|
||||
}
|
||||
}(rooms, eventsAddresses, conn)
|
||||
@@ -78,12 +77,7 @@ func (ds *discoveryService) BroadcastModuleAddresses(moduleName string, mesh sta
|
||||
room := Room.AddressListener(moduleName)
|
||||
event := utils.ModuleConnected(moduleName)
|
||||
addressList := mesh.GetModuleAddresses(moduleName)
|
||||
go func(room, event string, addressList []structure.AddressConfiguration) {
|
||||
err := ds.broadcastAddrList(room, event, addressList)
|
||||
if err != nil {
|
||||
log.Errorf(codes.DiscoveryServiceSendModulesError, "broadcast module connected %v", err)
|
||||
}
|
||||
}(room, event, addressList)
|
||||
go ds.broadcastModuleAddrList(room, event, addressList)
|
||||
}
|
||||
|
||||
func (ds *discoveryService) BroadcastEvent(event cluster.BroadcastEvent) {
|
||||
@@ -93,53 +87,49 @@ func (ds *discoveryService) BroadcastEvent(event cluster.BroadcastEvent) {
|
||||
|
||||
if len(event.ModuleNames) == 1 && event.ModuleNames[0] == AllModules {
|
||||
go func() {
|
||||
if err := holder.EtpServer.BroadcastToAll(eventName, payload); err != nil {
|
||||
err = errors.WithMessagef(err, "broadcast '%s' to all modules", eventName)
|
||||
log.Error(codes.DiscoveryServiceSendModulesError, err)
|
||||
conns := holder.EtpServer.Rooms().AllConns()
|
||||
for _, conn := range conns {
|
||||
err := EmitConnWithTimeout(conn, eventName, payload)
|
||||
if err != nil {
|
||||
log.WithMetadata(map[string]interface{}{
|
||||
"eventName": eventName,
|
||||
"remote_address": conn.RemoteAddr(),
|
||||
}).Errorf(codes.DiscoveryServiceSendModulesError, "broadcast event to all modules: %v", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
} else {
|
||||
for _, moduleName := range event.ModuleNames {
|
||||
go func(moduleName string) {
|
||||
room := Room.Module(moduleName)
|
||||
if err := holder.EtpServer.BroadcastToRoom(room, eventName, payload); err != nil {
|
||||
err = errors.WithMessagef(err, "broadcast '%s' to '%s'", eventName, moduleName)
|
||||
log.Error(codes.DiscoveryServiceSendModulesError, err)
|
||||
}
|
||||
}(moduleName)
|
||||
return
|
||||
}
|
||||
|
||||
rooms := make([]string, 0, len(event.ModuleNames))
|
||||
for _, moduleName := range event.ModuleNames {
|
||||
room := Room.Module(moduleName)
|
||||
rooms = append(rooms, room)
|
||||
}
|
||||
go func(rooms []string) {
|
||||
conns := holder.EtpServer.Rooms().ToBroadcast(rooms...)
|
||||
for _, conn := range conns {
|
||||
err := EmitConnWithTimeout(conn, eventName, payload)
|
||||
if err != nil {
|
||||
log.WithMetadata(map[string]interface{}{
|
||||
"eventName": eventName,
|
||||
"remote_address": conn.RemoteAddr(),
|
||||
}).Errorf(codes.DiscoveryServiceSendModulesError, "broadcast event: %v", err)
|
||||
}
|
||||
}
|
||||
}(rooms)
|
||||
}
|
||||
|
||||
func (ds *discoveryService) broadcastModuleAddrList(room string, event string, addressList []structure.AddressConfiguration) {
|
||||
body, err := json.Marshal(addressList)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
conns := holder.EtpServer.Rooms().ToBroadcast(room)
|
||||
for _, conn := range conns {
|
||||
err := EmitConnWithTimeout(conn, event, body)
|
||||
if err != nil {
|
||||
log.Errorf(codes.DiscoveryServiceSendModulesError, "broadcast module connected to %s: %v", conn.RemoteAddr(), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (ds *discoveryService) broadcastAddrList(room string, event string, addressList []structure.AddressConfiguration) error {
|
||||
bytes, err := json.Marshal(addressList)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = holder.EtpServer.BroadcastToRoom(room, event, bytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ds *discoveryService) sendAddrList(conn etp.Conn, event string, addressList []structure.AddressConfiguration) error {
|
||||
bytes, err := json.Marshal(addressList)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
bf := backoff.WithMaxRetries(backoff.NewConstantBackOff(messagesBackoffInterval), messagesBackoffMaxRetries)
|
||||
err = backoff.Retry(func() error {
|
||||
return conn.Emit(context.Background(), event, bytes)
|
||||
}, bf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func newDiscoveryService() *discoveryService {
|
||||
return &discoveryService{
|
||||
subs: make(map[string][]string),
|
||||
}
|
||||
}
|
||||
+15
-1
@@ -1,12 +1,20 @@
|
||||
package service
|
||||
|
||||
import "github.com/integration-system/isp-lib/v2/utils"
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
etp "github.com/integration-system/isp-etp-go/v2"
|
||||
"github.com/integration-system/isp-lib/v2/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
configWatchersRoomSuffix = "_config"
|
||||
routesSubscribersRoom = "__routesSubscribers"
|
||||
)
|
||||
|
||||
const wsWriteTimeout = time.Second
|
||||
|
||||
var (
|
||||
Room = roomService{}
|
||||
)
|
||||
@@ -24,3 +32,9 @@ func (s roomService) RoutesSubscribers() string {
|
||||
func (s roomService) AddressListener(moduleName string) string {
|
||||
return utils.ModuleConnected(moduleName)
|
||||
}
|
||||
|
||||
func EmitConnWithTimeout(conn etp.Conn, event string, body []byte) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), wsWriteTimeout)
|
||||
defer cancel()
|
||||
return conn.Emit(ctx, event, body)
|
||||
}
|
||||
+10
-20
@@ -1,10 +1,8 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/cenkalti/backoff/v4"
|
||||
etp "github.com/integration-system/isp-etp-go/v2"
|
||||
"github.com/integration-system/isp-lib/v2/utils"
|
||||
log "github.com/integration-system/isp-log"
|
||||
@@ -24,41 +22,33 @@ func (rs *routesService) HandleDisconnect(connID string) {
|
||||
func (rs *routesService) SubscribeRoutes(conn etp.Conn, mesh state.ReadonlyMesh) {
|
||||
holder.EtpServer.Rooms().Join(conn, Room.RoutesSubscribers())
|
||||
routes := mesh.GetRoutes()
|
||||
bytes, err := json.Marshal(routes)
|
||||
body, err := json.Marshal(routes)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
go func() {
|
||||
err := rs.sendRoutes(conn, utils.ConfigSendRoutesWhenConnected, bytes)
|
||||
err := EmitConnWithTimeout(conn, utils.ConfigSendRoutesWhenConnected, body)
|
||||
if err != nil {
|
||||
log.Errorf(codes.RoutesServiceSendRoutesError, "send routes %v", err)
|
||||
log.Errorf(codes.RoutesServiceSendRoutesError, "send routes to %s: %v", conn.RemoteAddr(), err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (rs *routesService) BroadcastRoutes(mesh state.ReadonlyMesh) {
|
||||
routes := mesh.GetRoutes()
|
||||
bytes, err := json.Marshal(routes)
|
||||
body, err := json.Marshal(routes)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
go func() {
|
||||
err := holder.EtpServer.BroadcastToRoom(Room.RoutesSubscribers(), utils.ConfigSendRoutesChanged, bytes)
|
||||
if err != nil {
|
||||
log.Errorf(codes.RoutesServiceSendRoutesError, "broadcast routes %v", err)
|
||||
conns := holder.EtpServer.Rooms().ToBroadcast(Room.RoutesSubscribers())
|
||||
for _, conn := range conns {
|
||||
err := EmitConnWithTimeout(conn, utils.ConfigSendRoutesChanged, body)
|
||||
if err != nil {
|
||||
log.Errorf(codes.RoutesServiceSendRoutesError, "broadcast routes to %s: %v", conn.RemoteAddr(), err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (rs *routesService) sendRoutes(conn etp.Conn, event string, bytes []byte) error {
|
||||
bf := backoff.WithMaxRetries(backoff.NewConstantBackOff(messagesBackoffInterval), messagesBackoffMaxRetries)
|
||||
err := backoff.Retry(func() error {
|
||||
return conn.Emit(context.Background(), event, bytes)
|
||||
}, bf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
+2
-2
@@ -1,7 +1,6 @@
|
||||
package subs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
etp "github.com/integration-system/isp-etp-go/v2"
|
||||
@@ -9,10 +8,11 @@ import (
|
||||
"isp-config-service/cluster"
|
||||
"isp-config-service/codes"
|
||||
"isp-config-service/holder"
|
||||
"isp-config-service/service"
|
||||
)
|
||||
|
||||
func EmitConn(conn etp.Conn, event string, body []byte) {
|
||||
err := conn.Emit(context.Background(), event, body)
|
||||
err := service.EmitConnWithTimeout(conn, event, body)
|
||||
if err != nil {
|
||||
log.WithMetadata(map[string]interface{}{
|
||||
"event": event,
|
||||
|
||||
+4
-3
@@ -81,7 +81,7 @@ func (h *SocketEventHandler) handleConnect(conn etp.Conn) {
|
||||
|
||||
func (h *SocketEventHandler) handleDisconnect(conn etp.Conn, _ error) {
|
||||
moduleName, _ := Parameters(conn)
|
||||
log.Debugf(0, "handleDisconnect from %s", moduleName) // REMOVE
|
||||
log.Debugf(0, "websocket handleDisconnect from module '%s' addr '%s'", moduleName, conn.RemoteAddr())
|
||||
if moduleName != "" {
|
||||
holder.EtpServer.Rooms().Leave(conn, service.Room.Module(moduleName))
|
||||
now := state.GenerateDate()
|
||||
@@ -105,8 +105,9 @@ func (h *SocketEventHandler) handleDisconnect(conn etp.Conn, _ error) {
|
||||
}
|
||||
}
|
||||
|
||||
func (h *SocketEventHandler) handleError(_ etp.Conn, err error) {
|
||||
log.Warnf(codes.WebsocketError, "isp-etp: %v", err)
|
||||
func (h *SocketEventHandler) handleError(conn etp.Conn, err error) {
|
||||
moduleName, _ := Parameters(conn)
|
||||
log.Warnf(codes.WebsocketError, "websocket handleError module '%s' from %s: %v", moduleName, conn.RemoteAddr(), err)
|
||||
}
|
||||
|
||||
func NewSocketEventHandler(server etp.Server, store *store.Store) *SocketEventHandler {
|
||||
|
||||
Reference in new issue
Block a user