-
Notifications
You must be signed in to change notification settings - Fork 9
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
Showing
10 changed files
with
126 additions
and
94 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
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
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,51 @@ | ||
package service | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/metal-stack/metal-api/cmd/metal-api/internal/datastore" | ||
) | ||
|
||
const ( | ||
// ASNMin is the minimum asn defined according to | ||
// https://en.wikipedia.org/wiki/Autonomous_system_(Internet) | ||
ASNMin = uint32(4200000000) | ||
|
||
// ASNBase is the offset for all Machine ASN´s | ||
ASNBase = uint32(4210000000) | ||
|
||
// ASNMax defines the maximum allowed asn | ||
// https://en.wikipedia.org/wiki/Autonomous_system_(Internet) | ||
ASNMax = uint32(4294967294) | ||
) | ||
|
||
// acquireASN fetches a unique integer by using the existing integer pool and adding to ASNBase | ||
func acquireASN(ds *datastore.RethinkStore) (*uint32, error) { | ||
asnPool, err := ds.GetIntegerPool(datastore.ASNIntegerPool) | ||
if err != nil { | ||
return nil, err | ||
} | ||
i, err := asnPool.AcquireRandomUniqueInteger() | ||
if err != nil { | ||
return nil, err | ||
} | ||
asn := ASNBase + uint32(i) | ||
if asn > ASNMax { | ||
return nil, fmt.Errorf("unable to calculate asn, got a asn larger than ASNMax: %d > %d", asn, ASNMax) | ||
} | ||
return &asn, nil | ||
} | ||
|
||
// releaseASN will release the asn from the integerpool | ||
func releaseASN(ds *datastore.RethinkStore, asn uint32) error { | ||
if asn < ASNBase || asn > ASNMax { | ||
return fmt.Errorf("asn %d might not be smaller than:%d or larger than %d", asn, ASNBase, ASNMax) | ||
} | ||
i := uint(asn - ASNBase) | ||
|
||
asnPool, err := ds.GetIntegerPool(datastore.ASNIntegerPool) | ||
if err != nil { | ||
return err | ||
} | ||
return asnPool.ReleaseUniqueInteger(i) | ||
} |
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
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
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,36 @@ | ||
package service | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/metal-stack/metal-api/cmd/metal-api/internal/datastore" | ||
) | ||
|
||
// acquireRandomVRF will grab a unique but random vrf out of the vrfintegerpool | ||
func acquireRandomVRF(ds *datastore.RethinkStore) (*uint, error) { | ||
vrfPool, err := ds.GetIntegerPool(datastore.VRFIntegerPool) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not acquire random vrf: %v", err) | ||
} | ||
vrf, err := vrfPool.AcquireRandomUniqueInteger() | ||
return &vrf, err | ||
} | ||
|
||
// acquireVRF will the given vrf out of the vrfintegerpool if not available a error is thrown | ||
func acquireVRF(ds *datastore.RethinkStore, vrf uint) error { | ||
vrfPool, err := ds.GetIntegerPool(datastore.VRFIntegerPool) | ||
if err != nil { | ||
return fmt.Errorf("could not acquire vrf:%d %v", vrf, err) | ||
} | ||
_, err = vrfPool.AcquireUniqueInteger(vrf) | ||
return err | ||
} | ||
|
||
// releaseVRF will return the given vrf to the vrfintegerpool for reuse | ||
func releaseVRF(ds *datastore.RethinkStore, vrf uint) error { | ||
vrfPool, err := ds.GetIntegerPool(datastore.VRFIntegerPool) | ||
if err != nil { | ||
return fmt.Errorf("could not release vrf: %v", err) | ||
} | ||
return vrfPool.ReleaseUniqueInteger(vrf) | ||
} |