-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from go-faster/feat/faker
feat(faker): allocate ip pools in model
- Loading branch information
Showing
7 changed files
with
211 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# faker | ||
|
||
Faker models a cluster of nodes, where services are deployed, exposing an API that | ||
is used by clients to interact with. | ||
|
||
## Overview | ||
|
||
```mermaid | ||
graph LR | ||
F[Frontend]-->A[API] | ||
A-->B[Backend] | ||
B-->C[Cache] | ||
B-->D[DB] | ||
``` | ||
|
||
```mermaid | ||
sequenceDiagram | ||
Frontend->>API: HTTP GET / | ||
API->>Backend: gRPC Get() | ||
Backend->>Cache: INCR counter | ||
Cache-->>Backend: counter | ||
Backend->>DB: SELECT * FROM table | ||
DB-->>Backend: posts (psql) | ||
Backend-->>API: posts (pb) | ||
API-->>Frontend: posts (json) | ||
``` | ||
|
||
## Services | ||
|
||
### Frontend | ||
|
||
Represents a web browser. | ||
|
||
#### Static | ||
- IP address | ||
- User agent | ||
- Target web domain | ||
|
||
#### Dynamic | ||
- External port | ||
|
||
### API | ||
|
||
Represents a web server that exposes an HTTP API. | ||
Accepts an HTTP request, forwards it to backend via gRPC and | ||
returns the response in json format. | ||
|
||
#### Static | ||
- IP address | ||
- Server | ||
|
||
#### Dynamic | ||
- External port |
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,30 @@ | ||
package faker | ||
|
||
import ( | ||
"encoding/binary" | ||
"net/netip" | ||
"sync/atomic" | ||
) | ||
|
||
// ipAllocator is sequential ip address allocator without garbage collection. | ||
type ipAllocator struct { | ||
offset uint32 | ||
} | ||
|
||
// Next allocates new ip address. It panics if the address space is exhausted. | ||
// It is safe for concurrent use. | ||
func (a *ipAllocator) Next() netip.Addr { | ||
data := binary.BigEndian.AppendUint32(nil, atomic.AddUint32(&a.offset, 1)) | ||
ip, ok := netip.AddrFromSlice(data) | ||
if !ok { | ||
panic("ip address overflow") | ||
} | ||
return ip | ||
} | ||
|
||
func newIPAllocator(ip netip.Addr) *ipAllocator { | ||
addr := binary.BigEndian.Uint32(ip.AsSlice()) | ||
return &ipAllocator{ | ||
offset: addr, | ||
} | ||
} |
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,19 @@ | ||
package faker | ||
|
||
import ( | ||
"net/netip" | ||
"testing" | ||
) | ||
|
||
func TestIPAllocator_Next(t *testing.T) { | ||
allocator := newIPAllocator(netip.MustParseAddr("10.0.5.0")) | ||
seen := make(map[netip.Addr]struct{}) | ||
for i := 0; i < 1600; i++ { | ||
ip := allocator.Next() | ||
if _, ok := seen[ip]; ok { | ||
t.Fatalf("duplicate ip address: %s", ip) | ||
} | ||
seen[ip] = struct{}{} | ||
} | ||
t.Logf("%s", allocator.Next()) | ||
} |
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,5 @@ | ||
package faker | ||
|
||
// request models full request trip from client to backend and back. | ||
type request struct { | ||
} |