Skip to content

Commit

Permalink
Merge pull request #366 from rabilrbl/develop
Browse files Browse the repository at this point in the history
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
fix: Increase buffer size when downloading binary in update command (#365)
  • Loading branch information
rabilrbl and dependabot[bot] authored Aug 6, 2024
2 parents 89d6131 + 28ab908 commit 8c370c8
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 79 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pre-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:

- name: Setup android NDK
run: |
wget -q --show-progress https://dl.google.com/android/repository/android-ndk-r26b-linux.zip
wget -q https://dl.google.com/android/repository/android-ndk-r26b-linux.zip
unzip -qq android-ndk-r26b-linux.zip
echo "$PWD/android-ndk-r26b/toolchains/llvm/prebuilt/linux-x86_64/bin" >> $GITHUB_PATH
Expand Down
128 changes: 61 additions & 67 deletions .github/workflows/test_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,75 +21,69 @@ concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

permissions:
contents: read
pull-requests: write
repository-projects: write

jobs:
init:
permissions:
contents: read
pull-requests: write
runs-on: ubuntu-latest
steps:
- name: Comment Build started
run: |
gh pr comment "$PR_URL" -b "Build started for this PR. [Check logs](${{ github.event.repository.html_url }}/actions/runs/${{ github.run_id }})"
env:
PR_URL: ${{ github.event.pull_request.html_url }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
init:
permissions:
contents: read
pull-requests: write
runs-on: ubuntu-latest
steps:
- name: Comment Build started
run: |
gh pr comment "$PR_URL" -b "Build started for this PR. [Check logs](${{ github.event.repository.html_url }}/actions/runs/${{ github.run_id }})"
env:
PR_URL: ${{ github.event.pull_request.html_url }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

build:
needs: init
runs-on: ubuntu-latest
strategy:
matrix:
goos: [linux, windows, darwin]
goarch: [amd64, arm64]
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version-file: ./go.mod

build:
needs: init
permissions:
contents: read
runs-on: ubuntu-latest
strategy:
matrix:
goos: [linux, windows]
goarch: [amd64, arm64]
exclude:
- goos: windows
goarch: arm64
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version-file: ./go.mod

- name: Build binary for ${{ matrix.goos }}/${{ matrix.goarch }}
run: |
GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} go build -o ./bin/jiotv_go-${{ matrix.goos }}-${{ matrix.goarch }} -ldflags "-s -w" .
- name: Build binary for ${{ matrix.goos }}/${{ matrix.goarch }}
run: |
GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} go build -o ./bin/jiotv_go-${{ matrix.goos }}-${{ matrix.goarch }} -ldflags "-s -w" .
- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: jiotv_go-${{ matrix.goos }}-${{ matrix.goarch }}
path: ./bin/
- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: jiotv_go-${{ matrix.goos }}-${{ matrix.goarch }}
path: ./bin/

post_success:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
needs: build
if: success()
steps:
- name: Comment Build success
run: |
gh pr comment "$PR_URL" --edit-last -b "Build success for this PR ✅. [Download artifacts](${{ github.event.repository.html_url }}/actions/runs/${{ github.run_id }})"
env:
PR_URL: ${{ github.event.pull_request.html_url }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
post_success:
runs-on: ubuntu-latest
needs: build
if: success()
steps:
- name: Comment Build success
run: |
gh pr comment "$PR_URL" --edit-last -b "Build success for this PR ✅. [Download artifacts](${{ github.event.repository.html_url }}/actions/runs/${{ github.run_id }})"
env:
PR_URL: ${{ github.event.pull_request.html_url }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

post_failure:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
needs: build
if: failure()
steps:
- name: Comment Build failed
run: |
gh pr comment "$PR_URL" --edit-last -b "Build failed for this PR ❌. [Check logs](${{ github.event.repository.html_url }}/actions/runs/${{ github.run_id }})"
env:
PR_URL: ${{ github.event.pull_request.html_url }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
post_failure:
runs-on: ubuntu-latest
needs: build
if: failure()
steps:
- name: Comment Build failed
run: |
gh pr comment "$PR_URL" --edit-last -b "Build failed for this PR ❌. [Check logs](${{ github.event.repository.html_url }}/actions/runs/${{ github.run_id }})"
env:
PR_URL: ${{ github.event.pull_request.html_url }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
36 changes: 28 additions & 8 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,37 @@ type Release struct {
// It returns an error if the request fails or the status code is not 200 OK.
// The saved binary file is made executable.
func downloadBinary(url, outputPath string) error {
statusCode, body, err := fasthttp.Get(nil, url)
if err != nil {
return err
}
initialBufferSize := 8192
maxBufferSize := 32768

// Iterate through buffer sizes, starting from initialBufferSize and doubling each time
for bufferSize := initialBufferSize; bufferSize <= maxBufferSize; bufferSize *= 2 {
client := &fasthttp.Client{
ReadBufferSize: bufferSize, // Set the read buffer size for the client
}

// Perform an HTTP GET request
statusCode, body, err := client.Get(nil, url)
if err != nil {
// Check if the error is due to a small read buffer and if we can increase the buffer size
if strings.Contains(err.Error(), "small read buffer") && bufferSize < maxBufferSize {
fmt.Println("Increasing buffer size and retrying...")
continue // Retry with a larger buffer size
}
return err // Return the error if it's not related to buffer size or max buffer size is reached
}

if statusCode != fasthttp.StatusOK {
return fmt.Errorf("failed to download binary. Status code: %d", statusCode)
}

if statusCode != fasthttp.StatusOK {
return fmt.Errorf("failed to download binary. Status code: %d", statusCode)
// Write the downloaded binary to the specified output path with executable permissions
// skipcq: GSC-G302 - We want executable permissions on the binary
return os.WriteFile(outputPath, body, 0744)
}

// skipcq: GSC-G302 - We want executable permissions on the binary
return os.WriteFile(outputPath, body, 0744)
// Return an error if the binary could not be downloaded after increasing the buffer size
return fmt.Errorf("failed to download binary after increasing buffer size")
}

// replaceBinary replaces the current executable binary with a new binary.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ require (
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/schollz/progressbar/v3 v3.14.5
github.com/schollz/progressbar/v3 v3.14.6
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.55.0
github.com/valyala/tcplisten v1.0.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/schollz/progressbar/v3 v3.14.5 h1:97RrSxbBASxQuZN9yemnyGrFZ/swnG6IrEe2R0BseX8=
github.com/schollz/progressbar/v3 v3.14.5/go.mod h1:Nrzpuw3Nl0srLY0VlTvC4V6RL50pcEymjy6qyJAaLa0=
github.com/schollz/progressbar/v3 v3.14.6 h1:GyjwcWBAf+GFDMLziwerKvpuS7ZF+mNTAXIB2aspiZs=
github.com/schollz/progressbar/v3 v3.14.6/go.mod h1:Nrzpuw3Nl0srLY0VlTvC4V6RL50pcEymjy6qyJAaLa0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
Expand Down

0 comments on commit 8c370c8

Please sign in to comment.