github.com/kayoticsully/syncthing@v0.8.9-0.20140724133906-c45a2fdc03f8/cmd/syncthing/limitedwriter.go (about)

     1  // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
     2  // All rights reserved. Use of this source code is governed by an MIT-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"io"
     9  
    10  	"github.com/juju/ratelimit"
    11  )
    12  
    13  type limitedWriter struct {
    14  	w      io.Writer
    15  	bucket *ratelimit.Bucket
    16  }
    17  
    18  func (w *limitedWriter) Write(buf []byte) (int, error) {
    19  	if w.bucket != nil {
    20  		w.bucket.Wait(int64(len(buf)))
    21  	}
    22  	return w.w.Write(buf)
    23  }