代码拉取完成,页面将自动刷新
package module
import (
"fmt"
"gitee.com/fpy-go/plugin/pkg/extend/model"
gplugin "github.com/hashicorp/go-plugin"
"net/rpc"
"os"
"time"
)
var parentPid int
func init() {
parentPid = os.Getppid()
}
// Notice 通知服务插件接口
type Notice interface {
Info() model.PluginInfo
Send(data []byte) model.JsonRes
}
// NoticeRPC 基于RPC实现
type NoticeRPC struct {
Client *rpc.Client
}
func (g *NoticeRPC) Info() model.PluginInfo {
var resp model.PluginInfo
err := g.Client.Call("Plugin.Info", new(interface{}), &resp)
if err != nil {
panic(err)
}
return resp
}
func (g *NoticeRPC) Send(data []byte) model.JsonRes {
var resp model.JsonRes
err := g.Client.Call("Plugin.Send", data, &resp)
if err != nil {
panic(err)
}
return resp
}
// NoticeRPCServer GreeterRPC的RPC服务器,符合 net/rpc的要求
type NoticeRPCServer struct {
Impl Notice
}
func (s *NoticeRPCServer) Info(args interface{}, resp *model.PluginInfo) error {
*resp = s.Impl.Info()
return nil
}
func (s *NoticeRPCServer) Send(data []byte, resp *model.JsonRes) error {
*resp = s.Impl.Send(data)
return nil
}
// NoticePlugin 插件的虚拟实现。用于PluginMap的插件接口。在运行时,来自插件实现的实际实现会覆盖
type NoticePlugin struct{}
// Server 此方法由插件进程延迟的调用
func (NoticePlugin) Server(*gplugin.MuxBroker) (interface{}, error) {
checkParentAlive()
return &NoticeRPCServer{}, nil
}
// Client 此方法由宿主进程调用
func (NoticePlugin) Client(b *gplugin.MuxBroker, c *rpc.Client) (interface{}, error) {
return &NoticeRPC{Client: c}, nil
}
// checkParentAlive 检查父进程(也就是 client )是否退出,如果退出了,自己也需要退出。
func checkParentAlive() {
go func() {
for {
if parentPid == 1 || os.Getppid() != parentPid {
fmt.Println("parent no alive, exit")
os.Exit(0)
}
_, err := os.FindProcess(parentPid)
if err != nil {
fmt.Println("parent no alive, exit")
os.Exit(0)
}
time.Sleep(5 * time.Second)
}
}()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。