Skip to content

Instantly share code, notes, and snippets.

View dhanush's full-sized avatar

Dhanush Gopinath dhanush

View GitHub Profile
@dhanush
dhanush / publisher.go
Last active December 25, 2020 04:02
The publisher main method which will publish messages to multiple queues
package main
func main() {
conn := comms.NewConnection("my-producer", "my-exchange", []string{"queue-1", "queue-2"})
if err := conn.Connect(); err != nil {
panic(err)
}
if err := conn.BindQueue(); err != nil {
panic(err)
}
@dhanush
dhanush / consumer.go
Last active December 25, 2020 04:02
The consumer main application that will consume from multiple queues.
package main
import (
"comms"
"github.com/streadway/amqp"
"log"
)
func main() {
forever := make(chan bool)
@dhanush
dhanush / connection_publish.go
Last active December 25, 2020 04:01
Handles the publishing of messages to the queue
package comms
//Publish publishes a request to the amqp queue
func (c *Connection) Publish(m Message) error {
select { //non blocking channel - if there is no error will go to default where we do nothing
case err := <-c.err:
if err != nil {
c.Reconnect()
}
default:
@dhanush
dhanush / connection_consume.go
Last active December 25, 2020 04:02
Handles the consuming of messages from queues
package comms
//Consume consumes the messages from the queues and passes it as map of chan of amqp.Delivery
func (c *Connection) Consume() (map[string]<-chan amqp.Delivery, error) {
m := make(map[string]<-chan amqp.Delivery)
for _, q := range c.queues {
deliveries, err := c.channel.Consume(q, "", false, false, false, false, nil)
if err != nil {
return nil ,err
}
m[q] = deliveries
@dhanush
dhanush / init_rabbitmq_connection.go
Last active December 25, 2020 04:01
Has the code for initialising an amqp connection with auto reconnect feature
package comms
import (
"errors"
"fmt"
"github.com/streadway/amqp"
)
//MessageBody is the struct for the body passed in the AMQP message. The type will be set on the Request header
type MessageBody struct {
@dhanush
dhanush / build_image.go
Last active March 21, 2020 06:14
Go file to build a docker image
func BuildImage(dockerBuildCtxDir, tagName string) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(300)*time.Second)
defer cancel()
dockerFileTarReader, err := tarDirectory(dockerBuildCtxDir)
//this above method will create a tar file from the directory passed in. The return has to be of type io.Reader
if err != nil {
log.Printf("Error in taring the docker root folder - %s", err.Error())
return err
}
buildArgs := make(map[string]*string)
@dhanush
dhanush / sample_dockerfile
Last active January 17, 2019 06:59
A sample docker file
FROM alpine:3.8
RUN apk update
RUN mkdir -p /tmp/dhanush
WORKDIR /tmp/dhanush
COPY ./test.exe ./
COPY ./test.env ./
ENTRYPOINT [ "/tmp/dhanush/test.exe" ]
func downloadMultipleFiles(urls []string) ([][]byte, error) {
done := make(chan []byte, len(urls))
errch := make(chan error, len(urls))
for _, URL := range urls {
go func(URL string) {
b, err := downloadFile(URL)
if err != nil {
errch <- err
done <- nil
return
func downloadFile(URL string) ([]byte, error) {
response, err := http.Get(URL)
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil, errors.New(response.Status)
}
var data bytes.Buffer
import pandas
import math
from numpy.random import permutation
from sklearn import svm
x_cols = [] #array of all the features (column names in the file) we use to predict
y_cols = ['role'] #to be predicted item
#Create Pandas dataframe
training_data_frame = pandas.read_csv('train.csv')