Ejemplo n.º 1
0
// NewClient creates a new client
func NewClient() *Client {
	client := new(Client)
	client.bots = make(map[string]*IrcBot)
	client.downloads = make(map[string]*models.Download)
	client.updateChan = make(chan models.DccUpdate)
	client.connPool = iothrottler.NewIOThrottlerPool(iothrottler.Unlimited)

	//start download updates
	go client.updateDownloads()

	return client
}
Ejemplo n.º 2
0
//CopyThrottled does a normal io.Copy but with throttling
func CopyThrottled(bandwidth iothrottler.Bandwidth, dest io.Writer, src io.Reader) (written int64, returnErr error) {
	pool := iothrottler.NewIOThrottlerPool(bandwidth)
	defer pool.ReleasePool()

	var readCloser io.ReadCloser
	if rc, ok := src.(io.ReadCloser); ok {
		readCloser = rc
	} else {
		readCloser = ioutil.NopCloser(src)
	}

	throttledFile, err := pool.AddReader(readCloser)
	if err != nil {
		return 0, fmt.Errorf("Cannot add reader to copy throttler, error: %s", err.Error())
	}

	return io.Copy(dest, throttledFile)
}
func NewThrottledResponseWriter(w http.ResponseWriter, bw iothrottler.Bandwidth) ThrottledResponseWriter {
	p := iothrottler.NewIOThrottlerPool(bw)
	tw, _ := p.AddWriter(NopWriteCloser{w})
	pusher, _ := w.(http2.Pusher)
	return ThrottledResponseWriter{pusher, tw, w, p}
}