Beispiel #1
0
func (this *ReqJesgooModule) Init(global_conf *context.GlobalContext) (err error) {

	err = this.redis.Init(global_conf)
	if err != nil {
		utils.FatalLog.Write("init redis fail, err[%s]", err.Error())
		return
	}

	this.host = global_conf.JesgooBs.Host
	this.port = global_conf.JesgooBs.Port
	this.timeout = global_conf.JesgooBs.Timeout

	this.pool = &connpool.ConnPool{
		Dial: func() (interface{}, error) {
			transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
			protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
			transport, _ := thrift.NewTSocket(net.JoinHostPort(this.host, this.port))
			usetransport := transportFactory.GetTransport(transport)
			client := ui2bs.NewBSServiceClientFactory(usetransport, protocolFactory)
			err := client.Transport.Open()
			if err != nil {
				utils.FatalLog.Write("new jesgoo bs client fail , [%s]", err.Error())
				return nil, err
			}
			return client, err
		},
		Close: func(c interface{}) error {
			err := c.(*ui2bs.BSServiceClient).Transport.Close()
			return err
		},
		Alive: func(c interface{}) bool {
			_, err := c.(*ui2bs.BSServiceClient).Ping()
			if err != nil {
				utils.DebugLog.Write("transport is closed")
				return false
			}
			return true
		},
		MaxIdle: 1024,
	}
	return
}
func main() {
	flag.Usage = Usage
	var host string
	var port int
	var protocol string
	var urlString string
	var framed bool
	var useHttp bool
	var parsedUrl url.URL
	var trans thrift.TTransport
	_ = math.MinInt32 // will become unneeded eventually
	_ = strconv.Atoi
	flag.Usage = Usage
	flag.StringVar(&host, "h", "localhost", "Specify host and port")
	flag.IntVar(&port, "p", 9090, "Specify port")
	flag.StringVar(&protocol, "P", "binary", "Specify the protocol (binary, compact, simplejson, json)")
	flag.StringVar(&urlString, "u", "", "Specify the url")
	flag.BoolVar(&framed, "framed", false, "Use framed transport")
	flag.BoolVar(&useHttp, "http", false, "Use http")
	flag.Parse()

	if len(urlString) > 0 {
		parsedUrl, err := url.Parse(urlString)
		if err != nil {
			fmt.Fprintln(os.Stderr, "Error parsing URL: ", err)
			flag.Usage()
		}
		host = parsedUrl.Host
		useHttp = len(parsedUrl.Scheme) <= 0 || parsedUrl.Scheme == "http"
	} else if useHttp {
		_, err := url.Parse(fmt.Sprint("http://", host, ":", port))
		if err != nil {
			fmt.Fprintln(os.Stderr, "Error parsing URL: ", err)
			flag.Usage()
		}
	}

	cmd := flag.Arg(0)
	var err error
	if useHttp {
		trans, err = thrift.NewTHttpClient(parsedUrl.String())
	} else {
		portStr := fmt.Sprint(port)
		if strings.Contains(host, ":") {
			host, portStr, err = net.SplitHostPort(host)
			if err != nil {
				fmt.Fprintln(os.Stderr, "error with host:", err)
				os.Exit(1)
			}
		}
		trans, err = thrift.NewTSocket(net.JoinHostPort(host, portStr))
		if err != nil {
			fmt.Fprintln(os.Stderr, "error resolving address:", err)
			os.Exit(1)
		}
		if framed {
			trans = thrift.NewTFramedTransport(trans)
		}
	}
	if err != nil {
		fmt.Fprintln(os.Stderr, "Error creating transport", err)
		os.Exit(1)
	}
	defer trans.Close()
	var protocolFactory thrift.TProtocolFactory
	switch protocol {
	case "compact":
		protocolFactory = thrift.NewTCompactProtocolFactory()
		break
	case "simplejson":
		protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
		break
	case "json":
		protocolFactory = thrift.NewTJSONProtocolFactory()
		break
	case "binary", "":
		protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
		break
	default:
		fmt.Fprintln(os.Stderr, "Invalid protocol specified: ", protocol)
		Usage()
		os.Exit(1)
	}
	client := ui2bs.NewBSServiceClientFactory(trans, protocolFactory)
	if err := trans.Open(); err != nil {
		fmt.Fprintln(os.Stderr, "Error opening socket to ", host, ":", port, " ", err)
		os.Exit(1)
	}

	switch cmd {
	case "search":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "Search requires 1 args")
			flag.Usage()
		}
		arg14 := flag.Arg(1)
		mbTrans15 := thrift.NewTMemoryBufferLen(len(arg14))
		defer mbTrans15.Close()
		_, err16 := mbTrans15.WriteString(arg14)
		if err16 != nil {
			Usage()
			return
		}
		factory17 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt18 := factory17.GetProtocol(mbTrans15)
		argvalue0 := ui2bs.NewBSRequest()
		err19 := argvalue0.Read(jsProt18)
		if err19 != nil {
			Usage()
			return
		}
		value0 := argvalue0
		fmt.Print(client.Search(value0))
		fmt.Print("\n")
		break
	case "ping":
		if flag.NArg()-1 != 0 {
			fmt.Fprintln(os.Stderr, "Ping requires 0 args")
			flag.Usage()
		}
		fmt.Print(client.Ping())
		fmt.Print("\n")
		break
	case "":
		Usage()
		break
	default:
		fmt.Fprintln(os.Stderr, "Invalid function ", cmd)
	}
}