Giter VIP home page Giter VIP logo

go-buffer-pool's Introduction

libp2p is a modular networking framework bundled together as a full stack of protocols for peer-to-peer systems. It is the product of a long and arduous quest into the internet's network stack and all of the plentiful peer-to-peer protocols of the past. Building large-scale peer-to-peer systems has been complex and challenging in the last twenty years and libp2p is a way to fix that. It is a "network stack"—a protocol suite—that cleanly separates concerns and enables sophisticated applications to use only the protocols they need without giving up interoperability and upgradeability. libp2p grew out of IPFS into an independent project designed to give any project state-of-the-art peer-to-peer capabilities.

Learn more about libp2p at libp2p.io and docs.libp2p.io.

Specifications

View the formal specifications of libp2p on the specifications repository.

Implementations

Follow the latest libp2p implementations at libp2p.io/implementations.

Dormant Implementation Projects

The following are implementation projects that have fallen dormant and we would love to help volunteers revive them. If you're interested, please reach out on any of our forums or chat rooms and we can help you start where the previous maintainers left off.

Community

Announcements

For all libp2p community announcements look for posts under the "News" topic in the community-wide forums or tagged as an announcement in the technical discussion forums and chat channels listed below.

Blog

The libp2p community blog can be read at blog.libp2p.io. If you would like to write a blog post about your cool project that uses libp2p, you can either post a message in this project's discussion forum or you may post a message in our community-wide discussion forums at discuss.libp2p.io. If you tag either @p-shahi or @dhuseby in your post we'll get back to you ASAP about your post.

Calendar

If you're a libp2p implementer, consumer, or user you are welcome to join the community meetings we host regularly. Stay up to date by following the libp2p community calendar on Lu.ma (add it to your calendar with this iCal feed).

Discussion

Please visit our community-wide discussion forums at discuss.libp2p.io to discussion community events such as hackathons and conferences and to ask questions about the past, present, and future of libp2p, and more.

If you are looking for technical help about a specific libp2p implementation, you may ask in the discussion forums on each of the implementation's Github Discusssions Forum. Here's the list of links to their technical discussion forums for each implementation listed above:

Chat

Events

Throughout the year, the libp2p community participates in a number of hackathons and we send speakers to community conferences such as EthGlobal all around the world. If you are interested in participating, keep an eye on the community calendar and announcements to learn about when and how you can get involved.

go-buffer-pool's People

Contributors

jbenet avatar kevina avatar kubuxu avatar libp2p-mgmt-read-write[bot] avatar marcopolo avatar marten-seemann avatar petroav avatar raulk avatar stebalien avatar web-flow avatar web3-bot avatar yusefnapora avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-buffer-pool's Issues

Intermittent test failure

Running package tests locally occasionally fails with

--- FAIL: TestAllocations (0.00s)
	~/go/src/github.com/libp2p/go-buffer-pool/pool_test.go:31: expected less than 100 frees after GC, got 527
FAIL
coverage: 96.4% of statements
FAIL	github.com/libp2p/go-buffer-pool	0.323s
Error: Tests failed.

@Stebalien should I investigate the failure? I see most of the code was taken from Go source with small modifications. I wanted to add Travis build and coverage files to the repo and ran into this.

Panic from go-yamux

This is what we get from time to time. Unfortunately I can not provide a way to replicate this.
Shouldn't have been something like:

if length > MaxLength {
		return make([]byte, MaxLength)
}

Thanks

panic: runtime error: makeslice: len out of range

goroutine 12857 [running]:
github.com/libp2p/go-buffer-pool.(*BufferPool).Get(0xc0000ef080, 0xff898405317b5, 0x400, 0x400, 0x2a3)
C:/Users/user/go/pkg/mod/github.com/libp2p/[email protected]/pool.go:62 +0x1a9
github.com/libp2p/go-buffer-pool.Get(...)
C:/Users/user/go/pkg/mod/github.com/libp2p/[email protected]/pool.go:95
github.com/libp2p/go-libp2p-secio.(*etmWriter).WriteMsg(0xc00d26a100, 0xc00abf8000, 0xff89840531791, 0xf8ff837e5bfd2, 0x0, 0x0)
C:/Users/user/go/pkg/mod/github.com/libp2p/[email protected]/rw.go:46 +0xd3
github.com/libp2p/go-libp2p-secio.(*etmWriter).Write(0xc00d26a100, 0xc00abf8000, 0xff89840531791, 0xf8ff837e5bfd2, 0xbf61f94125d7448b, 0x163e931c553, 0x2b66840)
C:/Users/user/go/pkg/mod/github.com/libp2p/[email protected]/rw.go:34 +0x4d
github.com/libp2p/go-yamux.(*Session).sendLoop(0xc00d19c0d0, 0x0, 0x0)
C:/Users/user/go/pkg/mod/github.com/libp2p/[email protected]/session.go:472 +0x25f
github.com/libp2p/go-yamux.(*Session).send(0xc00d19c0d0)
C:/Users/user/go/pkg/mod/github.com/libp2p/[email protected]/session.go:384 +0x2b
created by github.com/libp2p/go-yamux.newSession
C:/Users/user/go/pkg/mod/github.com/libp2p/[email protected]/session.go:130 +0x3a8

