当前位置: 首页>>代码示例>>Golang>>正文


Golang errors.NewErr函数代码示例

本文整理汇总了Golang中psearch/util/errors.NewErr函数的典型用法代码示例。如果您正苦于以下问题:Golang NewErr函数的具体用法?Golang NewErr怎么用?Golang NewErr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了NewErr函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Write

func (self *Gatekeeper) Write(url, key string, data []byte) (Value, error) {
	log.Printf("Gatekeeper.Write(%v, %v)\n", url, key)

	if self.file.file == nil {
		f, err := os.Create(self.dir + "/" + strconv.Itoa(int(self.fNum)))
		if err != nil {
			return Value{}, errors.NewErr(err)
		}

		self.file = gkFile{
			file:   f,
			offset: 0,
			end:    time.Now().Add(self.maxTime),
		}
	} else if self.file.offset >= self.maxFileSize {
		if err := self.nextFile(); err != nil {
			return Value{}, err
		}
	} else if self.file.end.Before(time.Now()) {
		if err := self.file.file.Sync(); err != nil {
			return Value{}, errors.NewErr(err)
		}
		self.file.end = time.Now().Add(self.maxTime)
	}

	offset := self.file.offset

	cnt := 0
	n, err := self.file.WriteLenval([]byte(url))
	if err != nil {
		return Value{}, err
	}
	cnt += n

	n, err = self.file.WriteLenval(data)
	if err != nil {
		return Value{}, err
	}
	cnt += n

	self.file.offset += uint64(cnt)

	res := Value{
		FNum:   self.fNum,
		Offset: offset,
		Len:    uint64(cnt),
	}

	self.trie.Add([]byte(key), res)

	// TODO: remove
	self.file.file.Sync()

	log.Printf("Gatekeeper.Write(%v, %v) OK (%+v)\n", url, key, res)
	return res, nil
}
开发者ID:Monnoroch,项目名称:psearch,代码行数:56,代码来源:gatekeeper.go

示例2: WriteLenval

func (self *gkFile) WriteLenval(b []byte) (n int, err error) {
	buf := make([]byte, 8)
	num := binary.PutUvarint(buf, uint64(len(b)))
	n1, err := self.file.Write(buf[:num])
	if err != nil {
		return n1, errors.NewErr(err)
	}

	n2, err := self.file.Write(b)
	return n1 + n2, errors.NewErr(err)
}
开发者ID:Monnoroch,项目名称:psearch,代码行数:11,代码来源:gatekeeper.go

示例3: DoIterTsvRequest

func DoIterTsvRequest(url string, it iter.Iterator) (*http.Response, error) {
	req, err := http.NewRequest("GET", url, iter.ReadDelim(it, []byte("\t")))
	if err != nil {
		return nil, errors.NewErr(err)
	}
	req.ContentLength = -1

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return nil, errors.NewErr(err)
	}

	return resp, nil
}
开发者ID:Monnoroch,项目名称:psearch,代码行数:14,代码来源:utils.go

示例4: Request

func (self *Balanser) Request(w http.ResponseWriter, r *http.Request) ([]error, error) {
	if r.Method != "GET" {
		return nil, util.ClientError(errors.New("Can only process GET requests!"))
	}

	url := *r.URL
	url.Scheme = "http"

	var resErrors []error = nil
	failed := map[string]struct{}{}
	for {
		if len(failed) == self.count {
			return resErrors, errors.New("All backends are broken!")
		}

		backend, err := self.router.Choose()
		if err != nil {
			return resErrors, err
		}

		if _, ok := failed[backend]; ok {
			continue
		}

		url.Host = backend
		nreq, err := http.NewRequest("GET", url.String(), r.Body)
		if err != nil {
			return resErrors, errors.NewErr(err)
		}

		nreq.Header = r.Header
		resp, err := (&http.Client{}).Do(nreq)
		if err != nil {
			failed[backend] = struct{}{}
			resErrors = append(resErrors, errors.NewErr(err))
			continue
		}
		defer resp.Body.Close()

		for k, hs := range resp.Header {
			for _, val := range hs {
				w.Header().Add(k, val)
			}
		}
		w.WriteHeader(resp.StatusCode)
		_, err = io.Copy(w, resp.Body)
		return resErrors, errors.NewErr(err)
	}
}
开发者ID:Monnoroch,项目名称:psearch,代码行数:49,代码来源:balanser.go

示例5: SendJson

func SendJson(w http.ResponseWriter, data interface{}) error {
	w.Header().Set("Content-type", "application/json")
	if err := json.NewEncoder(w).Encode(data); err != nil {
		return errors.NewErr(err)
	}
	return nil
}
开发者ID:Monnoroch,项目名称:psearch,代码行数:7,代码来源:utils.go

示例6: Download

func (self *Downloader) Download(url string) (string, error) {
	log.Printf("Downloader.Download(%s)\n", url)
	resp, err := http.Get(url)
	if err != nil {
		return "", errors.NewErr(err)
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", errors.NewErr(err)
	}

	log.Printf("Downloader.Download(%s) OK!\n", url)
	return string(body), nil
}
开发者ID:Monnoroch,项目名称:psearch,代码行数:16,代码来源:downloader.go

示例7: Write

func (self *FileReader) Write(b []byte) (n int, err error) {
	n, err = self.file.Write(b)
	if err == io.EOF {
		return n, err
	}
	return n, errors.NewErr(err)
}
开发者ID:Monnoroch,项目名称:psearch,代码行数:7,代码来源:file_reader.go

