-
Notifications
You must be signed in to change notification settings - Fork 335
/
run.go
141 lines (128 loc) · 3.62 KB
/
run.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright (C) 2020-2021, IrineSistiana
//
// This file is part of mosdns.
//
// mosdns is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// mosdns is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package coremain
import (
"fmt"
"github.com/IrineSistiana/mosdns/dispatcher/handler"
"github.com/IrineSistiana/mosdns/dispatcher/mlog"
"github.com/IrineSistiana/mosdns/dispatcher/pkg/concurrent_limiter"
_ "github.com/IrineSistiana/mosdns/dispatcher/plugin"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
"os/signal"
"plugin"
"runtime"
"sync"
"syscall"
)
// Run starts mosdns, it blocks.
func Run(c string) {
loadConfig(c, 0)
mlog.L().Info("all plugins are successfully loaded")
//wait for signals
osSignals := make(chan os.Signal, 1)
signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM)
s := <-osSignals
mlog.L().Info("exiting, bye", zap.Stringer("signal", s))
os.Exit(0)
}
const (
maxIncludeDepth = 10
)
// Init loads plugins from config
func loadConfig(f string, depth int) {
if depth >= maxIncludeDepth {
mlog.S().Fatal("max include depth reached")
}
depth++
mlog.L().Info("loading config", zap.String("file", f))
c, err := parseConfig(f)
if err != nil {
mlog.S().Fatalf("failed to parse config from file %s: %v", f, err)
}
if depth == 1 {
// init logger
level, err := parseLogLevel(c.Log.Level)
if err != nil {
mlog.S().Fatal(err)
}
mlog.Level().SetLevel(level)
if len(c.Log.File) != 0 {
mlog.L().Info("opening log file", zap.String("file", c.Log.File))
f, err := os.OpenFile(c.Log.File, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0755)
if err != nil {
mlog.S().Fatalf("can not open log file %s: %v", c.Log.File, err)
}
mlog.L().Info("redirecting log to file, end of console log", zap.String("file", c.Log.File))
mlog.Writer().Replace(f)
}
for _, lib := range c.Library {
mlog.L().Info("loading library", zap.String("library", lib))
_, err := plugin.Open(lib)
if err != nil {
mlog.S().Fatalf("failed to open library %s: %v", lib, err)
}
}
}
n := runtime.NumCPU() / 2
if n < 1 {
n = 1
}
pool := concurrent_limiter.NewConcurrentLimiter(n)
wg := new(sync.WaitGroup)
for i, pluginConfig := range c.Plugin {
if len(pluginConfig.Tag) == 0 || len(pluginConfig.Type) == 0 {
continue
}
i := i
pluginConfig := pluginConfig
select {
case <-pool.Wait():
wg.Add(1)
go func() {
defer pool.Done()
defer wg.Done()
mlog.L().Info("loading plugin", zap.String("tag", pluginConfig.Tag))
if err := handler.InitAndRegPlugin(pluginConfig, true); err != nil {
mlog.S().Fatalf("failed to register plugin #%d %s: %v", i, pluginConfig.Tag, err)
}
}()
}
}
wg.Wait()
for _, include := range c.Include {
if len(include) == 0 {
continue
}
loadConfig(include, depth)
}
}
func parseLogLevel(s string) (zapcore.Level, error) {
switch s {
case "debug":
return zap.DebugLevel, nil
case "", "info":
return zap.InfoLevel, nil
case "warn":
return zap.WarnLevel, nil
case "error":
return zap.ErrorLevel, nil
default:
return 0, fmt.Errorf("invalid log level [%s]", s)
}
}