{"id":38148699,"url":"https://github.com/netbrain/goautosocket","last_synced_at":"2026-01-16T22:59:27.523Z","repository":{"id":34186089,"uuid":"38038262","full_name":"netbrain/goautosocket","owner":"netbrain","description":"The GAS library provides auto-reconnecting TCP sockets in a tiny, fully tested, thread-safe API","archived":false,"fork":false,"pushed_at":"2015-06-25T08:43:02.000Z","size":120,"stargazers_count":19,"open_issues_count":0,"forks_count":21,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-06-19T00:29:51.504Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/netbrain.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-06-25T08:42:27.000Z","updated_at":"2024-01-22T15:03:10.000Z","dependencies_parsed_at":"2022-09-26T17:41:33.062Z","dependency_job_id":null,"html_url":"https://github.com/netbrain/goautosocket","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/netbrain/goautosocket","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/netbrain%2Fgoautosocket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/netbrain%2Fgoautosocket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/netbrain%2Fgoautosocket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/netbrain%2Fgoautosocket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/netbrain","download_url":"https://codeload.github.com/netbrain/goautosocket/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/netbrain%2Fgoautosocket/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28487053,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-16T22:54:02.790Z","status":"ssl_error","status_checked_at":"2026-01-16T22:50:10.344Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-01-16T22:59:27.457Z","updated_at":"2026-01-16T22:59:27.511Z","avatar_url":"https://github.com/netbrain.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# GoAutoSocket (GAS) ![Status](https://img.shields.io/badge/status-stable-green.svg?style=plastic) [![Build Status](http://img.shields.io/travis/teh-cmc/goautosocket.svg?style=plastic)](https://travis-ci.org/teh-cmc/goautosocket) [![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=plastic)](http://godoc.org/github.com/teh-cmc/goautosocket)\n\nThe GAS library provides auto-reconnecting TCP sockets in a tiny, fully tested, thread-safe API.\n\nThe `TCPClient` struct embeds a `net.TCPConn` and overrides its `Read()` and `Write()` methods, making it entirely compatible with the `net.Conn` interface and the rest of the `net` package.\nThis means you should be able to use this library by just replacing `net.Dial` with `gas.Dial` in your code.\n\n## Install\n\n```bash\nget -u github.com/teh-cmc/goautosocket\n```\n\n## Usage\n\nTo test the library, you can run a local TCP server with:\n\n    $ ncat -l 9999 -k\n\nand run this code:\n\n```go\npackage main\n\nimport (\n    \"log\"\n    \"time\"\n\n    \"github.com/teh-cmc/goautosocket\"\n)\n\nfunc main() {\n    // connect to a TCP server\n    conn, err := gas.Dial(\"tcp\", \"localhost:9999\")\n    if err != nil {\n        log.Fatal(err)\n    }\n\n    // client sends \"hello, world!\" to the server every second\n    for {\n        _, err := conn.Write([]byte(\"hello, world!\\n\"))\n        if err != nil {\n            // if the client reached its retry limit, give up\n            if err == gas.ErrMaxRetries {\n                log.Println(\"client gave up, reached retry limit\")\n                return\n            }\n            // not a GAS error, just panic\n            log.Fatal(err)\n        }\n        log.Println(\"client says hello!\")\n        time.Sleep(time.Second)\n    }\n}\n```\n\nThen try to kill and reboot your server, the client will automatically reconnect and start sending messages again; unless it has reached its retry limit.\n\n## Examples\n\nAn advanced example of a client writing to a buggy server that's randomly crashing and rebooting:\n\n```go\npackage main\n\nimport (\n    \"log\"\n    \"math/rand\"\n    \"net\"\n    \"sync\"\n    \"time\"\n\n    \"github.com/teh-cmc/goautosocket\"\n)\n\nfunc main() {\n    rand.Seed(time.Now().UnixNano())\n\n    // open a server socket\n    s, err := net.Listen(\"tcp\", \"localhost:0\")\n    if err != nil {\n        log.Fatal(err)\n    }\n    // save the original port\n    addr := s.Addr()\n\n    // connect a client to the server\n    c, err := gas.Dial(\"tcp\", s.Addr().String())\n    if err != nil {\n        log.Fatal(err)\n    }\n    defer c.Close()\n\n    // shut down and boot up the server randomly\n    var swg sync.WaitGroup\n    swg.Add(1)\n    go func() {\n        defer swg.Done()\n        for i := 0; i \u003c 5; i++ {\n            log.Println(\"server up\")\n            time.Sleep(time.Millisecond * 100 * time.Duration(rand.Intn(20)))\n            if err := s.Close(); err != nil {\n                log.Fatal(err)\n            }\n            log.Println(\"server down\")\n            time.Sleep(time.Millisecond * 100 * time.Duration(rand.Intn(20)))\n            s, err = net.Listen(\"tcp\", addr.String())\n            if err != nil {\n                log.Fatal(err)\n            }\n        }\n    }()\n\n    // client writes to the server and reconnects when it has to\n    // this is the interesting part\n    var cwg sync.WaitGroup\n    cwg.Add(1)\n    go func() {\n        defer cwg.Done()\n        for {\n            if _, err := c.Write([]byte(\"hello, world!\\n\")); err != nil {\n                switch e := err.(type) {\n                case gas.Error:\n                    if e == gas.ErrMaxRetries {\n                        log.Println(\"client leaving, reached retry limit\")\n                        return\n                    }\n                default:\n                    log.Fatal(err)\n                }\n            }\n            log.Println(\"client says hello!\")\n        }\n    }()\n\n    // terminates the server indefinitely\n    swg.Wait()\n    if err := s.Close(); err != nil {\n        log.Fatal(err)\n    }\n\n    // wait for the client to give up\n    cwg.Wait()\n}\n```\n\nYou can also find an example with concurrency [here](https://github.com/teh-cmc/goautosocket/blob/master/tcp_client_test.go#L97).\n\n## Disclaimer\n\nThis was built with my needs in mind, no more, no less. That is, I needed a simple, tested and thread-safe API to handle a situation in which I have:\n- on one end, a lot of goroutines concurrently writing to a TCP socket\n- on the other end, a TCP server that I have no control over (hence the main reason why UDP is out of the question) and which might be rebooted at anytime\nI also needed the ability to give up on sending a message after an abritrary amount of tries/time (i.e., ERR_MAX_TRIES). Pretty straightforward stuff.\n\nBasically, my use case is [this situation](https://github.com/teh-cmc/goautosocket/blob/master/tcp_client_test.go#L97).\n\nSurprisingly, I couldn't find such a library (I guess I either didn't look in the right place, or just not hard enough..? oh well); so here it is.\nDo not hesitate to send a pull request if this doesn't cover all your needs (and it probably won't), they are more than welcome.\n\nIf you're looking for some more insight, you might also want to look at [this discussion](http://redd.it/3aue82) we had on reddit.\n\n## License ![License](https://img.shields.io/badge/license-MIT-blue.svg?style=plastic)\n\nThe MIT License (MIT) - see LICENSE for more details\n\nCopyright (c) 2015  Clement 'cmc' Rey  \u003ccr.rey.clement@gmail.com\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnetbrain%2Fgoautosocket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnetbrain%2Fgoautosocket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnetbrain%2Fgoautosocket/lists"}