-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathplugin.go
More file actions
150 lines (117 loc) · 3.66 KB
/
Copy pathplugin.go
File metadata and controls
150 lines (117 loc) · 3.66 KB
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
142
143
144
145
146
147
148
149
150
/*
Copyright 2021 The Katanomi Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package plugin for building and running plugin
package plugin
import (
"context"
"fmt"
"log"
"net/http"
"os/signal"
"syscall"
"time"
"github.com/katanomi/pkg/plugin/client"
"github.com/katanomi/pkg/plugin/component/tracing"
"github.com/katanomi/pkg/plugin/config"
"github.com/emicklei/go-restful/v3"
"github.com/katanomi/pkg/plugin/route"
)
// Plugin
type plugin struct {
config *config.Config
clients []client.Interface
shutdownFuncs []ShutdownFunc
container *restful.Container
}
type ShutdownFunc func() error
// NewPlugin method never used. was supposed to initiate plugins
// Deprecated: This plugin initializer has never been used and is not recommended
// TODO: cleanup unnecessary code
func NewPlugin() *plugin {
plugin := &plugin{
container: restful.NewContainer(),
}
return plugin
}
func (p *plugin) WithConfig(c *config.Config) {
p.config = c
}
func (p *plugin) WithClient(clients ...client.Interface) *plugin {
p.clients = append(p.clients, clients...)
return p
}
// prepare prepare plugin component, include config, route, tracing
func (p *plugin) prepare() {
if p.config == nil {
p.config = config.NewConfig()
}
if p.config.Trace.Enable {
closer, err := tracing.Config(&p.config.Trace)
if err != nil {
panic(fmt.Sprintf("add srevice error: %s", err.Error()))
}
if closer != nil {
p.shutdownFuncs = append(p.shutdownFuncs, closer.Close)
}
}
// this plugin initialization process was never really used in production
// and is not recommended, so here we just give any context to the constructor
p.container.Add(route.NewDefaultService(context.Background()))
for _, each := range p.clients {
ws, err := route.NewService(each)
if err != nil {
panic(fmt.Sprintf("add srevice error: %s", err.Error()))
}
p.container.Add(ws)
}
p.container.Add(route.NewDocService(p.container.RegisteredWebServices()...))
}
// Run plugin run and shutdown gracefully
func (p *plugin) Run() error {
//todo watch configmap and reload config
p.prepare()
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
port := p.config.Server.Port
srv := &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: p.container,
}
// Initializing the server in a goroutine so that
// it won't block the graceful shutdown handling below
go func() {
log.Printf("plugin server run, port:%d\n", port)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
// Listen for the interrupt signal.
<-ctx.Done()
// Restore default behavior on the interrupt signal and notify user of shutdown.
stop()
log.Println("shutting down gracefully.")
for _, f := range p.shutdownFuncs {
if err := f(); err != nil {
log.Fatal("prepare shutdown error: ", err)
}
}
// The context is used to inform the server it has 5 seconds to finish
// the request it is currently handling
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err := srv.Shutdown(ctx)
if err != nil {
log.Fatal("server forced to shutdown: ", err)
}
return err
}