示例8: PushUrls

func (self *Caregiver) PushUrls(urls []string) error {
	log.Printf("Caregiver.PushUrls(%#v)\n", urls)
	data := map[string][]string{}
	for _, u := range urls {
		url, err := url.Parse(u)
		if err != nil {
			return errors.NewErr(err)
		}

		path := url.Path
		if len(path) != 0 && path[0] == '/' {
			path = path[1:]
		}
		if url.RawQuery != "" {
			path += "?"
			path += url.RawQuery
		}
		data[url.Host] = append(data[url.Host], path)
	}

	for host, urls := range data {
		self.mutex.Lock()
		hosts, ok := self.hosts[host]
		if !ok {
			hosts = self.getData(host)
			self.hosts[host] = hosts
		}
		self.mutex.Unlock()
		hosts.urls.EnqueueAll(urls...)
	}
	log.Printf("Caregiver.PushUrls(%#v) OK\n", urls)
	return nil
}
开发者ID:Monnoroch,项目名称:psearch,代码行数:33,代码来源:caregiver.go

示例9: main

func main() {
	var help = flag.Bool("help", false, "print help")
	var port = flag.Int("port", -1, "port to listen")
	var cacheTime = flag.Int("cachetime", -1, "time to keep resolved records in cache (in seconds)")
	var gracefulRestart = graceful.SetFlag()
	flag.Parse()

	if *help || *port == -1 || *cacheTime == -1 {
		flag.PrintDefaults()
		return
	}

	srv := rpc.NewServer()
	srv.Register(&dns.ResolverServer{dns.NewResolver(time.Duration(*cacheTime) * time.Second)})
	log.Println(srv)

	server := gjsonrpc.NewServer(srv)
	graceful.SetSighup(server)

	if err := server.ListenAndServe(":"+strconv.Itoa(*port), *gracefulRestart); err != nil {
		log.Fatal(errors.NewErr(err))
	}

	if err := graceful.Restart(server); err != nil {
		log.Fatal(err)
	}
}
开发者ID:Monnoroch,项目名称:psearch,代码行数:27,代码来源:main.go

示例10: NewGatekeeper

func NewGatekeeper(dir string, maxFileSize uint64, maxTime time.Duration) (*Gatekeeper, error) {
	self := &Gatekeeper{
		dir:         dir,
		maxTime:     maxTime,
		maxFileSize: maxFileSize,
		fNum:        0,
	}

	files, err := ioutil.ReadDir(dir)
	if err != nil {
		return nil, errors.NewErr(err)
	}

	arr := make(valTArr, len(files))
	for i, f := range files {
		num, err := strconv.Atoi(f.Name())
		if err != nil {
			return nil, errors.NewErr(err)
		}

		arr[i] = valT{
			num:  uint(num),
			file: f,
		}
	}

	sort.Sort(arr)
	counts := map[uint]uint{}
	for _, f := range arr {
		counts[f.num] = 0
		self.fNum = f.num + 1
		err := self.load(self.dir+"/"+f.file.Name(), f.num, counts)
		if err != nil {
			return nil, err
		}
	}

	for k, v := range counts {
		if v == 0 {
			if err := os.Remove(self.dir + "/" + strconv.Itoa(int(k))); err != nil {
				return nil, errors.NewErr(err)
			}
		}
	}

	return self, nil
}
开发者ID:Monnoroch,项目名称:psearch,代码行数:47,代码来源:gatekeeper.go

示例11: File

func (self *GracefulListener) File() (*os.File, error) {
	f, err := self.base.File()
	if err != nil {
		return nil, errors.NewErr(err)
	}

	return f, nil
}
开发者ID:Monnoroch,项目名称:psearch,代码行数:8,代码来源:graceful.go

示例12: JsonRpcDial

func JsonRpcDial(addr string) (*rpc.Client, error) {
	conn, err := net.Dial("tcp", addr)
	if err != nil {
		return nil, errors.NewErr(err)
	}

	return jsonrpc.NewClient(conn), nil
}
开发者ID:Monnoroch,项目名称:psearch,代码行数:8,代码来源:utils.go

示例13: Seek

func (self *FileReader) Seek(offset int64, whence int) (ret int64, err error) {
	ret, err = self.file.Seek(offset, whence)
	if err == io.EOF {
		return ret, err
	}

	return ret, errors.NewErr(err)
}
开发者ID:Monnoroch,项目名称:psearch,代码行数:8,代码来源:file_reader.go

示例14: GetListener

func GetListener(addr string, graceful bool) (*net.TCPListener, error) {
	var listener net.Listener = nil
	var err error = nil
	if graceful {
		file := os.NewFile(3, "")
		listener, err = net.FileListener(file)
		if err != nil {
			file.Close()
			return nil, errors.NewErr(err)
		}
	} else {
		listener, err = net.Listen("tcp", addr)
		if err != nil {
			return nil, errors.NewErr(err)
		}
	}
	return listener.(*net.TCPListener), nil
}
开发者ID:Monnoroch,项目名称:psearch,代码行数:18,代码来源:graceful.go

示例15: Serve

func (self *Server) Serve(l *net.TCPListener) error {
	self.listener = graceful.NewGracefulListener(l, self)
	err := self.base.Serve(self.listener)
	self.restart.Wait()
	if err == graceful.NeedRestart {
		return nil
	}
	return errors.NewErr(err)
}
开发者ID:Monnoroch,项目名称:psearch,代码行数:9,代码来源:server.go


注:本文中的psearch/util/errors.NewErr函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。