(ctx context.Context)
| 15 | ) |
| 16 | |
| 17 | func Run(ctx context.Context) (err error) { |
| 18 | ms := newMysqlProxy() |
| 19 | ms.ctx = ctx |
| 20 | ms.closed.Store(true) |
| 21 | |
| 22 | if err = ms.initClientPool(); err != nil { |
| 23 | return fmt.Errorf("initClientPool error: %v", err) |
| 24 | } |
| 25 | ln, err := net.Listen("tcp4", config.Get().Server.Addr) |
| 26 | if err != nil { |
| 27 | logrus.Errorf("mysql%v", err) |
| 28 | return |
| 29 | } |
| 30 | utils.GoWithRecover(func() { |
| 31 | if <-ctx.Done(); true { |
| 32 | _ = ln.Close() |
| 33 | ms.closed.Store(true) |
| 34 | } |
| 35 | }, nil) |
| 36 | |
| 37 | ms.closed.Store(false) |
| 38 | logrus.Infof("%s\n", config.Get().Server.Addr) |
| 39 | // p2p |
| 40 | if config.Get().P2P.Enable { |
| 41 | initP2P(ms) |
| 42 | } |
| 43 | for { |
| 44 | conn, err := ln.Accept() |
| 45 | if err != nil { |
| 46 | if errors.Is(err, io.EOF) || errors.Is(err, net.ErrClosed) { |
| 47 | continue |
| 48 | } |
| 49 | panic(err) |
| 50 | } |
| 51 | go ms.onConn(conn) |
| 52 | } |
| 53 | return |
| 54 | } |
| 55 | |
| 56 | func newMysqlProxy() *mysqlProxy { |
| 57 | p := &mysqlProxy{} |
no test coverage detected