Ejemplo n.º 1
0
// ConfigPIE provides the Configurator for the PIE class of plugin.
func ConfigPIE(lib *glick.Library) error {
	if lib == nil {
		return glick.ErrNilLib
	}
	return lib.AddConfigurator("PIE", func(l *glick.Library, line int, cfg *glick.Config) error {
		ppo, err := l.ProtoPlugOut(cfg.API)
		if err != nil {
			return fmt.Errorf("entry %d PIE register plugin error: %v",
				line, err) // no simple test possible for this path
		}
		pi := PluginPie(!cfg.Gob, cfg.Method, cfg.Cmd, ppo)
		for _, action := range cfg.Actions {
			if err := l.RegPlugin(cfg.API, action, pi, cfg); err != nil {
				return fmt.Errorf("entry %d PIE register plugin error: %v",
					line, err)
			}
		}
		return nil
	})
}
Ejemplo n.º 2
0
func ConfigGRPChw(lib *glick.Library) error {
	return lib.AddConfigurator("gRPChw", func(l *glick.Library, line int, cfg *glick.Config) error {
		for _, action := range cfg.Actions {
			if err := l.RegPlugin(cfg.API, action,
				func(ctx context.Context, in interface{}) (out interface{}, err error) {
					ins, ok := in.(*pb.HelloRequest)
					if !ok {
						return nil, errors.New("not *pb.HelloRequest")
					}
					out = interface{}(&pb.HelloReply{})
					outsp := out.(*pb.HelloReply)
					dialOpt := []grpc.DialOption{grpc.WithInsecure()}
					if deadline, ok := ctx.Deadline(); ok {
						dialOpt = append(dialOpt,
							grpc.WithTimeout(deadline.Sub(time.Now())))
					}
					conn, err := grpc.Dial(address, dialOpt...)
					if err != nil {
						return nil, err
					}
					defer func() {
						if e := conn.Close(); e != nil {
							panic(e)
						}
					}()
					c := pb.NewGreeterClient(conn)

					r, err := c.SayHello(context.Background(), ins)
					if err != nil {
						return nil, err
					}
					*outsp = *r
					return out, nil
				}, cfg); err != nil {
				return fmt.Errorf("entry %d GRPChw register plugin error: %v",
					line, err)
			}
		}
		return nil
	})
}