Skip to content

Commit 94ad67b

Browse files
committed
refactor(abi/fastly): PR feedback: use loops vs. goto
1 parent 4eb199f commit 94ad67b

File tree

1 file changed

+139
-146
lines changed

1 file changed

+139
-146
lines changed

internal/abi/fastly/hostcalls_guest.go

Lines changed: 139 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -638,23 +638,23 @@ func fastlyHTTPReqDownstreamTLSClientHello(
638638
// DownstreamTLSClientHello returns the ClientHello message sent by the client
639639
// in the singleton downstream request, if any.
640640
func DownstreamTLSClientHello() ([]byte, error) {
641-
n := DefaultLargeBufLen
642-
alloc:
643-
buf := prim.NewWriteBuffer(n) // Longest (~132,000); typically < 2^14; RFC https://datatracker.ietf.org/doc/html/rfc8446#section-4.1.2
644-
status := fastlyHTTPReqDownstreamTLSClientHello(
645-
prim.ToPointer(buf.Char8Pointer()),
646-
buf.Cap(),
647-
prim.ToPointer(buf.NPointer()),
648-
)
649-
if status == FastlyStatusBufLen && buf.NValue() > 0 {
650-
n = int(buf.NValue())
651-
goto alloc // goto saves having to allocate a function closure and avoids having to duplicate the hostcall
652-
}
653-
if err := status.toError(); err != nil {
654-
return nil, err
641+
n := DefaultLargeBufLen // Longest (~132,000); typically < 2^14; RFC https://datatracker.ietf.org/doc/html/rfc8446#section-4.1.2
642+
for {
643+
buf := prim.NewWriteBuffer(n)
644+
status := fastlyHTTPReqDownstreamTLSClientHello(
645+
prim.ToPointer(buf.Char8Pointer()),
646+
buf.Cap(),
647+
prim.ToPointer(buf.NPointer()),
648+
)
649+
if status == FastlyStatusBufLen && buf.NValue() > 0 {
650+
n = int(buf.NValue())
651+
continue
652+
}
653+
if err := status.toError(); err != nil {
654+
return nil, err
655+
}
656+
return buf.AsBytes(), nil
655657
}
656-
657-
return buf.AsBytes(), nil
658658
}
659659

660660
// witx:
@@ -1030,24 +1030,24 @@ func fastlyHTTPReqMethodGet(
10301030

10311031
// GetMethod returns the HTTP method of the request.
10321032
func (r *HTTPRequest) GetMethod() (string, error) {
1033-
n := DefaultSmallBufLen
1034-
alloc:
1035-
buf := prim.NewWriteBuffer(n)
1036-
status := fastlyHTTPReqMethodGet(
1037-
r.h,
1038-
prim.ToPointer(buf.Char8Pointer()),
1039-
buf.Cap(),
1040-
prim.ToPointer(buf.NPointer()),
1041-
)
1042-
if status == FastlyStatusBufLen && buf.NValue() > 0 {
1043-
n = int(buf.NValue())
1044-
goto alloc // goto saves having to allocate a function closure and avoids having to duplicate the hostcall
1045-
}
1046-
if err := status.toError(); err != nil {
1047-
return "", err
1033+
n := DefaultSmallBufLen // HTTP Methods are short: GET, POST, etc.
1034+
for {
1035+
buf := prim.NewWriteBuffer(n)
1036+
status := fastlyHTTPReqMethodGet(
1037+
r.h,
1038+
prim.ToPointer(buf.Char8Pointer()),
1039+
buf.Cap(),
1040+
prim.ToPointer(buf.NPointer()),
1041+
)
1042+
if status == FastlyStatusBufLen && buf.NValue() > 0 {
1043+
n = int(buf.NValue())
1044+
continue
1045+
}
1046+
if err := status.toError(); err != nil {
1047+
return "", err
1048+
}
1049+
return buf.ToString(), nil
10481050
}
1049-
1050-
return buf.ToString(), nil
10511051
}
10521052

10531053
// witx:
@@ -1097,23 +1097,23 @@ func fastlyHTTPReqURIGet(
10971097
// GetURI returns the fully qualified URI of the request.
10981098
func (r *HTTPRequest) GetURI() (string, error) {
10991099
n := DefaultMediumBufLen // Longest (unknown); Typically less than 1024, but some browsers accept much longer
1100-
alloc:
1101-
buf := prim.NewWriteBuffer(n)
1102-
status := fastlyHTTPReqURIGet(
1103-
r.h,
1104-
prim.ToPointer(buf.Char8Pointer()),
1105-
buf.Cap(),
1106-
prim.ToPointer(buf.NPointer()),
1107-
)
1108-
if status == FastlyStatusBufLen && buf.NValue() > 0 {
1109-
n = int(buf.NValue())
1110-
goto alloc // goto saves having to allocate a function closure and avoids having to duplicate the hostcall
1111-
}
1112-
if err := status.toError(); err != nil {
1113-
return "", err
1100+
for {
1101+
buf := prim.NewWriteBuffer(n)
1102+
status := fastlyHTTPReqURIGet(
1103+
r.h,
1104+
prim.ToPointer(buf.Char8Pointer()),
1105+
buf.Cap(),
1106+
prim.ToPointer(buf.NPointer()),
1107+
)
1108+
if status == FastlyStatusBufLen && buf.NValue() > 0 {
1109+
n = int(buf.NValue())
1110+
continue
1111+
}
1112+
if err := status.toError(); err != nil {
1113+
return "", err
1114+
}
1115+
return buf.ToString(), nil
11141116
}
1115-
1116-
return buf.ToString(), nil
11171117
}
11181118

11191119
// witx:
@@ -2480,28 +2480,26 @@ func fastlyDictionaryGet(
24802480
// Get the value for key, as a byte slice, if it exists.
24812481
func (d *Dictionary) GetBytes(key string) ([]byte, error) {
24822482
keyBuffer := prim.NewReadBufferFromString(key).Wstring()
2483-
24842483
n := DefaultMediumBufLen // Longest (8192) = Config Store limit; typical values likely less than 1024
2485-
alloc:
2486-
buf := prim.NewWriteBuffer(n)
2487-
status := fastlyDictionaryGet(
2488-
d.h,
2489-
keyBuffer.Data, keyBuffer.Len,
2490-
prim.ToPointer(buf.Char8Pointer()), buf.Cap(),
2491-
prim.ToPointer(buf.NPointer()),
2492-
)
2493-
// The Dictionary API cannot return the needed size with this error.
2494-
// Instead of perfectly adapting, we allocate the maximum length a value can have.
2495-
if status == FastlyStatusBufLen && n < dictionaryValueMaxLen {
2496-
n = dictionaryValueMaxLen
2497-
goto alloc // goto saves having to allocate a function closure and avoids having to duplicate the hostcall
2498-
}
2499-
2500-
if err := status.toError(); err != nil {
2501-
return nil, err
2484+
for {
2485+
buf := prim.NewWriteBuffer(n)
2486+
status := fastlyDictionaryGet(
2487+
d.h,
2488+
keyBuffer.Data, keyBuffer.Len,
2489+
prim.ToPointer(buf.Char8Pointer()), buf.Cap(),
2490+
prim.ToPointer(buf.NPointer()),
2491+
)
2492+
if status == FastlyStatusBufLen && n < dictionaryValueMaxLen {
2493+
// The Dictionary API cannot return the needed size with this error.
2494+
// Instead of perfectly adapting, we allocate the maximum length a value can have.
2495+
n = dictionaryValueMaxLen
2496+
continue
2497+
}
2498+
if err := status.toError(); err != nil {
2499+
return nil, err
2500+
}
2501+
return buf.AsBytes(), nil
25022502
}
2503-
2504-
return buf.AsBytes(), nil
25052503
}
25062504

25072505
// Get the value for key, if it exists.
@@ -2519,24 +2517,20 @@ func (d *Dictionary) Has(key string) (bool, error) {
25192517
keyBuffer := prim.NewReadBufferFromString(key).Wstring()
25202518
var npointer prim.Usize = 0
25212519

2522-
if err := fastlyDictionaryGet(
2520+
status := fastlyDictionaryGet(
25232521
d.h,
25242522
keyBuffer.Data, keyBuffer.Len,
25252523
prim.NullChar8Pointer(), 0,
25262524
prim.ToPointer(&npointer),
2527-
); err != FastlyStatusOK {
2528-
if err == FastlyStatusBufLen {
2529-
return true, nil
2530-
}
2531-
2532-
if err == FastlyStatusNone {
2533-
return false, nil
2534-
}
2535-
2536-
return false, err.toError()
2525+
)
2526+
switch status {
2527+
case FastlyStatusOK, FastlyStatusBufLen:
2528+
return true, nil
2529+
case FastlyStatusNone:
2530+
return false, nil
2531+
default:
2532+
return false, status.toError()
25372533
}
2538-
2539-
return true, nil
25402534
}
25412535

25422536
// witx:
@@ -2569,22 +2563,22 @@ func GeoLookup(ip net.IP) ([]byte, error) {
25692563
addrOctets := prim.NewReadBufferFromBytes(ip)
25702564

25712565
n := DefaultMediumBufLen
2572-
alloc:
2573-
buf := prim.NewWriteBuffer(n) // initial geo buf size
2574-
status := fastlyGeoLookup(
2575-
prim.ToPointer(addrOctets.Char8Pointer()), addrOctets.Len(),
2576-
prim.ToPointer(buf.Char8Pointer()), buf.Cap(),
2577-
prim.ToPointer(buf.NPointer()),
2578-
)
2579-
if status == FastlyStatusBufLen && buf.NValue() > 0 {
2580-
n = int(buf.NValue())
2581-
goto alloc // goto saves having to allocate a function closure and avoids having to duplicate the hostcall
2582-
}
2583-
if err := status.toError(); err != nil {
2584-
return nil, err
2566+
for {
2567+
buf := prim.NewWriteBuffer(n) // initial geo buf size
2568+
status := fastlyGeoLookup(
2569+
prim.ToPointer(addrOctets.Char8Pointer()), addrOctets.Len(),
2570+
prim.ToPointer(buf.Char8Pointer()), buf.Cap(),
2571+
prim.ToPointer(buf.NPointer()),
2572+
)
2573+
if status == FastlyStatusBufLen && buf.NValue() > 0 {
2574+
n = int(buf.NValue())
2575+
continue
2576+
}
2577+
if err := status.toError(); err != nil {
2578+
return nil, err
2579+
}
2580+
return buf.AsBytes(), nil
25852581
}
2586-
2587-
return buf.AsBytes(), nil
25882582
}
25892583

25902584
// witx:
@@ -2853,24 +2847,23 @@ func fastlySecretPlaintext(
28532847
// Plaintext decrypts and returns the secret value as a byte slice.
28542848
func (s *Secret) Plaintext() ([]byte, error) {
28552849
n := DefaultMediumBufLen
2856-
alloc:
2857-
buf := prim.NewWriteBuffer(n)
2858-
status := fastlySecretPlaintext(
2859-
s.h,
2860-
prim.ToPointer(buf.Char8Pointer()),
2861-
buf.Cap(),
2862-
prim.ToPointer(buf.NPointer()),
2863-
)
2864-
if status == FastlyStatusBufLen && buf.NValue() > 0 {
2865-
n = int(buf.NValue())
2866-
goto alloc // goto saves having to allocate a function closure and avoids having to duplicate the hostcall
2867-
}
2868-
2869-
if err := status.toError(); err != nil {
2870-
return nil, err
2850+
for {
2851+
buf := prim.NewWriteBuffer(n)
2852+
status := fastlySecretPlaintext(
2853+
s.h,
2854+
prim.ToPointer(buf.Char8Pointer()),
2855+
buf.Cap(),
2856+
prim.ToPointer(buf.NPointer()),
2857+
)
2858+
if status == FastlyStatusBufLen && buf.NValue() > 0 {
2859+
n = int(buf.NValue())
2860+
continue
2861+
}
2862+
if err := status.toError(); err != nil {
2863+
return nil, err
2864+
}
2865+
return buf.AsBytes(), nil
28712866
}
2872-
2873-
return buf.AsBytes(), nil
28742867
}
28752868

28762869
// witx:
@@ -3307,23 +3300,23 @@ func fastlyCacheGetUserMetadata(
33073300

33083301
func (c *CacheEntry) UserMetadata() ([]byte, error) {
33093302
n := DefaultMediumBufLen
3310-
alloc:
3311-
buf := prim.NewWriteBuffer(n) // Longest (unknown)
3312-
status := fastlyCacheGetUserMetadata(
3313-
c.h,
3314-
prim.ToPointer(buf.U8Pointer()),
3315-
buf.Cap(),
3316-
prim.ToPointer(buf.NPointer()),
3317-
)
3318-
if status == FastlyStatusBufLen && buf.NValue() > 0 {
3319-
n = int(buf.NValue())
3320-
goto alloc // goto saves having to allocate a function closure and avoids having to duplicate the hostcall
3321-
}
3322-
if err := status.toError(); err != nil {
3323-
return nil, err
3303+
for {
3304+
buf := prim.NewWriteBuffer(n) // Longest (unknown)
3305+
status := fastlyCacheGetUserMetadata(
3306+
c.h,
3307+
prim.ToPointer(buf.U8Pointer()),
3308+
buf.Cap(),
3309+
prim.ToPointer(buf.NPointer()),
3310+
)
3311+
if status == FastlyStatusBufLen && buf.NValue() > 0 {
3312+
n = int(buf.NValue())
3313+
continue
3314+
}
3315+
if err := status.toError(); err != nil {
3316+
return nil, err
3317+
}
3318+
return buf.AsBytes(), nil
33243319
}
3325-
3326-
return buf.AsBytes(), nil
33273320
}
33283321

33293322
// witx:
@@ -3542,23 +3535,23 @@ func fastlyDeviceDetectionLookup(
35423535
func DeviceLookup(userAgent string) ([]byte, error) {
35433536
userAgentBuffer := prim.NewReadBufferFromString(userAgent).Wstring()
35443537
n := DefaultMediumBufLen // Longest JSON of https://www.fastly.com/documentation/reference/vcl/variables/client-request/client-identified/
3545-
alloc:
3546-
buf := prim.NewWriteBuffer(n)
3547-
status := fastlyDeviceDetectionLookup(
3548-
userAgentBuffer.Data, userAgentBuffer.Len,
3549-
prim.ToPointer(buf.Char8Pointer()),
3550-
buf.Cap(),
3551-
prim.ToPointer(buf.NPointer()),
3552-
)
3553-
if status == FastlyStatusBufLen && buf.NValue() > 0 {
3554-
n = int(buf.NValue())
3555-
goto alloc // goto saves having to allocate a function closure and avoids having to duplicate the hostcall
3556-
}
3557-
if err := status.toError(); err != nil {
3558-
return nil, err
3538+
for {
3539+
buf := prim.NewWriteBuffer(n)
3540+
status := fastlyDeviceDetectionLookup(
3541+
userAgentBuffer.Data, userAgentBuffer.Len,
3542+
prim.ToPointer(buf.Char8Pointer()),
3543+
buf.Cap(),
3544+
prim.ToPointer(buf.NPointer()),
3545+
)
3546+
if status == FastlyStatusBufLen && buf.NValue() > 0 {
3547+
n = int(buf.NValue())
3548+
continue
3549+
}
3550+
if err := status.toError(); err != nil {
3551+
return nil, err
3552+
}
3553+
return buf.AsBytes(), nil
35593554
}
3560-
3561-
return buf.AsBytes(), nil
35623555
}
35633556

35643557
// witx:

0 commit comments

Comments
 (0)