Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
injoyai committed Nov 14, 2023
1 parent 0d34216 commit ea46fa3
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions dial/dial_serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package dial

import (
"github.com/goburrow/serial"
"github.com/injoyai/base/oss"
"github.com/injoyai/io"
"github.com/injoyai/logs"
serial2 "go.bug.st/serial"
"os"
"os/signal"
"syscall"
"time"
)

Expand Down Expand Up @@ -54,13 +56,25 @@ func WithSerial(cfg *SerialConfig) io.DialFunc {
func NewSerial(cfg *SerialConfig, options ...io.OptionClient) (*io.Client, error) {
return io.NewDial(WithSerial(cfg), func(c *io.Client) {
c.SetOptions(options...)
oss.ListenExit(func() { c.CloseAll() })
exitChan := make(chan os.Signal)
signal.Notify(exitChan, os.Interrupt, os.Kill, syscall.SIGTERM)
go func(c *io.Client) {
<-exitChan
c.CloseAll()
os.Exit(-127)
}(c)
})
}

func RedialSerial(cfg *SerialConfig, options ...io.OptionClient) *io.Client {
c := io.Redial(WithSerial(cfg), options...)
oss.ListenExit(func() { c.CloseAll() })
exitChan := make(chan os.Signal)
signal.Notify(exitChan, os.Interrupt, os.Kill, syscall.SIGTERM)
go func(c *io.Client) {
<-exitChan
c.CloseAll()
os.Exit(-127)
}(c)
return c
}

Expand Down

0 comments on commit ea46fa3

Please sign in to comment.