Buffer to implement io.ByteScanner interface

Hi,

Do you think it is possible to make Buffer implement io.ByteScanner interface? Specifically I mean UnreadByte() error method which is often used together with ReadByte to peek into the byte stream. Looking at the code I guess buffer shrinking may be a problem...

Too many pools

Since #1, I believe the number of pools should be 1 less. However, fixing #1 took priority so we can figure this out later.

Overflow on 32 bits platforms.

I am not sure why this wasn't a problem before but this constant:

const MaxLength = 1 << 32

should probably change to

const MaxLength = math.MaxInt32

Otherwise we will get these errors:

[go-ipfs] Running shell script
+ go build -i -ldflags=-X github.com/ipfs/go-ipfs/repo/config.CurrentCommit=pr-5553-5 -o cmd/ipfs/ipfs github.com/ipfs/go-ipfs/cmd/ipfs
# gx/ipfs/QmUQy76yspPa3fRyY3GzXFTg9n8JVwFru6ue3KFRt4MeTw/go-buffer-pool
../../../gx/ipfs/QmUQy76yspPa3fRyY3GzXFTg9n8JVwFru6ue3KFRt4MeTw/go-buffer-pool/pool.go:55:12: constant 4294967296 overflows int
../../../gx/ipfs/QmUQy76yspPa3fRyY3GzXFTg9n8JVwFru6ue3KFRt4MeTw/go-buffer-pool/pool.go:68:31: constant 4294967296 overflows int
script returned exit code 2

Advantages over sync.Pool of bytes.Buffer?

Forgive me for the unsolicited question. I saw this package being used somewhere and didn't quite understand what was going on. It seems to me that a collection of size-classed sync.Pool of *bytes.Buffer would get you all of the advantages enumerated in the README, and none of the drawbacks. But I'm sure I'm missing something! Is there a test or benchmark that shows the case(s) where this approach is better?

var bufferPool = sync.Pool{New: func() interface{} { return &bytes.Buffer{} }

func getBuffer() *bytes.Buffer {
    b := bufferPool.Get().(*bytes.Buffer)
    b.Reset() // important! :)
    return b
}

func putBuffer(b *bytes.Buffer) {
    bufferPool.Put(b)
}

BufferPool.Get doesn't detect negative length values and blindly converts them into uint32(negative) which can trigger out of memory issues

If I pass in a negative value whose uint32 overflown value fits within 31 bits, the code in (*BufferPool).Get or just .Get blindly converts it to a uint32 which wraps around to a very large positive number and causes massive memory allocations that'll cause out of memory errors to be triggered and the process will freeze and hopefully your OS will kill it but essentially it can freeze.

Repro

Here is the repro test that'll consume your RAM fast and freeze your other processes

var sink interface{}

func TestBufferPoolGetWithNegativeValue(t *testing.T) {
    for i := 0; i < 50; i++ {
        b := Get(-20008899456)
        sink = b
        Put(b)
    }
    if sink == nil { 
        t.Fatal("Test didn't run!")
    }   
    sink = (interface{})(nil)
} 

Remedy

The fix for this is rather trivial; when values are less than 0, please return nil ASAP

diff --git a/pool.go b/pool.go
index d812840..a83420e 100644
--- a/pool.go
+++ b/pool.go
@@ -55,7 +55,7 @@ type bufp struct {
 //
 // If no suitable buffer exists in the pool, Get creates one.
 func (p *BufferPool) Get(length int) []byte {
-       if length == 0 {
+       if length <= 0 {
                return nil
        }
        if length > MaxLength {

Suggestion

I kindly ask that if y'all have a bug bounty/security vulnerability reporting process that you paste something in your README.md so that I can report sensitive issues there. This work is courtesy of the Cosmos Network security team program to secure all dependencies. Thank you!

How to allow GC after use?

With this package, after a long run, the allocated buffers in the pool seems not GCed even after the Put and got no new Get

flaky unit test

On my local machine, this test fails in ~50% of the cases.

❯ go test
--- FAIL: TestPool (0.00s)
    pool_test.go:86: got a; want new slice after GC
FAIL
exit status 1
FAIL	github.com/libp2p/go-buffer-pool	0.271s

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.