-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0daa5a7
commit 0c9ddba
Showing
4 changed files
with
48 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package libp2p | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/libp2p/go-libp2p" | ||
rcmgr "github.com/libp2p/go-libp2p-resource-manager" | ||
) | ||
|
||
func ResourceManager() func() (Libp2pOpts, error) { | ||
return func() (opts Libp2pOpts, err error) { | ||
var limiter *rcmgr.BasicLimiter | ||
|
||
limitsIn, err := os.Open("./limits.json") | ||
switch { | ||
case err == nil: | ||
defer limitsIn.Close() | ||
limiter, err = rcmgr.NewDefaultLimiterFromJSON(limitsIn) | ||
if err != nil { | ||
return opts, fmt.Errorf("error parsing limit file: %w", err) | ||
} | ||
case errors.Is(err, os.ErrNotExist): | ||
limiter = rcmgr.NewDefaultLimiter() | ||
default: | ||
return opts, err | ||
} | ||
|
||
libp2p.SetDefaultServiceLimits(limiter) | ||
|
||
// TODO: close the resource manager when the node is shut down | ||
rcmgr, err := rcmgr.NewResourceManager(limiter) | ||
if err != nil { | ||
return opts, fmt.Errorf("error creating resource manager: %w", err) | ||
} | ||
opts.Opts = append(opts.Opts, libp2p.ResourceManager(rcmgr)) | ||
return opts, nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters